Skip to content

Commit ee9d7db

Browse files
committed
Merge branch 'gmstl10n' into 'master'
Allow composition of GMST l10n values See merge request OpenMW/openmw!4873
2 parents e422c09 + b424929 commit ee9d7db

File tree

8 files changed

+349
-98
lines changed

8 files changed

+349
-98
lines changed

apps/components_tests/lua/testl10n.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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}"
8385
speed: "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
}

apps/openmw/engine.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -868,21 +868,15 @@ void OMW::Engine::prepareEngine()
868868
mWorld->setRandomSeed(mRandomSeed);
869869

870870
const MWWorld::Store<ESM::GameSetting>* gmst = &mWorld->getStore().get<ESM::GameSetting>();
871-
mL10nManager->setGmstLoader(
872-
[gmst, misses = std::set<std::string, std::less<>>()](std::string_view gmstName) mutable {
873-
const ESM::GameSetting* res = gmst->search(gmstName);
874-
if (res && res->mValue.getType() == ESM::VT_String)
875-
return res->mValue.getString();
876-
else
877-
{
878-
if (misses.count(gmstName) == 0)
879-
{
880-
misses.emplace(gmstName);
881-
Log(Debug::Error) << "GMST " << gmstName << " not found";
882-
}
883-
return std::string("GMST:") + std::string(gmstName);
884-
}
885-
});
871+
mL10nManager->setGmstLoader([gmst, misses = std::set<std::string, Misc::StringUtils::CiComp>()](
872+
std::string_view gmstName) mutable -> const std::string* {
873+
const ESM::GameSetting* res = gmst->search(gmstName);
874+
if (res && res->mValue.getType() == ESM::VT_String)
875+
return &res->mValue.getString();
876+
if (misses.emplace(gmstName).second)
877+
Log(Debug::Error) << "GMST " << gmstName << " not found";
878+
return nullptr;
879+
});
886880

887881
mWindowManager->setStore(mWorld->getStore());
888882
mWindowManager->initUI();

components/l10n/manager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace L10n
2424
void dropCache() { mCache.clear(); }
2525
void setPreferredLocales(const std::vector<std::string>& locales, bool gmstHasPriority = true);
2626
const std::vector<icu::Locale>& getPreferredLocales() const { return mPreferredLocales; }
27-
void setGmstLoader(std::function<std::string(std::string_view)> fn) { mGmstLoader = std::move(fn); }
27+
void setGmstLoader(GmstLoader fn) { mGmstLoader = std::move(fn); }
2828

2929
std::shared_ptr<const MessageBundles> getContext(
3030
std::string_view contextName, const std::string& fallbackLocale = "en");
@@ -41,7 +41,7 @@ namespace L10n
4141
const VFS::Manager* mVFS;
4242
std::vector<icu::Locale> mPreferredLocales;
4343
std::map<std::tuple<std::string, std::string>, std::shared_ptr<MessageBundles>, std::less<>> mCache;
44-
std::function<std::string(std::string_view)> mGmstLoader;
44+
GmstLoader mGmstLoader;
4545
};
4646

4747
}

0 commit comments

Comments
 (0)