How to get localised string?

Place to get help with not working mods / modding interface.
Post Reply
ravenbs
Inserter
Inserter
Posts: 49
Joined: Wed Jun 13, 2018 11:07 am
Contact:

How to get localised string?

Post 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 :(

ravenbs
Inserter
Inserter
Posts: 49
Joined: Wed Jun 13, 2018 11:07 am
Contact:

Re: How to get localised string?

Post 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.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1638
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How to get localised string?

Post 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! :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

ravenbs
Inserter
Inserter
Posts: 49
Joined: Wed Jun 13, 2018 11:07 am
Contact:

Re: How to get localised string?

Post 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)

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5147
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: How to get localised string?

Post 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?

ravenbs
Inserter
Inserter
Posts: 49
Joined: Wed Jun 13, 2018 11:07 am
Contact:

Re: How to get localised string?

Post 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.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2915
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: How to get localised string?

Post 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™.

ravenbs
Inserter
Inserter
Posts: 49
Joined: Wed Jun 13, 2018 11:07 am
Contact:

Re: How to get localised string?

Post 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.

Post Reply

Return to “Modding help”