Any way to programatically generate localised names?

Place to get help with not working mods / modding interface.
Post Reply
KoRaLLL
Burner Inserter
Burner Inserter
Posts: 15
Joined: Sun Sep 11, 2022 7:22 pm
Contact:

Any way to programatically generate localised names?

Post 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!

robot256
Filter Inserter
Filter Inserter
Posts: 597
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: Any way to programatically generate localised names?

Post 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

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Any way to programatically generate localised names?

Post 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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Bilka
Factorio Staff
Factorio Staff
Posts: 3140
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Any way to programatically generate localised names?

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Any way to programatically generate localised names?

Post by Pi-C »

Bilka wrote:
Mon Jan 09, 2023 12:25 pm
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.
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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

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

Re: Any way to programatically generate localised names?

Post 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 of stone furnace with the name Lol __ENTITY__electric-furnace__
Image 404.png (21.57 KiB) Viewed 419 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 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 419 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 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 419 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 of stone furnace with the name Lol Electric Furnace
Image 405.png (15.78 KiB) Viewed 419 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?

Post Reply

Return to “Modding help”