Page 1 of 1

How to get localised string?

Posted: Thu Apr 23, 2020 6:58 am
by ravenbs
Hello,

Im stuck with how to get a localised name of an item.

local realName = game.item_prototypes[itemName].localised_name
gives something like: "item-name.sand"

But how to get the real localised string "Sand"?
Trying to find this out for hours now :(

Re: How to get localised string?

Posted: Thu Apr 23, 2020 7:26 am
by ravenbs
Im really stuck with this.

Found that there is a async method:
local stringRealName = player.request_translation(realName)
returning true.

But the answer is never comming. Any idea why? Any tipp to do this syncron?
local function entities_translated(event)
print("Translated")
end

script.on_event({defines.on_string_translated}, function(event)
entities_translated(event)
end)


Need it only for GUI to filter a list of items with a custom filter.
So no Locale/Desync issues in this spot.
Please help to get the real translated string.

Re: How to get localised string?

Posted: Thu Apr 23, 2020 7:58 am
by Pi-C
ravenbs wrote:
Thu Apr 23, 2020 6:58 am
Hello,

Im stuck with how to get a localised name of an item.

local realName = game.item_prototypes[itemName].localised_name
gives something like: "item-name.sand"

But how to get the real localised string "Sand"?
Trying to find this out for hours now :(
There is not one localised string, but an array of strings -- one for each language that has been localised. The game knows what locale you've selected and picks the correct string from that array (if a localisation exists).

If you want to print out a localized string, do something like this:

Code: Select all

game.print({realName})
If you haven't done so already, you should also read the Localisation tutorial! :-)

Re: How to get localised string?

Posted: Thu Apr 23, 2020 8:03 am
by ravenbs
I need it in a string variable. Found no function doing this.

So currently I use "local stringRealNameBool = player.request_translation(realName)" for all elements on startup and write it in "local function entities_translated(event)" in a global array. Then use this array for translation.

Really really bad - but only thing working i found so far.
Creating a own translation array, because there is no access to the already existing once.

Code: Select all

script.on_event(defines.events.on_marked_for_deconstruction, function(event)
	entities_marked_for_deconstruction(event)
end)

local function entities_translated(event)
	if not global.translatedItems then
		global.translatedItems ={}
	end
	local toTranslateString = event.localised_string[1]
	local translatedString = event.result;

	if not global.translatedItems[toTranslateString] then
		global.translatedItems[toTranslateString] = translatedString
	end		
end

script.on_event(defines.events.on_string_translated, function(event)
	entities_translated(event)
end)

Re: How to get localised string?

Posted: Thu Apr 23, 2020 9:27 am
by Klonan
ravenbs wrote:
Thu Apr 23, 2020 8:03 am
I need it in a string variable. Found no function doing this.
Why do you need it as a string?

Re: How to get localised string?

Posted: Thu Apr 23, 2020 9:59 am
by ravenbs
Filtering elements in the GUI for substring search of items.

Its a custom GUI - a list with hundrets of items.
Display the name is not a problem, because the widget ist doing the translation.
But Searching for the displayed string ist hard, because you can not read this localised string anywhere.

Re: How to get localised string?

Posted: Thu Apr 23, 2020 10:23 am
by Optera
Searching localized name is a lot more involved than searching the name.
Raiguard already made a framework doing that see https://github.com/raiguard/Factorio-Ra ... ranslation
Word of warning though. We are currently in the process of merging libraries into one community library. Expect the individual libraries to be deprecated soon™.

Re: How to get localised string?

Posted: Thu Apr 23, 2020 10:40 am
by ravenbs
Have a working solution - by building my own dictionary on startup with async call.
Then search with this:

Code: Select all

if string.find( string.lower(stringRealName), string.lower(frame.fastfiltertext.text) ) then
Only for interest, what is "more involved" then comparing the strings in this pre-build dictionary?

Interesting would be:
- Any chance to get the live translation data without building and using a dictionary?
Dont geht the reason why the mod-API is hiding those strings...
read all the threads regarding desync in multiplayer, but function still should be able to be used in GUI classes by developers knowing what they do.