LCOV - code coverage report
Current view: top level - source/i18n/scripting - JSInterface_L10n.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 27 42 64.3 %
Date: 2023-01-19 00:18:29 Functions: 3 7 42.9 %

          Line data    Source code
       1             : /* Copyright (C) 2021 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "JSInterface_L10n.h"
      21             : 
      22             : #include "i18n/L10n.h"
      23             : #include "lib/utf8.h"
      24             : #include "ps/CLogger.h"
      25             : #include "scriptinterface/FunctionWrapper.h"
      26             : #include "scriptinterface/ScriptRequest.h"
      27             : 
      28             : namespace JSI_L10n
      29             : {
      30           0 : L10n* L10nGetter(const ScriptRequest&, JS::CallArgs&)
      31             : {
      32           0 :     if (!g_L10n.IsInitialised())
      33             :     {
      34           0 :         LOGERROR("Trying to access g_L10n when it's not initialized!");
      35           0 :         return nullptr;
      36             :     }
      37           0 :     return &g_L10n.GetSingleton();
      38             : }
      39             : 
      40           0 : std::vector<std::string> TranslateArray(const std::vector<std::string>& sourceArray)
      41             : {
      42           0 :     std::vector<std::string> translatedArray;
      43           0 :     if (g_L10n.IsInitialised())
      44           0 :         for (const std::string& elem : sourceArray)
      45           0 :             translatedArray.push_back(g_L10n.Translate(elem));
      46             : 
      47           0 :     return translatedArray;
      48             : }
      49             : 
      50             : // Return a localized version of a time given in milliseconds.
      51           0 : std::string FormatMillisecondsIntoDateStringLocal(UDate milliseconds, const std::string& formatString)
      52             : {
      53           0 :     return g_L10n.FormatMillisecondsIntoDateString(milliseconds, formatString, true);
      54             : }
      55             : 
      56             : // Return a localized version of a duration or a time in GMT given in milliseconds.
      57           0 : std::string FormatMillisecondsIntoDateStringGMT(UDate milliseconds, const std::string& formatString)
      58             : {
      59           0 :     return g_L10n.FormatMillisecondsIntoDateString(milliseconds, formatString, false);
      60             : }
      61             : 
      62          12 : void RegisterScriptFunctions(const ScriptRequest& rq)
      63             : {
      64             : #define REGISTER_L10N(name) \
      65             :     ScriptFunction::Register<&L10n::name, &L10nGetter>(rq, #name);
      66             : #define REGISTER_L10N_FUNC(func, name) \
      67             :     ScriptFunction::Register<func, &L10nGetter>(rq, name);
      68             : 
      69          12 :     REGISTER_L10N(Translate)
      70          12 :     REGISTER_L10N(TranslateWithContext)
      71          12 :     REGISTER_L10N(TranslatePlural)
      72          12 :     REGISTER_L10N(TranslatePluralWithContext)
      73          12 :     REGISTER_L10N(TranslateLines)
      74          12 :     ScriptFunction::Register<&TranslateArray>(rq, "TranslateArray");
      75          12 :     ScriptFunction::Register<&FormatMillisecondsIntoDateStringLocal>(rq, "FormatMillisecondsIntoDateStringLocal");
      76          12 :     ScriptFunction::Register<&FormatMillisecondsIntoDateStringGMT>(rq, "FormatMillisecondsIntoDateStringGMT");
      77          12 :     REGISTER_L10N(FormatDecimalNumberIntoString)
      78             : 
      79          12 :     REGISTER_L10N(GetSupportedLocaleBaseNames)
      80          12 :     REGISTER_L10N(GetSupportedLocaleDisplayNames)
      81          12 :     REGISTER_L10N_FUNC(&L10n::GetCurrentLocaleString, "GetCurrentLocale");
      82          12 :     REGISTER_L10N(GetAllLocales)
      83             :     // Select the appropriate overload.
      84          12 :     REGISTER_L10N_FUNC(static_cast<std::string(L10n::*)(const std::string&) const>(&L10n::GetDictionaryLocale), "GetDictionaryLocale");
      85          12 :     REGISTER_L10N(GetDictionariesForLocale)
      86             : 
      87          12 :     REGISTER_L10N(UseLongStrings)
      88          12 :     REGISTER_L10N(GetLocaleLanguage)
      89          12 :     REGISTER_L10N(GetLocaleBaseName)
      90          12 :     REGISTER_L10N(GetLocaleCountry)
      91          12 :     REGISTER_L10N(GetLocaleScript)
      92             :     // Select the appropriate overload.
      93          12 :     REGISTER_L10N_FUNC(static_cast<std::wstring(L10n::*)(const std::string&) const>(&L10n::GetFallbackToAvailableDictLocale), "GetFallbackToAvailableDictLocale");
      94             : 
      95             :     // Select the appropriate overloads.
      96          12 :     REGISTER_L10N_FUNC(static_cast<bool(L10n::*)(const std::string&) const>(&L10n::ValidateLocale), "ValidateLocale");
      97          12 :     REGISTER_L10N_FUNC(static_cast<bool(L10n::*)(const std::string&) const>(&L10n::SaveLocale), "SaveLocale");
      98          12 :     REGISTER_L10N(ReevaluateCurrentLocaleAndReload)
      99             : #undef REGISTER_L10N
     100             : #undef REGISTER_L10N_FUNC
     101          12 : }
     102           3 : }

Generated by: LCOV version 1.13