Page 1 of 1

Checking for non-existant localization

Posted: Thu Nov 25, 2021 4:33 am
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?

Re: Checking for non-existant localization

Posted: Sat Nov 27, 2021 3:29 pm
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).

Re: Checking for non-existant localization

Posted: Sat Nov 27, 2021 4:59 pm
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

Re: Checking for non-existant localization

Posted: Sun Nov 28, 2021 6:24 am
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.

Re: Checking for non-existant localization

Posted: Sun Oct 29, 2023 11:26 pm
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}}}