@@ -26,6 +26,8 @@ namespace
2626 constexpr VFS::Path::NormalizedView test3DePath (" l10n/test3/de.yaml" );
2727 constexpr VFS::Path::NormalizedView test4RuPath (" l10n/test4/ru.yaml" );
2828 constexpr VFS::Path::NormalizedView test4EnPath (" l10n/test4/en.yaml" );
29+ constexpr VFS::Path::NormalizedView test5GmstPath (" l10n/test5/gmst.yaml" );
30+ constexpr VFS::Path::NormalizedView test5EnPath (" l10n/test5/en.yaml" );
2931
3032 VFSTestFile invalidScript (" not a script" );
3133 VFSTestFile incorrectScript (
@@ -83,6 +85,31 @@ stat_increase: "Your {stat} has increased to {value}"
8385speed: "Speed"
8486)X" );
8587
88+ VFSTestFile test5 (R"X(
89+ string: "sSimpleString"
90+ format_string: "sFormatString"
91+ inject_string: "sStringInjection"
92+ not_found: "sNoSuchString"
93+ no_gmst:
94+ pattern: "Hello world"
95+ interpreted_string:
96+ pattern: "{gmst:sFormatString} {sSimpleString} {gmst:sFormatString}"
97+ variables:
98+ - ["a", "b"]
99+ - ["b", "a"]
100+ plural_string:
101+ pattern: |-
102+ {count, plural,
103+ one{{gmst:sSimpleString}}
104+ other{{gmst:sFormatString}}
105+ }
106+ variables:
107+ - []
108+ - ["count", "count"]
109+ unnamed_string:
110+ pattern: "{gmst:sFormatString}"
111+ )X" );
112+
86113 struct LuaL10nTest : Test
87114 {
88115 std::unique_ptr<VFS::Manager> mVFS = createTestVFS({
@@ -94,6 +121,8 @@ speed: "Speed"
94121 { test3DePath, &test1De },
95122 { test4RuPath, &test4Ru },
96123 { test4EnPath, &test4En },
124+ { test5GmstPath, &test5 },
125+ { test5EnPath, &test5 },
97126 });
98127
99128 LuaUtil::ScriptsConfiguration mCfg ;
@@ -197,4 +226,52 @@ speed: "Speed"
197226 " Your Speed has increased to 100" );
198227 });
199228 }
229+
230+ TEST_F (LuaL10nTest, L10nGMST)
231+ {
232+ LuaUtil::LuaState lua{ mVFS .get (), &mCfg };
233+ lua.protectedCall ([&](LuaUtil::LuaView& view) {
234+ sol::state_view& l = view.sol ();
235+ L10n::Manager l10nManager (mVFS .get ());
236+ const std::map<std::string_view, std::string, Misc::StringUtils::CiComp> gmsts{
237+ { " sSimpleString" , " Hello it's the world" },
238+ { " sFormatString" , " You have %i %s" },
239+ { " sStringInjection" , " {a}" },
240+ };
241+ l10nManager.setGmstLoader ([&](std::string_view gmst) -> const std::string* {
242+ auto it = gmsts.find (gmst);
243+ if (it != gmsts.end ())
244+ return &it->second ;
245+ return nullptr ;
246+ });
247+
248+ internal::CaptureStdout ();
249+ l10nManager.setPreferredLocales ({ " en" });
250+ EXPECT_THAT (internal::GetCapturedStdout (), " Preferred locales: gmst en\n " );
251+
252+ l[" l10n" ] = LuaUtil::initL10nLoader (l, &l10nManager);
253+ l.safe_script (" t5 = l10n('Test5')" );
254+
255+ EXPECT_EQ (get<std::string>(l, " t5('string')" ), " Hello it's the world" );
256+ EXPECT_EQ (get<std::string>(l, " t5('format_string')" ), " You have %i %s" );
257+ EXPECT_EQ (get<std::string>(l, " t5('format_string', { i = 1, s = 'a' })" ), " You have %i %s" );
258+ EXPECT_EQ (
259+ get<std::string>(l, " t5('interpreted_string')" ), " You have {a} {b} {sSimpleString} You have {b} {a}" );
260+ EXPECT_EQ (get<std::string>(l, " t5('interpreted_string', { a = 1, b = 2, sSimpleString = 3 })" ),
261+ " You have 1 2 3 You have 2 1" );
262+ EXPECT_EQ (get<std::string>(l, " t5('inject_string')" ), " {a}" );
263+ EXPECT_EQ (get<std::string>(l, " t5('inject_string', { a = 1 })" ), " {a}" );
264+ EXPECT_EQ (get<std::string>(l, " t5('plural_string', { count = 1 })" ), " Hello it's the world" );
265+ EXPECT_EQ (get<std::string>(l, " t5('plural_string', { count = 2 })" ), " You have 2 2" );
266+ EXPECT_EQ (get<std::string>(l, " t5('unnamed_string')" ), " You have {0} {1}" );
267+ EXPECT_EQ (get<std::string>(l, " t5('unnamed_string', { ['0'] = 'a', ['1'] = 'b' })" ), " You have a b" );
268+ EXPECT_EQ (get<std::string>(l, " t5('no_gmst')" ), " Hello world" );
269+ EXPECT_EQ (get<std::string>(l, " t5('not_found')" ), " GMST:sNoSuchString" );
270+
271+ l10nManager.setPreferredLocales ({ " en" }, false );
272+ EXPECT_EQ (get<std::string>(l, " t5('string')" ), " sSimpleString" );
273+ EXPECT_EQ (get<std::string>(l, " t5('format_string')" ), " sFormatString" );
274+ EXPECT_EQ (get<std::string>(l, " t5('not_found')" ), " sNoSuchString" );
275+ });
276+ }
200277}
0 commit comments