Checking for non-existant localization

Place to get help with not working mods / modding interface.
Post Reply
FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2534
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Checking for non-existant localization

Post by FuryoftheStars »

Ok, so I have an item that is procedurally generated. As such, I'm also procedurally generating the name for it.

Code: Select all

localised_name = {"", {"item-name.steam-loco-cartridge"}, ": ", {"item-name."..name}},
The problem here is that, apparently, not all the items I'm catching actually have an entry under [item-name] in the localization files and instead are using their [entity-name] entries. This results in game my item description showing the Unknown name string in these cases.

How can I check for {"item-name."..name} being non-existent and thus switch to trying {"entity-name."..name} instead?
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2534
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Checking for non-existant localization

Post by FuryoftheStars »

No one, eh? I mean, I already know neither of these work:

Code: Select all

localised_name = {"", {"item-name.steam-loco-cartridge"}, ": ", {"item-name."..name or "entity-name."..name}}
localised_name = {"", {"item-name.steam-loco-cartridge"}, ": ", {"item-name."..name} or {"entity-name."..name}}
And I'm assuming the localization doesn't even get evaluated until after the data stages. So, what can I do? Aside from manually inputting names into my own localization file, which means I need to know about them (mods).
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Checking for non-existant localization

Post by DaveMcW »

A workaround is to prefer the entity name if it exists.

Code: Select all

if item.localised_name then
  localised_name = {"", {"item-name.steam-loco-cartridge"}, ": ", localised_name}
elseif item.place_result then
  localised_name = {"", {"item-name.steam-loco-cartridge"}, ": ", {"entity-name."..item.place_result}}
else
  localised_name = {"", {"item-name.steam-loco-cartridge"}, ": ", {"item-name."..name}}
end

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2534
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Checking for non-existant localization

Post by FuryoftheStars »

Hmm, ok. I have noticed some that have item names but not entity, but those most likely don't have place results. Thanks.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

Gweneph
Inserter
Inserter
Posts: 25
Joined: Thu Nov 14, 2019 11:51 pm
Contact:

Re: Checking for non-existant localization

Post by Gweneph »

Old post, but it still shows up in the google search results and there's now a solution:
https://lua-api.factorio.com/latest/concepts.html#LocalisedString wrote:if the key is a question mark ("?"), then the first valid parameter will be used. A parameter can be invalid if its name doesn't match any string template. If no parameters are valid, the last one is returned. This is useful to implement a fallback for missing locale templates.
So you can use:

Code: Select all

localised_name = {"", {"item-name.steam-loco-cartridge"}, ": ", {"?",{"item-name."..name}, {"entity-name."..name}}}

Post Reply

Return to “Modding help”