Информация об изменениях

Сообщение Re[6]: Простой способ статической языковой интернациализации от 26.10.2016 20:37

Изменено 27.10.2016 10:23 _niko_

Ну или как то так:
  langs.h
#ifndef LANG_ID
#   define LANG_ID__EN_US 0x0409
#   define LANG_ID__RU_RU 0x0419

#   define LANG_ID LANG_ID__EN_US

#    if LANG_ID == LANG_ID__EN_US
#        define LANGS(msg_en_US, msg_ru_RU) msg_en_US
#    elif LANG_ID == LANG_ID__RU_RU
#        define LANGS(msg_en_US, msg_ru_RU) msg_ru_RU
#    endif
#endif // LANG_ID

LOCALIZATION_ITEM(hello, "Hello", "Привет")
LOCALIZATION_ITEM(bye,   "Bye",   "Пока")

  localization.h
#ifndef LOCALIZATION_H
#define LOCALIZATION_H

#include <string>

namespace localization
{
    enum class msg_id
    {
        unknown = 0,

        #define LOCALIZATION_ITEM(id, ...) id,
        #include "langs.h"
        #undef LOCALIZATION_ITEM
    };

    std::string message(msg_id id);

} // namespace localization

#endif // LOCALIZATION_H

  localization.cpp
#include <exception>

#include "localization.h"

#define LOCALIZATION_ITEM(...)
#include "langs.h"
#undef LOCALIZATION_ITEM


namespace localization
{
    std::string message(msg_id id)
    {
        switch (id)
        {
        case msg_id::unknown:
            return std::string();
        #define LOCALIZATION_ITEM(id, msg_en_US, msg_ru_RU) case msg_id::id: return std::string(LANGS(msg_en_US, msg_ru_RU));
        #include "langs.h"
        #undef LOCALIZATION_ITEM

        default:
            throw std::runtime_error("bad msg_id");
        }
    }
    
} // namespace localization

  main.cpp
#include <iostream>

#include "localization.h"

int main(int, char**)
{
    std::cout << localization::message(localization::msg_id::hello) << std::endl;
    std::cout << localization::message(localization::msg_id::bye) << std::endl;

    return 0;
}
Re[6]: Простой способ статической языковой интернациализации
Ну или как то так:
  langs.h
#ifndef LANG_ID
#   define LANG_ID__EN_US 0x0409
#   define LANG_ID__RU_RU 0x0419

#   define LANG_ID LANG_ID__EN_US

#    if LANG_ID == LANG_ID__EN_US
#        define LANGS(msg_en_US, msg_ru_RU) msg_en_US
#    elif LANG_ID == LANG_ID__RU_RU
#        define LANGS(msg_en_US, msg_ru_RU) msg_ru_RU
#    endif
#endif // LANG_ID

LOCALIZATION_ITEM(hello, "Hello", "Привет")
LOCALIZATION_ITEM(bye,   "Bye",   "Пока")

  localization.h
#ifndef LOCALIZATION_H
#define LOCALIZATION_H

#include <string>

namespace localization
{
    enum class msg_id
    {
        unknown = 0,

        #define LOCALIZATION_ITEM(id, ...) id,
        #include "langs.h"
        #undef LOCALIZATION_ITEM
    };

    std::string message(msg_id id);

} // namespace localization

#endif // LOCALIZATION_H

  localization.cpp
#include <exception>

#include "localization.h"

#define LOCALIZATION_ITEM(...)
#include "langs.h"
#undef LOCALIZATION_ITEM


namespace localization
{
    std::string message(msg_id id)
    {
        switch (id)
        {
        case msg_id::unknown:
            return std::string();
        #define LOCALIZATION_ITEM(id, ...) case msg_id::id: return std::string(LANGS(__VA_ARGS__));
        #include "langs.h"
        #undef LOCALIZATION_ITEM

        default:
            throw std::runtime_error("bad msg_id");
        }
    }
    
} // namespace localization

  main.cpp
#include <iostream>

#include "localization.h"

int main(int, char**)
{
    std::cout << localization::message(localization::msg_id::hello) << std::endl;
    std::cout << localization::message(localization::msg_id::bye) << std::endl;

    return 0;
}