diff --git a/i18n/I18N.cpp b/i18n/I18N.cpp new file mode 100644 index 00000000..fe04bcb6 --- /dev/null +++ b/i18n/I18N.cpp @@ -0,0 +1,36 @@ +/* +* Copyright (c) 2021-2022, The PurpleI2P Project +* +* This file is part of Purple i2pd project and licensed under BSD3 +* +* See full license text in LICENSE file at top of project tree +*/ + +#include "ClientContext.h" +#include "I18N_langs.h" +#include "I18N.h" + +namespace i2p +{ +namespace i18n +{ + void SetLanguage(const std::string &lang) + { + const auto it = i2p::i18n::languages.find(lang); + if (it == i2p::i18n::languages.end()) // fallback + i2p::client::context.SetLanguage (i2p::i18n::english::GetLocale()); + else + i2p::client::context.SetLanguage (it->second.LocaleFunc()); + } + + std::string translate (const std::string& arg) + { + return i2p::client::context.GetLanguage ()->GetString (arg); + } + + std::string translate (const std::string& arg, const std::string& arg2, const int& n) + { + return i2p::client::context.GetLanguage ()->GetPlural (arg, arg2, n); + } +} // i18n +} // i2p diff --git a/i18n/I18N.h b/i18n/I18N.h index dd804926..27e043b9 100644 --- a/i18n/I18N.h +++ b/i18n/I18N.h @@ -9,30 +9,68 @@ #ifndef __I18N_H__ #define __I18N_H__ -#include "ClientContext.h" +#include +#include +#include +#include namespace i2p { namespace i18n { - inline void SetLanguage(const std::string &lang) + class Locale { - const auto it = i2p::i18n::languages.find(lang); - if (it == i2p::i18n::languages.end()) // fallback - i2p::client::context.SetLanguage (i2p::i18n::english::GetLocale()); - else - i2p::client::context.SetLanguage (it->second.LocaleFunc()); - } - - inline std::string translate (const std::string& arg) - { - return i2p::client::context.GetLanguage ()->GetString (arg); - } + public: + Locale ( + const std::string& language, + const std::map& strings, + const std::map>& plurals, + std::function formula + ): m_Language (language), m_Strings (strings), m_Plurals (plurals), m_Formula (formula) { }; - inline std::string translate (const std::string& arg, const std::string& arg2, const int& n) - { - return i2p::client::context.GetLanguage ()->GetPlural (arg, arg2, n); - } + // Get activated language name for webconsole + std::string GetLanguage() const + { + return m_Language; + } + + std::string GetString (const std::string& arg) const + { + const auto it = m_Strings.find(arg); + if (it == m_Strings.end()) + { + return arg; + } + else + { + return it->second; + } + } + + std::string GetPlural (const std::string& arg, const std::string& arg2, const int& n) const + { + const auto it = m_Plurals.find(arg2); + if (it == m_Plurals.end()) // not found, fallback to english + { + return n == 1 ? arg : arg2; + } + else + { + int form = m_Formula(n); + return it->second[form]; + } + } + + private: + const std::string m_Language; + const std::map m_Strings; + const std::map> m_Plurals; + std::function m_Formula; + }; + + void SetLanguage(const std::string &lang); + std::string translate (const std::string& arg); + std::string translate (const std::string& arg, const std::string& arg2, const int& n); } // i18n } // i2p diff --git a/i18n/I18N_langs.h b/i18n/I18N_langs.h index da70e578..42c7ba4e 100644 --- a/i18n/I18N_langs.h +++ b/i18n/I18N_langs.h @@ -9,60 +9,12 @@ #ifndef __I18N_LANGS_H__ #define __I18N_LANGS_H__ +#include "I18N.h" + namespace i2p { namespace i18n { - class Locale - { - public: - Locale ( - const std::string& language, - const std::map& strings, - const std::map>& plurals, - std::function formula - ): m_Language (language), m_Strings (strings), m_Plurals (plurals), m_Formula (formula) { }; - - // Get activated language name for webconsole - std::string GetLanguage() const - { - return m_Language; - } - - std::string GetString (const std::string& arg) const - { - const auto it = m_Strings.find(arg); - if (it == m_Strings.end()) - { - return arg; - } - else - { - return it->second; - } - } - - std::string GetPlural (const std::string& arg, const std::string& arg2, const int& n) const - { - const auto it = m_Plurals.find(arg2); - if (it == m_Plurals.end()) // not found, fallback to english - { - return n == 1 ? arg : arg2; - } - else - { - int form = m_Formula(n); - return it->second[form]; - } - } - - private: - const std::string m_Language; - const std::map m_Strings; - const std::map> m_Plurals; - std::function m_Formula; - }; - struct langData { std::string LocaleName; // localized name