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: 25 41 61.0 %
Date: 2026-12-03 01:19:39 Functions: 1 5 20.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 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 "ps/CLogger.h"
      24             : #include "scriptinterface/FunctionWrapper.h"
      25             : 
      26             : #include <unicode/utypes.h>
      27             : #include <string>
      28             : #include <vector>
      29             : 
      30             : namespace JS { class CallArgs; }
      31             : 
      32             : namespace JSI_L10n
      33             : {
      34           0 : L10n* L10nGetter(const Script::Request&, JS::CallArgs&)
      35             : {
      36           0 :     if (!g_L10n.IsInitialised())
      37             :     {
      38           0 :         LOGERROR("Trying to access g_L10n when it's not initialized!");
      39           0 :         return nullptr;
      40             :     }
      41           0 :     return &g_L10n.GetSingleton();
      42             : }
      43             : 
      44           0 : std::vector<std::string> TranslateArray(const std::vector<std::string>& sourceArray)
      45             : {
      46           0 :     std::vector<std::string> translatedArray;
      47           0 :     if (g_L10n.IsInitialised())
      48           0 :         for (const std::string& elem : sourceArray)
      49           0 :             translatedArray.push_back(g_L10n.Translate(elem));
      50             : 
      51           0 :     return translatedArray;
      52           0 : }
      53             : 
      54             : // Return a localized version of a time given in milliseconds.
      55           0 : std::string FormatMillisecondsIntoDateStringLocal(UDate milliseconds, const std::string& formatString)
      56             : {
      57           0 :     return g_L10n.FormatMillisecondsIntoDateString(milliseconds, formatString, true);
      58             : }
      59             : 
      60             : // Return a localized version of a duration or a time in GMT given in milliseconds.
      61           0 : std::string FormatMillisecondsIntoDateStringGMT(UDate milliseconds, const std::string& formatString)
      62             : {
      63           0 :     return g_L10n.FormatMillisecondsIntoDateString(milliseconds, formatString, false);
      64             : }
      65             : 
      66          26 : void RegisterScriptFunctions(const Script::Request& rq)
      67             : {
      68             : #define REGISTER_L10N(name) \
      69             :     Script::Function::Register<&L10n::name, &L10nGetter>(rq, #name);
      70             : #define REGISTER_L10N_FUNC(func, name) \
      71             :     Script::Function::Register<func, &L10nGetter>(rq, name);
      72             : 
      73          26 :     REGISTER_L10N(Translate)
      74          26 :     REGISTER_L10N(TranslateWithContext)
      75          26 :     REGISTER_L10N(TranslatePlural)
      76          26 :     REGISTER_L10N(TranslatePluralWithContext)
      77          26 :     REGISTER_L10N(TranslateLines)
      78          26 :     Script::Function::Register<&TranslateArray>(rq, "TranslateArray");
      79          26 :     Script::Function::Register<&FormatMillisecondsIntoDateStringLocal>(rq, "FormatMillisecondsIntoDateStringLocal");
      80          26 :     Script::Function::Register<&FormatMillisecondsIntoDateStringGMT>(rq, "FormatMillisecondsIntoDateStringGMT");
      81          26 :     REGISTER_L10N(FormatDecimalNumberIntoString)
      82             : 
      83          26 :     REGISTER_L10N(GetSupportedLocaleBaseNames)
      84          26 :     REGISTER_L10N(GetSupportedLocaleDisplayNames)
      85          26 :     REGISTER_L10N_FUNC(&L10n::GetCurrentLocaleString, "GetCurrentLocale");
      86          26 :     REGISTER_L10N(GetAllLocales)
      87             :     // Select the appropriate overload.
      88          26 :     REGISTER_L10N_FUNC(static_cast<std::string(L10n::*)(const std::string&) const>(&L10n::GetDictionaryLocale), "GetDictionaryLocale");
      89          26 :     REGISTER_L10N(GetDictionariesForLocale)
      90             : 
      91          26 :     REGISTER_L10N(GetLocaleLanguage)
      92          26 :     REGISTER_L10N(GetLocaleBaseName)
      93          26 :     REGISTER_L10N(GetLocaleCountry)
      94          26 :     REGISTER_L10N(GetLocaleScript)
      95             :     // Select the appropriate overload.
      96          26 :     REGISTER_L10N_FUNC(static_cast<std::wstring(L10n::*)(const std::string&) const>(&L10n::GetFallbackToAvailableDictLocale), "GetFallbackToAvailableDictLocale");
      97             : 
      98             :     // Select the appropriate overloads.
      99          26 :     REGISTER_L10N_FUNC(static_cast<bool(L10n::*)(const std::string&) const>(&L10n::ValidateLocale), "ValidateLocale");
     100          26 :     REGISTER_L10N_FUNC(static_cast<bool(L10n::*)(const std::string&) const>(&L10n::SaveLocale), "SaveLocale");
     101          26 :     REGISTER_L10N(ReevaluateCurrentLocaleAndReload)
     102             : #undef REGISTER_L10N
     103             : #undef REGISTER_L10N_FUNC
     104          26 : }
     105             : }

Generated by: LCOV version 1.16