localised name as string

Place to get help with not working mods / modding interface.
User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

localised name as string

Post by steinio »

Hello,

this bothers me for a long time and i need some help here please - i don't understand the localised string function.

I have following code:

Code: Select all

local force = game.forces.player
local current_research = force.current_research

text = "Current research: " .. tostring(current_research.localised_name)
but it only returns a table.

How can i get the localised name as a string?

Greetings steinio
Image

Transport Belt Repair Man

View unread Posts
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: localised name as string

Post by prg »

You can't get at the actual localised string from within Lua. It could differ for people in a multiplayer game using different locales, potentially leading to desyncs. You can only pass this table to LuaPlayer::print or set it as the caption for a GUI element or things like that so the game translates the text for you.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: localised name as string

Post by steinio »

Ok thank you.

Can i pass it as a construct?
Saw this in some mods:

Code: Select all

{"chest-type"}
It get transfered to a gui flow text.

If i play with this i get only a "can't concat table" error.

Greetings steinio
Image

Transport Belt Repair Man

View unread Posts
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: localised name as string

Post by prg »

You can create a localization table yourself:

Code: Select all

game.player.print({"entity-name.fast-inserter"})
and pass parameters to fill in placeholders:

Code: Select all

game.player.print({"description.durability", 10, 20})
which can themselves be localization tables:

Code: Select all

game.player.print({"inventory-restriction.cant-be-burned", {"item-name.steel-plate"}})
so you could create a locale entry for your mod containing something like "current-research=Current research: __1__" to achieve what you want.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: localised name as string

Post by aubergine18 »

In short, anywhere you can specify some text to that will go on screen, you can either pass in static text (a string literal) --* or *-- a table defining a locale key and (optionally) parameters.

The first value of the table is the locale key, which Factorio will go find in the locale files in your mod, then any additional values in the table are parameters that can appear in the locale string as shown in prg's examples above.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: localised name as string

Post by steinio »

Well sadly this all works only with .print() function.

Thank you for your help,
So i stay with current.research.name, this works but it's only the string left of the = in the locale.cfg.

Greetings steinio
Image

Transport Belt Repair Man

View unread Posts
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: localised name as string

Post by prg »

Setting the caption of a GUI element this way should work, too. If it doesn't, show the code.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: localised name as string

Post by steinio »

Had a look on other mods how they managed it.

Keywords are #locale #en #en.cfg

No modding tutorial covers this.
All localisation tutorials only mention game.print() what is not practically relevant.

SO i made a localisation file - important is the UTF8 without BOM encoding - now it works.

Greetings steinio
Image

Transport Belt Repair Man

View unread Posts
Post Reply

Return to “Modding help”