Problems getting the Localized Names of Items

Place to get help with not working mods / modding interface.
User avatar
picklock
Filter Inserter
Filter Inserter
Posts: 259
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Problems getting the Localized Names of Items

Post by picklock »

While working on one of my mods, I'm having trouble understanding something.

In the mod, I display a list of items in a GUI. The localized names of the items are shown as their names. I determine the localized names using the following code, based on the items’ internal names contained in a table generated by the mod:

Code: Select all

local myLocalName = prototypes.item[myTable[i].name].localised_name
I add the localized names of the items to the list in the GUI using the following code:

Code: Select all

my_gui_table.add{type = 'label', caption = myLocalName, style = 'bold_label'}
That works fine.

Now, as part of the updates for Factorio version 2.1, I want to add a sorting function to the list of items in the mod. So I took the variable described above, which contains the supposed localized names of the items, and wanted to add the localized names to the table so I could sort by them. However, the localized names aren’t being added to the table. When I try to print the contents of the variable to the console using the following code, it outputs “table.”

Code: Select all

myPlayer.print(tostring(myLocalName))
Now I’m a bit confused, because the localized name is displayed correctly in the GUI list, but I apparently can’t insert it into a table. What am I doing wrong? Or how can I use the internal name to transfer the localized name to a variable or add it to a table?
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
Bilka
Factorio Staff
Factorio Staff
Posts: 3871
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Problems getting the Localized Names of Items

Post by Bilka »

The localised_name in this case is a LocalisedString so something like {"item-name.iron-plate"}. If you try to sort by that, it will sort by exactly that, not the string that's displayed in the GUI (like Iron plate in English or Eisenplatte in German). If you want to sort by the translated result you need to use LuaPlayer::request_translations to get the translations and then use those for sorting.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
picklock
Filter Inserter
Filter Inserter
Posts: 259
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: Problems getting the Localized Names of Items

Post by picklock »

Hi Bilka,

Thank you for the quick response.
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
Post Reply

Return to “Modding help”