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. 6
      libi2pd/RouterContext.h

6
daemon/Daemon.cpp

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

52
i18n/English.cpp

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

38
i18n/I18N.h

@ -11,37 +11,27 @@ @@ -11,37 +11,27 @@
#include "RouterContext.h"
namespace i2p {
namespace i18n {
namespace i2p
{
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)
{
switch (i2p::context.GetLanguage ())
{
case eEnglish:
return i2p::i18n::english::GetString (arg);
case eRussian:
return i2p::i18n::russian::GetString (arg);
default:
return arg;
}
return i2p::context.GetLanguage ()->GetString (arg);
}
template<typename inttype>
std::string translate (const std::string& arg, inttype&& n)
{
switch (i2p::context.GetLanguage ())
inline std::string translate (const std::string& arg, const int& n)
{
case eEnglish:
return i2p::i18n::english::GetPlural (arg, (int) n);
case eRussian:
return i2p::i18n::russian::GetPlural (arg, (int) n);
default:
return arg;
return i2p::context.GetLanguage ()->GetPlural (arg, n);
}
}
} // i18n
} // i2p

59
i18n/I18N_langs.h

@ -9,24 +9,55 @@ @@ -9,24 +9,55 @@
#ifndef __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 {
eEnglish = 0,
eRussian
};
namespace i18n {
namespace english {
std::string GetString (std::string arg);
std::string GetPlural (std::string arg, int n);
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;
}
}
namespace russian {
std::string GetString (std::string arg);
std::string GetPlural (std::string arg, int n);
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];
}
}
private:
const std::map<std::string, std::string> m_Strings;
const std::map<std::string, std::vector<std::string>> m_Plurals;
std::function<int(int)> m_Formula;
};
// Add localization here with language name as namespace
namespace english { std::shared_ptr<const i2p::i18n::Locale> GetLocale (); }
namespace russian { std::shared_ptr<const i2p::i18n::Locale> GetLocale (); }
} // i18n
} // i2p

46
i18n/Russian.cpp

@ -1,16 +1,28 @@ @@ -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 <vector>
#include <string>
#include <memory>
#include "I18N.h"
// Russian localization file
namespace i2p {
namespace i18n {
namespace russian { // language
namespace i2p
{
namespace i18n
{
namespace russian // language
{
// See for language plural forms here:
// 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;
}
@ -218,30 +230,12 @@ namespace russian { // language @@ -218,30 +230,12 @@ namespace russian { // language
{"hours", {"час", "часа", "часов"}},
{"minutes", {"минуту", "минуты", "минут"}},
{"seconds", {"секунду", "секунды", "секунд"}},
{"", {"", ""}},
{"", {"", "", ""}},
};
std::string GetString (std::string arg)
{
auto it = strings.find(arg);
if (it == strings.end())
std::shared_ptr<const i2p::i18n::Locale> GetLocale()
{
return arg;
} else {
return it->second;
}
}
std::string GetPlural (std::string arg, int n)
{
auto it = plurals.find(arg);
if (it == plurals.end())
{
return arg;
} else {
int form = plural(n);
return it->second[form];
}
return std::make_shared<i2p::i18n::Locale>(strings, plurals, [] (int n)->int { return plural(n); });
}
} // language

7
libi2pd/RouterContext.cpp

@ -29,7 +29,7 @@ namespace i2p @@ -29,7 +29,7 @@ namespace i2p
RouterContext::RouterContext ():
m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false),
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 @@ -910,11 +910,6 @@ namespace i2p
}
}
void RouterContext::SetLanguage (Lang language)
{
m_Language = language;
}
i2p::crypto::X25519Keys& RouterContext::GetStaticKeys ()
{
if (!m_StaticKeys)

6
libi2pd/RouterContext.h

@ -146,8 +146,8 @@ namespace garlic @@ -146,8 +146,8 @@ namespace garlic
void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
// i18n
Lang GetLanguage () const { return m_Language; };
void SetLanguage (Lang language);
std::shared_ptr<const i2p::i18n::Locale> GetLanguage () { return m_Language; };
void SetLanguage (const std::shared_ptr<const i2p::i18n::Locale> language) { m_Language = language; };
protected:
@ -185,7 +185,7 @@ namespace garlic @@ -185,7 +185,7 @@ namespace garlic
std::unique_ptr<i2p::crypto::NoiseSymmetricState> m_InitialNoiseState, m_CurrentNoiseState;
// i18n
Lang m_Language;
std::shared_ptr<const i2p::i18n::Locale> m_Language;
};
extern RouterContext context;

Loading…
Cancel
Save