Community Forum

l10n text from other Mod?

Forum Overview >> Scripting

CategoryScripting
Created22.02.2019 19:33


Heady Planet-ls (Headshot XXL) 22.02.2019 19:33
Is there a way to get a l10n text from other Mod?

Text defined in moddesc in Mod 1 (text name can be different):
<l10n>
<text name="SETPLACE">
<de>Objekt setzen</de>
</text>
</l10n>



Get Text in script at Mod 2:
local text = g_i18n:getText("SETPLACE");

Bilbo Beutlin (BBeutlin) 23.02.2019 00:15
No - afaik the <l10n> is 'local' (to the mod) to avoid conflicts if defined already in other mods.

Kevin I. (Ifkonator2) 25.02.2019 16:42
Hi Heady!

Of Course you can get an Text from annother Mod! Use:

local newText = "SETPLACE";

for l10nTAG, text in pairs(g_i18n.texts) do --## in g_i18n.texts are all registered l10n texts
if string.find(newText, l10nTAG) then --## check if l10n text is the searched one
newText = text; --## here you get your translatet l10n text
end;
end;

I think, you know where you put this code in your lua file.

Greetings,

Ifko[nator]

Bilbo Beutlin (BBeutlin) 25.02.2019 19:22
Ok - but I supposed the TO has searched a 'direct' reference instead scanning the complete localization table. *g*

Kevin I. (Ifkonator2) 26.02.2019 15:46
Better as nothing. And, as is possible to create an own function for this, so you can search direct.

local function getText(searchedL10nTAG)
local newText = searchedL10nTAG;

for l10nTAG, text in pairs(g_i18n.texts) do --## in g_i18n.texts are all registered l10n texts
if string.find(newText, l10nTAG) then --## check if l10n text is the searched one
newText = text; --## here you get your translatet l10n text
end;
end;

return newText;
end;

call:

getText("SETPLACE");

Its not so much code.

Heady Planet-ls (Headshot XXL) 26.02.2019 19:17
Thank you Ifkonator2.
But no matter in which load status. g_i18n.texts has no content. I do not get any text.


for l10nTAG, text in pairs(g_i18n.texts) do
print("g_i18n.texts["..tostring(l10nTAG).."] = "..text)
end;

ModEvent = {};

function ModEvent:loadMap(name)
for l10nTAG, text in pairs(g_i18n.texts) do
print("g_i18n.texts["..tostring(l10nTAG).."] = "..text)
end;
end;

...
function MyClass:draw()
for l10nTAG, text in pairs(g_i18n.texts) do
print("g_i18n.texts["..tostring(l10nTAG).."] = "..text)
end;
end;



Kevin I. (Ifkonator2) 26.02.2019 21:06
Hi Heady,

i see your Problem... I have modifided the code, unfortunately it is a little bit dirty... But it works know for all Mods in the Mod folder.

New Function:

local function getText(searchedL10nText)
local newText = searchedL10nText;

for _, mods in pairs(g_modManager.mods) do
for modFile, filename in pairs(mods) do
if string.find(tostring(modFile), "modFile") then
local modDesc = loadXMLFile("modDesc", filename);
local l10nCount = 0;

while true do
local l10nKey = "modDesc.l10n.text(" .. tostring(l10nCount) .. ")";

if not hasXMLProperty(modDesc, l10nKey) then
break;
end;

local l10nTAG = getXMLString(modDesc, l10nKey .. "#name");
local text = getXMLString(modDesc, l10nKey .. "." .. g_languageShort);

if string.find(newText, l10nTAG) then
newText = text;
end;

l10nCount = l10nCount + 1;
end;
end;
end;
end;

return newText;
end;

usage:

local text = getText("SETPLACE");

print("text = " .. text);

Bilbo Beutlin (BBeutlin) 27.02.2019 06:59
Another idea:
If you know the mod name (resp. directory), you could use "XMLUtil.getXMLI18NValue()".
Unluckily not documented (afaik) but see examples in LUADOC Vehicles -> Vehicle

Kevin I. (Ifkonator2) 27.02.2019 15:56
This Function could work, but you need anyway to load the modDesc.xml, to get access to it, as i do with "loadXMLFile()". Otherwise you can read nothing out of it. And with my Method, it dosen't matter whats the mod name is, because my function has it already in the variable "filename" with the full Mod directory.

There are, unfortunately, enough users who rename mods, that would be a problem if you hardcode the mod name in the lua..

Greetings,

Ifko[nator]

Bilbo Beutlin (BBeutlin) 27.02.2019 17:22
Yes - but one could use this function with pre-defined name as 'first shot'. If it gives no result, one has to scan the mod list like in your example. This funcion has the advantage that one doesn't need to scan the <l10n> table.


Note: Log in to post. Create a new account here.