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!
Any way to programatically generate localised names?
Re: Any way to programatically generate localised names?
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
My mods: Multiple Unit Train Control, Smart Artillery Wagons
Maintainer of Vehicle Wagon 2, Cargo Ships, Honk
Maintainer of Vehicle Wagon 2, Cargo Ships, Honk
Re: Any way to programatically generate localised names?
You could use something like this in your locale file:KoRaLLL wrote: ↑Sun Jan 08, 2023 3:31 pmFor 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.
Code: Select all
[entity-name]
generic-entity=__1__
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)}
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Re: Any way to programatically generate localised names?
Here is your magic:Pi-C wrote: ↑Sun Jan 08, 2023 6:35 pmCode: 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)}
Code: Select all
local get_localised_item_name = function(entity)
return "__ENTITY__" .. entity.name .. "__"
end
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: Any way to programatically generate localised names?
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:Bilka wrote: ↑Mon Jan 09, 2023 12:25 pmHere is your magic:See https://wiki.factorio.com/Tutorial:Loca ... parameters - this is what those built-in parameters are for.Code: Select all
local get_localised_item_name = function(entity) return "__ENTITY__" .. entity.name .. "__" end
KoRaLLL wrote: ↑Sun Jan 08, 2023 3:31 pmFor 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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Re: Any way to programatically generate localised names?
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
This console behavior was pretty unexpected.
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:
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?
Here's a few things I tried:
In data.lua
Code: Select all
data.raw.furnace['stone-furnace'].localised_name = "Lol __ENTITY__electric-furnace__"
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"}}