Skip to content

Commit

Permalink
Localization - Better Languages Check
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Jan 27, 2024
1 parent f2d3a73 commit 51e172d
Showing 1 changed file with 44 additions and 42 deletions.
86 changes: 44 additions & 42 deletions src/localization/documentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,53 @@
#include <boost/locale.hpp>
#include "aura/aura.h"
#include "helpers/stringhelpers.h"
#include "localization/gettext.h"

namespace Nickvision::Localization
{
std::string Documentation::getHelpUrl(const std::string& pageName) noexcept
{
std::string Documentation::getHelpUrl(const std::string& pageName) noexcept
{
#ifdef __linux__
if (Aura::Aura::getEnvVar("SNAP").empty())
{
return "help:" + StringHelpers::toLower(Aura::Aura::getActive().getAppInfo().getEnglishShortName()) + "/" + pageName;
}
if (Aura::Aura::getEnvVar("SNAP").empty())
{
return "help:" + StringHelpers::toLower(Aura::Aura::getActive().getAppInfo().getEnglishShortName()) + "/" + pageName;
}
#endif
std::string lang{ "C" };
std::string sysLocale{ StringHelpers::split(boost::locale::util::get_system_locale(), ".")[0] }; //split to remove the .UTF-8
if (!sysLocale.empty() && sysLocale != "C" && sysLocale != "en_US")
{
/*
* Because the translations of a Nickvision application are generated and stored in the application's
* running directory, we can look at the list of directory names and see if they match the sysLocale.
* For example, if there is a Brazilian Portuguese translation, there will be a folder called pt_BR.
*/
std::vector<std::string> langs;
for (const std::filesystem::directory_entry& e : std::filesystem::directory_iterator(std::filesystem::current_path()))
{
if (e.is_directory())
{
langs.push_back(e.path().filename().string());
}
}
if (std::find(langs.begin(), langs.end(), sysLocale) != langs.end())
{
lang = sysLocale;
}
else
{
std::string twoLetter{ StringHelpers::split(sysLocale, "_")[0] };
for (const std::string& l : langs)
{
if (l.find(twoLetter) != std::string::npos)
{
lang = l;
break;
}
}
}
}
return "https://htmlpreview.github.io/?" + Aura::Aura::getActive().getAppInfo().getHtmlDocsStore() + "/" + lang + "/" + pageName + ".html";
}
std::string lang{ "C" };
std::string sysLocale{ StringHelpers::split(boost::locale::util::get_system_locale(), ".")[0] }; //split to remove the .UTF-8
if (!sysLocale.empty() && sysLocale != "C" && sysLocale != "en_US")
{
/*
* Because the translations of a Nickvision application are stored in the application's running
* directory, we can look at the list of directory names and see if they contain a translation
* file (.mo). If a directory contains an mo file, we can add that directory name (which is a
* language code) to the list of available translated languages for the app.
*/
std::vector<std::string> langs;
for (const std::filesystem::directory_entry& e : std::filesystem::directory_iterator(std::filesystem::current_path()))
{
if (e.is_directory() && std::filesystem::exists(e / Gettext::getDomainName() / ".mo"))
{
langs.push_back(e.path().filename().string());
}
}
if (std::find(langs.begin(), langs.end(), sysLocale) != langs.end())
{
lang = sysLocale;
}
else
{
std::string twoLetter{ StringHelpers::split(sysLocale, "_")[0] };
for (const std::string& l : langs)
{
if (l.find(twoLetter) != std::string::npos)
{
lang = l;
break;
}
}
}
}
return "https://htmlpreview.github.io/?" + Aura::Aura::getActive().getAppInfo().getHtmlDocsStore() + "/" + lang + "/" + pageName + ".html";
}
}

0 comments on commit 51e172d

Please sign in to comment.