Browse Source

[i18n] rework localization system

Signed-off-by: R4SAS <r4sas@i2pmail.org>
pull/1659/head
R4SAS 4 years ago
parent
commit
779f2fa451
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 6
      daemon/Daemon.cpp
  2. 52
      i18n/English.cpp
  3. 38
      i18n/I18N.h
  4. 59
      i18n/I18N_langs.h
  5. 46
      i18n/Russian.cpp
  6. 7
      libi2pd/RouterContext.cpp
  7. 12
      libi2pd/RouterContext.h

6
daemon/Daemon.cpp

@ -32,6 +32,7 @@
#include "UPnP.h" #include "UPnP.h"
#include "Timestamp.h" #include "Timestamp.h"
#include "util.h" #include "util.h"
#include "I18N.h"
namespace i2p namespace i2p
{ {
@ -345,10 +346,7 @@ namespace util
} }
std::string httpLang; i2p::config::GetOption("http.lang", httpLang); std::string httpLang; i2p::config::GetOption("http.lang", httpLang);
if (!httpLang.compare("russian")) i2p::i18n::SetLanguage(httpLang);
i2p::context.SetLanguage (eRussian);
else
i2p::context.SetLanguage (eEnglish);
return true; return true;
} }

52
i18n/English.cpp

@ -1,23 +1,34 @@
/*
* Copyright (c) 2021, 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 <map> #include <map>
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory>
#include "I18N.h"
// Russian localization file // English localization file
namespace i2p {
namespace i18n {
namespace english { // language
namespace i2p
{
namespace i18n
{
namespace english // language
{
// See for language plural forms here: // See for language plural forms here:
// https://localization-guide.readthedocs.io/en/latest/l10n/pluralforms.html // https://localization-guide.readthedocs.io/en/latest/l10n/pluralforms.html
int plural (int n) { static int plural (int n) {
return n != 1 ? 1 : 0; return n != 1 ? 1 : 0;
} }
static std::map<std::string, std::string> strings static std::map<std::string, std::string> strings
{ {
{"Enabled", "Enabled"}, {"", ""},
{"Disabled", "Disabled"}
}; };
static std::map<std::string, std::vector<std::string>> plurals static std::map<std::string, std::vector<std::string>> plurals
@ -25,30 +36,13 @@ namespace english { // language
{"days", {"day", "days"}}, {"days", {"day", "days"}},
{"hours", {"hour", "hours"}}, {"hours", {"hour", "hours"}},
{"minutes", {"minute", "minutes"}}, {"minutes", {"minute", "minutes"}},
{"seconds", {"second", "seconds"}} {"seconds", {"second", "seconds"}},
{"", {"", ""}},
}; };
std::string GetString (std::string arg) std::shared_ptr<const i2p::i18n::Locale> GetLocale()
{
auto it = strings.find(arg);
if (it == strings.end())
{
return arg;
} else {
return it->second;
}
}
std::string GetPlural (std::string arg, int n)
{ {
auto it = plurals.find(arg); return std::make_shared<i2p::i18n::Locale>(strings, plurals, [] (int n)->int { return plural(n); });
if (it == plurals.end())
{
return arg;
} else {
int form = plural(n);
return it->second[form];
}
} }
} // language } // language

38
i18n/I18N.h

@ -11,37 +11,27 @@
#include "RouterContext.h" #include "RouterContext.h"
namespace i2p
namespace i2p { {
namespace i18n { namespace i18n
{
inline void SetLanguage(const std::string &lang)
{
if (!lang.compare("russian"))
i2p::context.SetLanguage (i2p::i18n::russian::GetLocale());
else
i2p::context.SetLanguage (i2p::i18n::english::GetLocale());
}
inline std::string translate (const std::string& arg) inline std::string translate (const std::string& arg)
{ {
switch (i2p::context.GetLanguage ()) return i2p::context.GetLanguage ()->GetString (arg);
{
case eEnglish:
return i2p::i18n::english::GetString (arg);
case eRussian:
return i2p::i18n::russian::GetString (arg);
default:
return arg;
}
} }
template<typename inttype> inline std::string translate (const std::string& arg, const int& n)
std::string translate (const std::string& arg, inttype&& n)
{ {
switch (i2p::context.GetLanguage ()) return i2p::context.GetLanguage ()->GetPlural (arg, n);
{
case eEnglish:
return i2p::i18n::english::GetPlural (arg, (int) n);
case eRussian:
return i2p::i18n::russian::GetPlural (arg, (int) n);
default:
return arg;
}
} }
} // i18n } // i18n
} // i2p } // i2p

59
i18n/I18N_langs.h

@ -9,24 +9,55 @@
#ifndef __I18N_LANGS_H__ #ifndef __I18N_LANGS_H__
#define __I18N_LANGS_H__ #define __I18N_LANGS_H__
namespace i2p { namespace i2p
{
namespace i18n
{
class Locale
{
public:
Locale (
const std::map<std::string, std::string>& strings,
const std::map<std::string, std::vector<std::string>>& plurals,
std::function<int(int)> formula
): m_Strings (strings), m_Plurals (plurals), m_Formula (formula) { };
enum Lang { std::string GetString (const std::string& arg) const
eEnglish = 0, {
eRussian const auto it = m_Strings.find(arg);
}; if (it == m_Strings.end())
{
return arg;
}
else
{
return it->second;
}
}
namespace i18n { std::string GetPlural (const std::string& arg, const int& n) const
{
const auto it = m_Plurals.find(arg);
if (it == m_Plurals.end())
{
return arg;
}
else
{
int form = m_Formula(n);
return it->second[form];
}
}
namespace english { private:
std::string GetString (std::string arg); const std::map<std::string, std::string> m_Strings;
std::string GetPlural (std::string arg, int n); const std::map<std::string, std::vector<std::string>> m_Plurals;
} std::function<int(int)> m_Formula;
};
namespace russian { // Add localization here with language name as namespace
std::string GetString (std::string arg); namespace english { std::shared_ptr<const i2p::i18n::Locale> GetLocale (); }
std::string GetPlural (std::string arg, int n); namespace russian { std::shared_ptr<const i2p::i18n::Locale> GetLocale (); }
}
} // i18n } // i18n
} // i2p } // i2p

46
i18n/Russian.cpp

@ -1,16 +1,28 @@
/*
* Copyright (c) 2021, 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 <map> #include <map>
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory>
#include "I18N.h"
// Russian localization file // Russian localization file
namespace i2p { namespace i2p
namespace i18n { {
namespace russian { // language namespace i18n
{
namespace russian // language
{
// See for language plural forms here: // See for language plural forms here:
// https://localization-guide.readthedocs.io/en/latest/l10n/pluralforms.html // https://localization-guide.readthedocs.io/en/latest/l10n/pluralforms.html
int plural (int n) { static int plural (int n) {
return n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2; return n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
} }
@ -218,30 +230,12 @@ namespace russian { // language
{"hours", {"час", "часа", "часов"}}, {"hours", {"час", "часа", "часов"}},
{"minutes", {"минуту", "минуты", "минут"}}, {"minutes", {"минуту", "минуты", "минут"}},
{"seconds", {"секунду", "секунды", "секунд"}}, {"seconds", {"секунду", "секунды", "секунд"}},
{"", {"", ""}}, {"", {"", "", ""}},
}; };
std::string GetString (std::string arg) std::shared_ptr<const i2p::i18n::Locale> GetLocale()
{
auto it = strings.find(arg);
if (it == strings.end())
{
return arg;
} else {
return it->second;
}
}
std::string GetPlural (std::string arg, int n)
{ {
auto it = plurals.find(arg); return std::make_shared<i2p::i18n::Locale>(strings, plurals, [] (int n)->int { return plural(n); });
if (it == plurals.end())
{
return arg;
} else {
int form = plural(n);
return it->second[form];
}
} }
} // language } // language

7
libi2pd/RouterContext.cpp

@ -29,7 +29,7 @@ namespace i2p
RouterContext::RouterContext (): RouterContext::RouterContext ():
m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false), m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false),
m_ShareRatio (100), m_Status (eRouterStatusUnknown), m_StatusV6 (eRouterStatusUnknown), m_ShareRatio (100), m_Status (eRouterStatusUnknown), m_StatusV6 (eRouterStatusUnknown),
m_Error (eRouterErrorNone), m_NetID (I2PD_NET_ID), m_Language (eEnglish) m_Error (eRouterErrorNone), m_NetID (I2PD_NET_ID)
{ {
} }
@ -910,11 +910,6 @@ namespace i2p
} }
} }
void RouterContext::SetLanguage (Lang language)
{
m_Language = language;
}
i2p::crypto::X25519Keys& RouterContext::GetStaticKeys () i2p::crypto::X25519Keys& RouterContext::GetStaticKeys ()
{ {
if (!m_StaticKeys) if (!m_StaticKeys)

12
libi2pd/RouterContext.h

@ -99,7 +99,7 @@ namespace garlic
bool DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data); bool DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data);
void UpdatePort (int port); // called from Daemon void UpdatePort (int port); // called from Daemon
void UpdateAddress (const boost::asio::ip::address& host); // called from SSU or Daemon void UpdateAddress (const boost::asio::ip::address& host); // called from SSU or Daemon
void PublishNTCP2Address (int port, bool publish, bool v4, bool v6, bool ygg); void PublishNTCP2Address (int port, bool publish, bool v4, bool v6, bool ygg);
void UpdateNTCP2Address (bool enable); void UpdateNTCP2Address (bool enable);
void RemoveNTCPAddress (bool v4only = true); // delete NTCP address for older routers. TODO: remove later void RemoveNTCPAddress (bool v4only = true); // delete NTCP address for older routers. TODO: remove later
@ -125,11 +125,11 @@ namespace garlic
void SetSupportsMesh (bool supportsmesh, const boost::asio::ip::address_v6& host); void SetSupportsMesh (bool supportsmesh, const boost::asio::ip::address_v6& host);
bool IsECIES () const { return GetIdentity ()->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD; }; bool IsECIES () const { return GetIdentity ()->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD; };
std::unique_ptr<i2p::crypto::NoiseSymmetricState>& GetCurrentNoiseState () { return m_CurrentNoiseState; }; std::unique_ptr<i2p::crypto::NoiseSymmetricState>& GetCurrentNoiseState () { return m_CurrentNoiseState; };
void UpdateNTCP2V6Address (const boost::asio::ip::address& host); // called from Daemon. TODO: remove void UpdateNTCP2V6Address (const boost::asio::ip::address& host); // called from Daemon. TODO: remove
void UpdateStats (); void UpdateStats ();
void UpdateTimestamp (uint64_t ts); // in seconds, called from NetDb before publishing void UpdateTimestamp (uint64_t ts); // in seconds, called from NetDb before publishing
void CleanupDestination (); // garlic destination void CleanupDestination (); // garlic destination
// implements LocalDestination // implements LocalDestination
std::shared_ptr<const i2p::data::IdentityEx> GetIdentity () const { return m_Keys.GetPublic (); }; std::shared_ptr<const i2p::data::IdentityEx> GetIdentity () const { return m_Keys.GetPublic (); };
@ -146,8 +146,8 @@ namespace garlic
void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg); void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
// i18n // i18n
Lang GetLanguage () const { return m_Language; }; std::shared_ptr<const i2p::i18n::Locale> GetLanguage () { return m_Language; };
void SetLanguage (Lang language); void SetLanguage (const std::shared_ptr<const i2p::i18n::Locale> language) { m_Language = language; };
protected: protected:
@ -185,7 +185,7 @@ namespace garlic
std::unique_ptr<i2p::crypto::NoiseSymmetricState> m_InitialNoiseState, m_CurrentNoiseState; std::unique_ptr<i2p::crypto::NoiseSymmetricState> m_InitialNoiseState, m_CurrentNoiseState;
// i18n // i18n
Lang m_Language; std::shared_ptr<const i2p::i18n::Locale> m_Language;
}; };
extern RouterContext context; extern RouterContext context;

Loading…
Cancel
Save