Skip to content

Commit

Permalink
Add support for side-loading translation files (#535)
Browse files Browse the repository at this point in the history
Closes #497
  • Loading branch information
vktr committed Mar 5, 2018
1 parent 89ff713 commit 30e12a8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/picotorrent/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void Configuration::CurrentLanguageId(int languageId)
Set<int>("language_id", languageId);
}

fs::path Configuration::LanguagesPath()
{
return Get<std::string>("languages_path", (m_env->GetApplicationDataPath() / "Languages").string());
}

fs::path Configuration::DefaultSavePath()
{
return Get<std::string>("default_save_path", m_env->GetKnownFolderPath(Environment::KnownFolder::UserDownloads).string());
Expand Down
2 changes: 2 additions & 0 deletions src/picotorrent/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ namespace pt
int CurrentLanguageId();
void CurrentLanguageId(int id);

fs::path LanguagesPath();

fs::path DefaultSavePath();
void DefaultSavePath(fs::path path);

Expand Down
44 changes: 41 additions & 3 deletions src/picotorrent/translator.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#include "translator.hpp"

#include <filesystem>
#include <fstream>
#include <sstream>

#include "config.hpp"
#include "environment.hpp"
#include "picojson.hpp"
#include "utils.hpp"

namespace fs = std::experimental::filesystem::v1;
namespace pj = picojson;
using pt::Translator;

Expand Down Expand Up @@ -55,6 +61,27 @@ std::shared_ptr<Translator> Translator::Load(HINSTANCE hInstance, std::shared_pt
LoadTranslationResource,
reinterpret_cast<LONG_PTR>(&langs));

fs::path translationsDirectory = config->LanguagesPath();

for (fs::directory_entry const& entry : fs::directory_iterator(translationsDirectory))
{
if (entry.path().extension() != ".json")
{
continue;
}

std::ifstream jsonStream(entry.path(), std::ios::binary | std::ios::in);
std::stringstream json;
json << jsonStream.rdbuf();

Language lang;

if (LoadLanguageFromJson(json.str(), lang))
{
langs[lang.code] = lang;
}
}

return std::shared_ptr<Translator>(
new Translator(langs,
config->CurrentLanguageId()));
Expand All @@ -70,13 +97,24 @@ BOOL Translator::LoadTranslationResource(HMODULE hModule, LPCTSTR lpszType, LPTS
const char* buffer = reinterpret_cast<const char*>(LockResource(data));

std::string json(buffer, static_cast<size_t>(size));
Language lang;

if (LoadLanguageFromJson(json, lang))
{
langs->insert({ lang.code, lang });
}

return TRUE;
}

bool Translator::LoadLanguageFromJson(std::string const& json, Translator::Language& lang)
{
pj::value v;
std::string err = pj::parse(v, json);

if (!err.empty())
{
return TRUE;
return false;
}

pj::object obj = v.get<pj::object>();
Expand All @@ -96,7 +134,7 @@ BOOL Translator::LoadTranslationResource(HMODULE hModule, LPCTSTR lpszType, LPTS
l.translations.insert({ p.first, converted });
}

langs->insert({ langId, l });
lang = l;

return TRUE;
return true;
}
2 changes: 2 additions & 0 deletions src/picotorrent/translator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace pt
{
class Configuration;
class Environment;

class Translator
{
Expand All @@ -35,6 +36,7 @@ namespace pt
Translator(std::map<int, Language> const& languages, int selectedLanguage);

static BOOL CALLBACK LoadTranslationResource(HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam);
static bool LoadLanguageFromJson(std::string const& json, Language& lang);

int m_selectedLanguage;
std::map<int, Language> m_languages;
Expand Down

0 comments on commit 30e12a8

Please sign in to comment.