Page 1 of 1
Any way to programatically generate localised names?
Posted: Sun Jan 08, 2023 3:31 pm
by KoRaLLL
Hi all, I hope everyone is having a great start to the year!
I'm currently working on a project that takes both default and modded entities and manipulates them in various ways, turning some of them into other entities.
I'm running into the issue of trying to localise names programatically. Adding the names manually to the /locale/x.cfg file is not an option.
For vanilla items, I figured that I can eliminate the hyphens from the entity.name value, break this in to an array, capitalize the first word and then setting that as the entity.localised_name value.
For modded items however, this method won't work, as some mods have the entity.name variable set to something that isn't the item's name, for example aa-bb-item, so if I was to use the above method, the name of the item would become "Aa bb item", when in reality, it's actually "Big box" or something.
The next solution I tried was to edit the cfg file and add the names directly into the cfg locale file, but I don't think Factorio has any file-writing modules (for obvious reasons), so that isn't an option either.
Does anyone have any solutions or workarounds here?
Thanks as always!
Re: Any way to programatically generate localised names?
Posted: Sun Jan 08, 2023 6:01 pm
by robot256
This is the purpose of the "localised_name" and "localised_description" properties in the data phase. See the docs for some examples, and there are a lot of topics on the forum with answers to common questions.
https://wiki.factorio.com/PrototypeBase#localised_name
Re: Any way to programatically generate localised names?
Posted: Sun Jan 08, 2023 6:35 pm
by Pi-C
KoRaLLL wrote: Sun Jan 08, 2023 3:31 pm
For modded items however, this method won't work, as some mods have the entity.name variable set to something that isn't the item's name, for example aa-bb-item, so if I was to use the above method, the name of the item would become "Aa bb item", when in reality, it's actually "Big box" or something.
You could use something like this in your locale file:
Code: Select all
[entity-name]
generic-entity=__1__
and something like this in your data file:
Code: Select all
local entity = …
local get_localised_item_name = function(entity)
-- Some magic needed here!
…
return localised_name
end
entity.localised_name = {"entity-name.generic-entity", get_localised_item_name(entity)}
The problem is how to find the proper localised_name of the corresponding item -- which is easy if item.localised_name has been defined, but more complicated if it isn't. You should consult the section
Default Behavior(s) for finding an Unspecified Localised String of the localisation tutorial for building the function get_localised_item_name.
Re: Any way to programatically generate localised names?
Posted: Mon Jan 09, 2023 12:25 pm
by Bilka
Pi-C wrote: Sun Jan 08, 2023 6:35 pm
Code: Select all
local entity = …
local get_localised_item_name = function(entity)
-- Some magic needed here!
…
return localised_name
end
entity.localised_name = {"entity-name.generic-entity", get_localised_item_name(entity)}
Here is your magic:
Code: Select all
local get_localised_item_name = function(entity)
return "__ENTITY__" .. entity.name .. "__"
end
See
https://wiki.factorio.com/Tutorial:Loca ... parameters - this is what those built-in parameters are for.
Re: Any way to programatically generate localised names?
Posted: Mon Jan 09, 2023 12:58 pm
by Pi-C
Perhaps a misunderstanding? I thought KoRaLLL wanted to automatically set the name of entities to the name of the item used to placed the entity -- even if that item has a completely different name:
KoRaLLL wrote: Sun Jan 08, 2023 3:31 pm
For modded items however, this method won't work, as some mods have the entity.name variable set to something that isn't the item's name, for example aa-bb-item, so if I was to use the above method, the name of the item would become "Aa bb item", when in reality, it's actually "Big box" or something.
Re: Any way to programatically generate localised names?
Posted: Sun Oct 29, 2023 11:16 pm
by Gweneph
I'm pretty confused by this advise. AFAIK the __MAGIC__ notations only work for the localisation cfg file as well as apparently the console (but not in the way I was expecting.)
Here's a few things I tried:
In data.lua
Code: Select all
data.raw.furnace['stone-furnace'].localised_name = "Lol __ENTITY__electric-furnace__"

- Image of stone furnace with the name Lol __ENTITY__electric-furnace__
- Image 404.png (21.57 KiB) Viewed 1210 times

- Image of the factorio console, where what's typed is /c game.print('__ENTITY__electric-furnace__'), the previous line says __ENTITY__electric-furnace__ but then the first line says Gweneph (command): game.print('Electric furnace')
- Image 406.png (52.88 KiB) Viewed 1210 times
This console behavior was pretty unexpected.

- Image of the factorio console, where what's typed is /c localised_print('__ENTITY__electric-furnace__'), but the previous line says Gweneph (command): localised_print('Electric furnace')
- Image 403.png (64.36 KiB) Viewed 1210 times
This command also printed __ENTITY__electric-furnace__ to STDOUT.
So the only place that __ENTITY__electric-furnace__ seemed to get translated was when what I typed got printed to the console (which I feel like should not have gotten translated? but maybe it's functionality for chat purposes?) I'm sure it also works in the cfg files as expected.
For KoRaLLL's use case this should work though:
Code: Select all
data.raw.furnace['stone-furnace'].localised_name = {"", "Lol ",{"entity-name.electric-furnace"}}

- image of stone furnace with the name Lol Electric Furnace
- Image 405.png (15.78 KiB) Viewed 1210 times
However, I'm actually very interested in the __CONTROL__custom-event__ localization magic strings, as I'd really like to be able to print those to STDOUT from a programmatically selected custom-event. So is there a way to get get the game to localize one of these __COTNROL__ strings?