/* Copyright (C) 2018 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see .
*/
#include "precompiled.h"
#include "JSInterface_L10n.h"
#include "i18n/L10n.h"
#include "lib/utf8.h"
#include "scriptinterface/ScriptInterface.h"
// Returns a translation of the specified English string into the current language.
std::wstring JSI_L10n::Translate(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::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.
std::wstring JSI_L10n::TranslateWithContext(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& context, const std::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.
std::wstring JSI_L10n::TranslatePlural(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), 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 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::CmptPrivate* UNUSED(pCmptPrivate), 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 a translated version of the given string, localizing it line by line.
std::wstring JSI_L10n::TranslateLines(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::wstring& sourceString)
{
return wstring_from_utf8(g_L10n.TranslateLines(utf8_from_wstring(sourceString)));
}
// Return a translated version of the items in the specified array.
std::vector JSI_L10n::TranslateArray(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::vector& sourceArray)
{
std::vector translatedArray;
for (const std::wstring& elem : sourceArray)
translatedArray.push_back(wstring_from_utf8(g_L10n.Translate(utf8_from_wstring(elem))));
return translatedArray;
}
std::wstring JSI_L10n::GetFallbackToAvailableDictLocale(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& locale)
{
return g_L10n.GetFallbackToAvailableDictLocale(locale);
}
// Return a localized version of a time given in milliseconds.
std::wstring JSI_L10n::FormatMillisecondsIntoDateStringLocal(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), UDate milliseconds, const std::wstring& formatString)
{
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.
std::wstring JSI_L10n::FormatMillisecondsIntoDateStringGMT(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), UDate milliseconds, const std::wstring& formatString)
{
return wstring_from_utf8(g_L10n.FormatMillisecondsIntoDateString(milliseconds, utf8_from_wstring(formatString), false));
}
// Return a localized version of the given decimal number.
std::wstring JSI_L10n::FormatDecimalNumberIntoString(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), double number)
{
return wstring_from_utf8(g_L10n.FormatDecimalNumberIntoString(number));
}
std::vector JSI_L10n::GetSupportedLocaleBaseNames(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
{
return g_L10n.GetSupportedLocaleBaseNames();
}
std::vector JSI_L10n::GetSupportedLocaleDisplayNames(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
{
return g_L10n.GetSupportedLocaleDisplayNames();
}
std::string JSI_L10n::GetCurrentLocale(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
{
return g_L10n.GetCurrentLocaleString();
}
bool JSI_L10n::UseLongStrings(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
{
return g_L10n.UseLongStrings();
}
std::vector JSI_L10n::GetAllLocales(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
{
return g_L10n.GetAllLocales();
}
std::string JSI_L10n::GetDictionaryLocale(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& configLocale)
{
return g_L10n.GetDictionaryLocale(configLocale);
}
std::vector JSI_L10n::GetDictionariesForLocale(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& locale)
{
return g_L10n.GetDictionariesForLocale(locale);
}
std::string JSI_L10n::GetLocaleLanguage(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& locale)
{
return g_L10n.GetLocaleLanguage(locale);
}
std::string JSI_L10n::GetLocaleBaseName(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& locale)
{
return g_L10n.GetLocaleBaseName(locale);
}
std::string JSI_L10n::GetLocaleCountry(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& locale)
{
return g_L10n.GetLocaleCountry(locale);
}
std::string JSI_L10n::GetLocaleScript(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& locale)
{
return g_L10n.GetLocaleScript(locale);
}
bool JSI_L10n::ValidateLocale(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& locale)
{
return g_L10n.ValidateLocale(locale);
}
bool JSI_L10n::SaveLocale(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::string& locale)
{
return g_L10n.SaveLocale(locale);
}
void JSI_L10n::ReevaluateCurrentLocaleAndReload(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
{
g_L10n.ReevaluateCurrentLocaleAndReload();
}
void JSI_L10n::RegisterScriptFunctions(const ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction("Translate");
scriptInterface.RegisterFunction("TranslateWithContext");
scriptInterface.RegisterFunction("TranslatePlural");
scriptInterface.RegisterFunction("TranslatePluralWithContext");
scriptInterface.RegisterFunction("TranslateLines");
scriptInterface.RegisterFunction, std::vector, &TranslateArray>("TranslateArray");
scriptInterface.RegisterFunction("FormatMillisecondsIntoDateStringLocal");
scriptInterface.RegisterFunction("FormatMillisecondsIntoDateStringGMT");
scriptInterface.RegisterFunction("FormatDecimalNumberIntoString");
scriptInterface.RegisterFunction, &GetSupportedLocaleBaseNames>("GetSupportedLocaleBaseNames");
scriptInterface.RegisterFunction, &GetSupportedLocaleDisplayNames>("GetSupportedLocaleDisplayNames");
scriptInterface.RegisterFunction("GetCurrentLocale");
scriptInterface.RegisterFunction, &GetAllLocales>("GetAllLocales");
scriptInterface.RegisterFunction("GetDictionaryLocale");
scriptInterface.RegisterFunction, std::string, &GetDictionariesForLocale>("GetDictionariesForLocale");
scriptInterface.RegisterFunction("UseLongStrings");
scriptInterface.RegisterFunction("GetLocaleLanguage");
scriptInterface.RegisterFunction("GetLocaleBaseName");
scriptInterface.RegisterFunction("GetLocaleCountry");
scriptInterface.RegisterFunction("GetLocaleScript");
scriptInterface.RegisterFunction("GetFallbackToAvailableDictLocale");
scriptInterface.RegisterFunction("ValidateLocale");
scriptInterface.RegisterFunction("SaveLocale");
scriptInterface.RegisterFunction("ReevaluateCurrentLocaleAndReload");
}