Skip to content

Commit

Permalink
Support multilang, update code
Browse files Browse the repository at this point in the history
Signed-off-by: R4SAS <r4sas@i2pmail.org>
  • Loading branch information
r4sas committed May 22, 2021
1 parent 0e68fe4 commit 80b44fc
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 28 deletions.
3 changes: 3 additions & 0 deletions contrib/i2pd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ port = 7070
# auth = true
# user = i2pd
# pass = changeme
## Select webconsole language
## Currently supported only english (default) and russian languages
# lang = english

[httpproxy]
## Uncomment and set to 'false' to disable HTTP Proxy
Expand Down
7 changes: 7 additions & 0 deletions daemon/Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ namespace util
LogPrint(eLogInfo, "Daemon: using hidden mode");
i2p::data::netdb.SetHidden(true);
}

std::string httpLang; i2p::config::GetOption("http.lang", httpLang);
if (!httpLang.compare("russian"))
i2p::context.SetLanguage (eRussian);
else
i2p::context.SetLanguage (eEnglish);

return true;
}

Expand Down
23 changes: 1 addition & 22 deletions daemon/HTTPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
#include "ECIESX25519AEADRatchetSession.h"
#include "I18N.h"

#ifdef _WIN32
#include <boost/filesystem.hpp>
#include <codecvt>
#endif

#ifdef WIN32_APP
#include "Win32App.h"
#endif
Expand Down Expand Up @@ -136,20 +131,6 @@ namespace http {
static std::string ConvertTime (uint64_t time);
std::map<uint32_t, uint32_t> HTTPConnection::m_Tokens;

std::string DataPath;

static void SetDataDir ()
{
#ifdef _WIN32
boost::filesystem::wpath path (i2p::fs::GetDataDir());
auto loc = boost::filesystem::path::imbue(std::locale( std::locale(), new std::codecvt_utf8_utf16<wchar_t>() ) );
i2p::http::DataPath = path.string();
boost::filesystem::path::imbue(loc); // Return it back
#else
i2p::http::DataPath = i2p::fs::GetDataDir();
#endif
}

static void ShowUptime (std::stringstream& s, int seconds)
{
int num;
Expand Down Expand Up @@ -331,7 +312,7 @@ namespace http {
s << "<b>Transit:</b> ";
ShowTraffic (s, i2p::transport::transports.GetTotalTransitTransmittedBytes ());
s << " (" << (double) i2p::transport::transports.GetTransitBandwidth () / 1024 << " KiB/s)<br>\r\n";
s << "<b>Data path:</b> " << i2p::http::DataPath << "<br>\r\n";
s << "<b>Data path:</b> " << i2p::fs::GetUTF8DataDir() << "<br>\r\n";
s << "<div class='slide'>";
if((outputFormat==OutputFormatEnum::forWebConsole)||!includeHiddenContent) {
s << "<label for=\"slide-info\">Hidden content. Press on text to see.</label>\r\n<input type=\"checkbox\" id=\"slide-info\" />\r\n<div class=\"slidecontent\">\r\n";
Expand Down Expand Up @@ -1390,8 +1371,6 @@ namespace http {
LogPrint(eLogInfo, "HTTPServer: password set to ", pass);
}

i2p::http::SetDataDir();

m_IsRunning = true;
m_Thread.reset (new std::thread (std::bind (&HTTPServer::Run, this)));
m_Acceptor.listen ();
Expand Down
56 changes: 56 additions & 0 deletions i18n/English.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <map>
#include <vector>
#include <string>

// Russian 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) {
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
{
{"days", {"day", "days"}},
{"hours", {"hour", "hours"}},
{"minutes", {"minute", "minutes"}},
{"seconds", {"second", "seconds"}}
};

std::string GetString (std::string arg)
{
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);
if (it == plurals.end())
{
return arg;
} else {
int form = plural(n);
return it->second[form];
}
}

} // language
} // i18n
} // i2p
21 changes: 19 additions & 2 deletions i18n/I18N.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,40 @@
#ifndef __I18N_H__
#define __I18N_H__

#include "RouterContext.h"

namespace i2p {
namespace i18n {

namespace english {
std::string GetString (std::string arg);
std::string GetPlural (std::string arg, int n);
}

namespace russian {
std::string GetString (std::string arg);
std::string GetPlural (std::string arg, int n);
}

std::string translate (std::string arg)
{
return i2p::i18n::russian::GetString (arg);
switch (i2p::context.GetLanguage ())
{
case eEnglish: return i2p::i18n::english::GetString (arg);
case eRussian: return i2p::i18n::russian::GetString (arg);
default: return arg;
}
}

template<typename inttype>
std::string translate (std::string arg, inttype&& n)
{
return i2p::i18n::russian::GetPlural (arg, (int) n);
switch (i2p::context.GetLanguage ())
{
case eEnglish: return i2p::i18n::english::GetPlural (arg, (int) n);
case eRussian: return i2p::i18n::russian::GetPlural (arg, (int) n);
default: return arg;
}
}

} // i18n
Expand Down
1 change: 1 addition & 0 deletions libi2pd/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ namespace config {
("http.strictheaders", value<bool>()->default_value(true), "Enable strict host checking on WebUI")
("http.hostname", value<std::string>()->default_value("localhost"), "Expected hostname for WebUI")
("http.webroot", value<std::string>()->default_value("/"), "WebUI root path (default: / )")
("http.lang", value<std::string>()->default_value("english"), "WebUI language (default: english )")
;

options_description httpproxy("HTTP Proxy options");
Expand Down
13 changes: 13 additions & 0 deletions libi2pd/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifdef _WIN32
#include <shlobj.h>
#include <windows.h>
#include <codecvt>
#endif

#include "Base.h"
Expand Down Expand Up @@ -41,6 +42,18 @@ namespace fs {
return dataDir;
}

const std::string GetUTF8DataDir () {
#ifdef _WIN32
boost::filesystem::wpath path (dataDir);
auto loc = boost::filesystem::path::imbue(std::locale( std::locale(), new std::codecvt_utf8_utf16<wchar_t>() ) ); // convert path to UTF-8
auto dataDirUTF8 = path.string();
boost::filesystem::path::imbue(loc); // Return locale settings back
return dataDirUTF8;
#else
return dataDir; // linux, osx, android uses UTF-8 by default
#endif
}

void DetectDataDir(const std::string & cmdline_param, bool isService) {
if (cmdline_param != "") {
dataDir = cmdline_param;
Expand Down
3 changes: 3 additions & 0 deletions libi2pd/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ namespace fs {
/** @brief Returns datadir path */
const std::string & GetDataDir();

/** @brief Returns datadir path in UTF-8 encoding */
const std::string GetUTF8DataDir();

/**
* @brief Set datadir either from cmdline option or using autodetection
* @param cmdline_param Value of cmdline parameter --datadir=<something>
Expand Down
7 changes: 6 additions & 1 deletion libi2pd/RouterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_Error (eRouterErrorNone), m_NetID (I2PD_NET_ID), m_Language (eEnglish)
{
}

Expand Down Expand Up @@ -916,6 +916,11 @@ namespace i2p
}
}

void RouterContext::SetLanguage (Lang language)
{
m_Language = language;
}

i2p::crypto::X25519Keys& RouterContext::GetStaticKeys ()
{
if (!m_StaticKeys)
Expand Down
18 changes: 15 additions & 3 deletions libi2pd/RouterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace i2p
namespace garlic
{
class RouterIncomingRatchetSession;
}
}

const char ROUTER_INFO[] = "router.info";
const char ROUTER_KEYS[] = "router.keys";
Expand All @@ -39,7 +39,7 @@ namespace garlic
eRouterStatusError = 3,
eRouterStatusUnknown = 4,
eRouterStatusProxy = 5,
eRouterStatusMesh = 6
eRouterStatusMesh = 6
};

enum RouterError
Expand All @@ -49,7 +49,12 @@ namespace garlic
eRouterErrorOffline = 2,
eRouterErrorSymmetricNAT = 3
};


enum Lang {
eEnglish = 0,
eRussian
};

class RouterContext: public i2p::garlic::GarlicDestination
{
private:
Expand Down Expand Up @@ -144,6 +149,10 @@ namespace garlic
void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);

// i18n
Lang GetLanguage () const { return m_Language; };
void SetLanguage (Lang language);

protected:

// implements GarlicDestination
Expand Down Expand Up @@ -178,6 +187,9 @@ namespace garlic
std::unique_ptr<i2p::crypto::X25519Keys> m_StaticKeys;
// for ECIESx25519
std::unique_ptr<i2p::crypto::NoiseSymmetricState> m_InitialNoiseState, m_CurrentNoiseState;

// i18n
Lang m_Language;
};

extern RouterContext context;
Expand Down

0 comments on commit 80b44fc

Please sign in to comment.