Changeset View
Changeset View
Standalone View
Standalone View
source/i18n/scripting/JSInterface_L10n.cpp
Show All 16 Lines | |||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "JSInterface_L10n.h" | #include "JSInterface_L10n.h" | ||||
#include "i18n/L10n.h" | #include "i18n/L10n.h" | ||||
#include "lib/utf8.h" | #include "lib/utf8.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
// Returns a translation of the specified English string into the current language. | // Returns a translation of the specified English string into the current language. | ||||
std::wstring JSI_L10n::Translate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& sourceString) | std::wstring JSI_L10n::Translate(const std::wstring& sourceString) | ||||
{ | { | ||||
return wstring_from_utf8(g_L10n.Translate(utf8_from_wstring(sourceString))); | return wstring_from_utf8(g_L10n.Translate(utf8_from_wstring(sourceString))); | ||||
} | } | ||||
// Returns a translation of the specified English string, for the specified context. | // Returns a translation of the specified English string, for the specified context. | ||||
std::wstring JSI_L10n::TranslateWithContext(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& context, const std::wstring& sourceString) | std::wstring JSI_L10n::TranslateWithContext(const std::string& context, const std::wstring& sourceString) | ||||
{ | { | ||||
return wstring_from_utf8(g_L10n.TranslateWithContext(context, utf8_from_wstring(sourceString))); | return wstring_from_utf8(g_L10n.TranslateWithContext(context, utf8_from_wstring(sourceString))); | ||||
} | } | ||||
// Return a translated version of the given strings (singular and plural) depending on an integer value. | // Return a translated version of the given strings (singular and plural) depending on an integer value. | ||||
std::wstring JSI_L10n::TranslatePlural(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& singularSourceString, const std::wstring& pluralSourceString, int number) | std::wstring JSI_L10n::TranslatePlural(const std::wstring& singularSourceString, const std::wstring& pluralSourceString, int number) | ||||
{ | { | ||||
return wstring_from_utf8(g_L10n.TranslatePlural(utf8_from_wstring(singularSourceString), utf8_from_wstring(pluralSourceString), number)); | return wstring_from_utf8(g_L10n.TranslatePlural(utf8_from_wstring(singularSourceString), utf8_from_wstring(pluralSourceString), number)); | ||||
} | } | ||||
// Return a translated version of the given strings (singular and plural) depending on an integer value, for the specified context. | // Return a translated version of the given strings (singular and plural) depending on an integer value, for the specified context. | ||||
std::wstring JSI_L10n::TranslatePluralWithContext(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& context, const std::wstring& singularSourceString, const std::wstring& pluralSourceString, int number) | std::wstring JSI_L10n::TranslatePluralWithContext(const std::string& context, const std::wstring& singularSourceString, const std::wstring& pluralSourceString, int number) | ||||
{ | { | ||||
return wstring_from_utf8(g_L10n.TranslatePluralWithContext(context, utf8_from_wstring(singularSourceString), utf8_from_wstring(pluralSourceString), number)); | return wstring_from_utf8(g_L10n.TranslatePluralWithContext(context, utf8_from_wstring(singularSourceString), utf8_from_wstring(pluralSourceString), number)); | ||||
} | } | ||||
// Return a translated version of the given string, localizing it line by line. | // Return a translated version of the given string, localizing it line by line. | ||||
std::wstring JSI_L10n::TranslateLines(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& sourceString) | std::wstring JSI_L10n::TranslateLines(const std::wstring& sourceString) | ||||
{ | { | ||||
return wstring_from_utf8(g_L10n.TranslateLines(utf8_from_wstring(sourceString))); | return wstring_from_utf8(g_L10n.TranslateLines(utf8_from_wstring(sourceString))); | ||||
} | } | ||||
// Return a translated version of the items in the specified array. | // Return a translated version of the items in the specified array. | ||||
std::vector<std::wstring> JSI_L10n::TranslateArray(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::vector<std::wstring>& sourceArray) | std::vector<std::wstring> JSI_L10n::TranslateArray(const std::vector<std::wstring>& sourceArray) | ||||
{ | { | ||||
std::vector<std::wstring> translatedArray; | std::vector<std::wstring> translatedArray; | ||||
for (const std::wstring& elem : sourceArray) | for (const std::wstring& elem : sourceArray) | ||||
translatedArray.push_back(wstring_from_utf8(g_L10n.Translate(utf8_from_wstring(elem)))); | translatedArray.push_back(wstring_from_utf8(g_L10n.Translate(utf8_from_wstring(elem)))); | ||||
return translatedArray; | return translatedArray; | ||||
} | } | ||||
std::wstring JSI_L10n::GetFallbackToAvailableDictLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& locale) | std::wstring JSI_L10n::GetFallbackToAvailableDictLocale(const std::string& locale) | ||||
{ | { | ||||
return g_L10n.GetFallbackToAvailableDictLocale(locale); | return g_L10n.GetFallbackToAvailableDictLocale(locale); | ||||
} | } | ||||
// Return a localized version of a time given in milliseconds. | // Return a localized version of a time given in milliseconds. | ||||
std::wstring JSI_L10n::FormatMillisecondsIntoDateStringLocal(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), UDate milliseconds, const std::wstring& formatString) | std::wstring JSI_L10n::FormatMillisecondsIntoDateStringLocal(UDate milliseconds, const std::wstring& formatString) | ||||
{ | { | ||||
return wstring_from_utf8(g_L10n.FormatMillisecondsIntoDateString(milliseconds, utf8_from_wstring(formatString), true)); | return wstring_from_utf8(g_L10n.FormatMillisecondsIntoDateString(milliseconds, utf8_from_wstring(formatString), true)); | ||||
} | } | ||||
// Return a localized version of a duration or a time in GMT given in milliseconds. | // Return a localized version of a duration or a time in GMT given in milliseconds. | ||||
std::wstring JSI_L10n::FormatMillisecondsIntoDateStringGMT(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), UDate milliseconds, const std::wstring& formatString) | std::wstring JSI_L10n::FormatMillisecondsIntoDateStringGMT(UDate milliseconds, const std::wstring& formatString) | ||||
{ | { | ||||
return wstring_from_utf8(g_L10n.FormatMillisecondsIntoDateString(milliseconds, utf8_from_wstring(formatString), false)); | return wstring_from_utf8(g_L10n.FormatMillisecondsIntoDateString(milliseconds, utf8_from_wstring(formatString), false)); | ||||
} | } | ||||
// Return a localized version of the given decimal number. | // Return a localized version of the given decimal number. | ||||
std::wstring JSI_L10n::FormatDecimalNumberIntoString(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), double number) | std::wstring JSI_L10n::FormatDecimalNumberIntoString(double number) | ||||
{ | { | ||||
return wstring_from_utf8(g_L10n.FormatDecimalNumberIntoString(number)); | return wstring_from_utf8(g_L10n.FormatDecimalNumberIntoString(number)); | ||||
} | } | ||||
std::vector<std::string> JSI_L10n::GetSupportedLocaleBaseNames(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::vector<std::string> JSI_L10n::GetSupportedLocaleBaseNames() | ||||
{ | { | ||||
return g_L10n.GetSupportedLocaleBaseNames(); | return g_L10n.GetSupportedLocaleBaseNames(); | ||||
} | } | ||||
std::vector<std::wstring> JSI_L10n::GetSupportedLocaleDisplayNames(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::vector<std::wstring> JSI_L10n::GetSupportedLocaleDisplayNames() | ||||
{ | { | ||||
return g_L10n.GetSupportedLocaleDisplayNames(); | return g_L10n.GetSupportedLocaleDisplayNames(); | ||||
} | } | ||||
std::string JSI_L10n::GetCurrentLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::string JSI_L10n::GetCurrentLocale() | ||||
{ | { | ||||
return g_L10n.GetCurrentLocaleString(); | return g_L10n.GetCurrentLocaleString(); | ||||
} | } | ||||
bool JSI_L10n::UseLongStrings(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | bool JSI_L10n::UseLongStrings() | ||||
{ | { | ||||
return g_L10n.UseLongStrings(); | return g_L10n.UseLongStrings(); | ||||
} | } | ||||
std::vector<std::string> JSI_L10n::GetAllLocales(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::vector<std::string> JSI_L10n::GetAllLocales() | ||||
{ | { | ||||
return g_L10n.GetAllLocales(); | return g_L10n.GetAllLocales(); | ||||
} | } | ||||
std::string JSI_L10n::GetDictionaryLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& configLocale) | std::string JSI_L10n::GetDictionaryLocale(const std::string& configLocale) | ||||
{ | { | ||||
return g_L10n.GetDictionaryLocale(configLocale); | return g_L10n.GetDictionaryLocale(configLocale); | ||||
} | } | ||||
std::vector<std::wstring> JSI_L10n::GetDictionariesForLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& locale) | std::vector<std::wstring> JSI_L10n::GetDictionariesForLocale(const std::string& locale) | ||||
{ | { | ||||
return g_L10n.GetDictionariesForLocale(locale); | return g_L10n.GetDictionariesForLocale(locale); | ||||
} | } | ||||
std::string JSI_L10n::GetLocaleLanguage(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& locale) | std::string JSI_L10n::GetLocaleLanguage(const std::string& locale) | ||||
{ | { | ||||
return g_L10n.GetLocaleLanguage(locale); | return g_L10n.GetLocaleLanguage(locale); | ||||
} | } | ||||
std::string JSI_L10n::GetLocaleBaseName(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& locale) | std::string JSI_L10n::GetLocaleBaseName(const std::string& locale) | ||||
{ | { | ||||
return g_L10n.GetLocaleBaseName(locale); | return g_L10n.GetLocaleBaseName(locale); | ||||
} | } | ||||
std::string JSI_L10n::GetLocaleCountry(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& locale) | std::string JSI_L10n::GetLocaleCountry(const std::string& locale) | ||||
{ | { | ||||
return g_L10n.GetLocaleCountry(locale); | return g_L10n.GetLocaleCountry(locale); | ||||
} | } | ||||
std::string JSI_L10n::GetLocaleScript(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& locale) | std::string JSI_L10n::GetLocaleScript(const std::string& locale) | ||||
{ | { | ||||
return g_L10n.GetLocaleScript(locale); | return g_L10n.GetLocaleScript(locale); | ||||
} | } | ||||
bool JSI_L10n::ValidateLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& locale) | bool JSI_L10n::ValidateLocale(const std::string& locale) | ||||
{ | { | ||||
return g_L10n.ValidateLocale(locale); | return g_L10n.ValidateLocale(locale); | ||||
} | } | ||||
bool JSI_L10n::SaveLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& locale) | bool JSI_L10n::SaveLocale(const std::string& locale) | ||||
{ | { | ||||
return g_L10n.SaveLocale(locale); | return g_L10n.SaveLocale(locale); | ||||
} | } | ||||
void JSI_L10n::ReevaluateCurrentLocaleAndReload(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_L10n::ReevaluateCurrentLocaleAndReload() | ||||
{ | { | ||||
g_L10n.ReevaluateCurrentLocaleAndReload(); | g_L10n.ReevaluateCurrentLocaleAndReload(); | ||||
} | } | ||||
void JSI_L10n::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_L10n::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<std::wstring, std::wstring, &Translate>("Translate"); | #define Register(name) ScriptWrapper::WrapAndRegister<decltype(&name), &name>(scriptInterface, #name) | ||||
scriptInterface.RegisterFunction<std::wstring, std::string, std::wstring, &TranslateWithContext>("TranslateWithContext"); | Register(Translate); | ||||
scriptInterface.RegisterFunction<std::wstring, std::wstring, std::wstring, int, &TranslatePlural>("TranslatePlural"); | Register(TranslateWithContext); | ||||
scriptInterface.RegisterFunction<std::wstring, std::string, std::wstring, std::wstring, int, &TranslatePluralWithContext>("TranslatePluralWithContext"); | Register(TranslatePlural); | ||||
scriptInterface.RegisterFunction<std::wstring, std::wstring, &TranslateLines>("TranslateLines"); | Register(TranslatePluralWithContext); | ||||
scriptInterface.RegisterFunction<std::vector<std::wstring>, std::vector<std::wstring>, &TranslateArray>("TranslateArray"); | Register(TranslateLines); | ||||
scriptInterface.RegisterFunction<std::wstring, UDate, std::wstring, &FormatMillisecondsIntoDateStringLocal>("FormatMillisecondsIntoDateStringLocal"); | Register(TranslateArray); | ||||
scriptInterface.RegisterFunction<std::wstring, UDate, std::wstring, &FormatMillisecondsIntoDateStringGMT>("FormatMillisecondsIntoDateStringGMT"); | Register(FormatMillisecondsIntoDateStringLocal); | ||||
scriptInterface.RegisterFunction<std::wstring, double, &FormatDecimalNumberIntoString>("FormatDecimalNumberIntoString"); | Register(FormatMillisecondsIntoDateStringGMT); | ||||
Register(FormatDecimalNumberIntoString); | |||||
Stan: Likewise | |||||
scriptInterface.RegisterFunction<std::vector<std::string>, &GetSupportedLocaleBaseNames>("GetSupportedLocaleBaseNames"); | |||||
scriptInterface.RegisterFunction<std::vector<std::wstring>, &GetSupportedLocaleDisplayNames>("GetSupportedLocaleDisplayNames"); | Register(GetSupportedLocaleBaseNames); | ||||
scriptInterface.RegisterFunction<std::string, &GetCurrentLocale>("GetCurrentLocale"); | Register(GetSupportedLocaleDisplayNames); | ||||
scriptInterface.RegisterFunction<std::vector<std::string>, &GetAllLocales>("GetAllLocales"); | Register(GetCurrentLocale); | ||||
scriptInterface.RegisterFunction<std::string, std::string, &GetDictionaryLocale>("GetDictionaryLocale"); | Register(GetAllLocales); | ||||
scriptInterface.RegisterFunction<std::vector<std::wstring>, std::string, &GetDictionariesForLocale>("GetDictionariesForLocale"); | Register(GetDictionaryLocale); | ||||
Register(GetDictionariesForLocale); | |||||
scriptInterface.RegisterFunction<bool, &UseLongStrings>("UseLongStrings"); | |||||
scriptInterface.RegisterFunction<std::string, std::string, &GetLocaleLanguage>("GetLocaleLanguage"); | Register(GetLocaleLanguage); | ||||
scriptInterface.RegisterFunction<std::string, std::string, &GetLocaleBaseName>("GetLocaleBaseName"); | Register(GetLocaleBaseName); | ||||
scriptInterface.RegisterFunction<std::string, std::string, &GetLocaleCountry>("GetLocaleCountry"); | Register(GetLocaleCountry); | ||||
scriptInterface.RegisterFunction<std::string, std::string, &GetLocaleScript>("GetLocaleScript"); | Register(GetLocaleScript); | ||||
scriptInterface.RegisterFunction<std::wstring, std::string, &GetFallbackToAvailableDictLocale>("GetFallbackToAvailableDictLocale"); | Register(GetFallbackToAvailableDictLocale); | ||||
scriptInterface.RegisterFunction<bool, std::string, &ValidateLocale>("ValidateLocale"); | Register(ValidateLocale); | ||||
scriptInterface.RegisterFunction<bool, std::string, &SaveLocale>("SaveLocale"); | Register(SaveLocale); | ||||
scriptInterface.RegisterFunction<void, &ReevaluateCurrentLocaleAndReload>("ReevaluateCurrentLocaleAndReload"); | Register(ReevaluateCurrentLocaleAndReload); | ||||
#undef Register | |||||
} | } |
Wildfire Games · Phabricator
Likewise