Changeset View
Changeset View
Standalone View
Standalone View
source/i18n/L10n.cpp
/* Copyright (C) 2018 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* | * | ||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||
* "Software"), to deal in the Software without restriction, including | * "Software"), to deal in the Software without restriction, including | ||||
* without limitation the rights to use, copy, modify, merge, publish, | * without limitation the rights to use, copy, modify, merge, publish, | ||||
* distribute, sublicense, and/or sell copies of the Software, and to | * distribute, sublicense, and/or sell copies of the Software, and to | ||||
* permit persons to whom the Software is furnished to do so, subject to | * permit persons to whom the Software is furnished to do so, subject to | ||||
* the following conditions: | * the following conditions: | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
std::vector<std::wstring> L10n::GetDictionariesForLocale(const std::string& locale) const | std::vector<std::wstring> L10n::GetDictionariesForLocale(const std::string& locale) const | ||||
{ | { | ||||
std::vector<std::wstring> ret; | std::vector<std::wstring> ret; | ||||
VfsPaths filenames; | VfsPaths filenames; | ||||
std::wstring dictName = GetFallbackToAvailableDictLocale(icu::Locale::createCanonical(locale.c_str())); | std::wstring dictName = GetFallbackToAvailableDictLocale(icu::Locale::createCanonical(locale.c_str())); | ||||
vfs::GetPathnames(g_VFS, L"l10n/", dictName.append(L".*.po").c_str(), filenames); | |||||
// Iterate consistently and platform-independently | |||||
vfs::GetPathnames(g_VFS, L"l10n/", dictName.append(L".*.po").c_str(), filenames, true); | |||||
for (const VfsPath& path : filenames) | for (const VfsPath& path : filenames) | ||||
ret.push_back(path.Filename().string()); | ret.push_back(path.Filename().string()); | ||||
return ret; | return ret; | ||||
} | } | ||||
std::wstring L10n::GetFallbackToAvailableDictLocale(const std::string& locale) const | std::wstring L10n::GetFallbackToAvailableDictLocale(const std::string& locale) const | ||||
▲ Show 20 Lines • Show All 338 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
delete dictionary; | delete dictionary; | ||||
dictionary = new tinygettext::Dictionary(); | dictionary = new tinygettext::Dictionary(); | ||||
VfsPaths filenames; | VfsPaths filenames; | ||||
if (useLongStrings) | if (useLongStrings) | ||||
{ | { | ||||
if (vfs::GetPathnames(g_VFS, L"l10n/", L"long.*.po", filenames) < 0) | if (vfs::GetPathnames(g_VFS, L"l10n/", L"long.*.po", filenames, false) < 0) | ||||
return; | return; | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
std::wstring dictName = GetFallbackToAvailableDictLocale(currentLocale); | std::wstring dictName = GetFallbackToAvailableDictLocale(currentLocale); | ||||
if (vfs::GetPathnames(g_VFS, L"l10n/", dictName.append(L".*.po").c_str(), filenames) < 0) | if (vfs::GetPathnames(g_VFS, L"l10n/", dictName.append(L".*.po").c_str(), filenames, false) < 0) | ||||
{ | { | ||||
LOGERROR("No files for the dictionary found, but at this point the input should already be validated!"); | LOGERROR("No files for the dictionary found, but at this point the input should already be validated!"); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
for (const VfsPath& path : filenames) | for (const VfsPath& path : filenames) | ||||
{ | { | ||||
CVFSFile file; | CVFSFile file; | ||||
file.Load(g_VFS, path); | file.Load(g_VFS, path); | ||||
std::string content = file.DecodeUTF8(); | std::string content = file.DecodeUTF8(); | ||||
ReadPoIntoDictionary(content, dictionary); | ReadPoIntoDictionary(content, dictionary); | ||||
} | } | ||||
} | } | ||||
void L10n::LoadListOfAvailableLocales() | void L10n::LoadListOfAvailableLocales() | ||||
{ | { | ||||
for (icu::Locale* const& locale : availableLocales) | for (icu::Locale* const& locale : availableLocales) | ||||
delete locale; | delete locale; | ||||
availableLocales.clear(); | availableLocales.clear(); | ||||
icu::Locale* defaultLocale = new icu::Locale(icu::Locale::getUS()); | icu::Locale* defaultLocale = new icu::Locale(icu::Locale::getUS()); | ||||
availableLocales.push_back(defaultLocale); // Always available. | availableLocales.push_back(defaultLocale); // Always available. | ||||
// Sort filenames, so as to consistently and platform-independently process the data, | |||||
// including but not limited to passing it to the GUI. | |||||
VfsPaths filenames; | VfsPaths filenames; | ||||
if (vfs::GetPathnames(g_VFS, L"l10n/", L"*.po", filenames) < 0) | if (vfs::GetPathnames(g_VFS, L"l10n/", L"*.po", filenames, true) < 0) | ||||
return; | return; | ||||
for (const VfsPath& path : filenames) | for (const VfsPath& path : filenames) | ||||
{ | { | ||||
// Note: PO files follow this naming convention: “l10n/<locale code>.<mod name>.po”. For example: “l10n/gl.public.po”. | // Note: PO files follow this naming convention: “l10n/<locale code>.<mod name>.po”. For example: “l10n/gl.public.po”. | ||||
std::string filename = utf8_from_wstring(path.string()).substr(strlen("l10n/")); | std::string filename = utf8_from_wstring(path.string()).substr(strlen("l10n/")); | ||||
size_t lengthToFirstDot = filename.find('.'); | size_t lengthToFirstDot = filename.find('.'); | ||||
std::string localeCode = filename.substr(0, lengthToFirstDot); | std::string localeCode = filename.substr(0, lengthToFirstDot); | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator