API call to localize string

thenameipicked
Burner Inserter
Burner Inserter
Posts: 13
Joined: Wed Mar 15, 2017 12:36 am
Contact:

API call to localize string

Post by thenameipicked »

I'm programatically creating clones of existing prototypes in the game. However, this requires me to name them differently, which breaks localization. I've come up with 5 possible solutions (3 of which would require a change from the game):

1. When data:extend() is called, immediately localize, and then add it to the prototype.

2. Allow me to call a function to localize an arbitrary string. I believe in this post Rseding said it could lead to desync issues, but that doesn't make sense to me, because I have access to localised strings at control.lua time, so I can desync anyways off of that data.

3. Allow wildcards when matching names in localization. This way I can have `furnace-1` and `furnace-2` both be localized to the same thing.

4. Add an attribute "localizing_name" which is the name it will use to localize the item.

5. I simply take the name of the prototype and programatically generate an English localized name.

(For context, this is why I'm creating clones of existing prototypes)
Rseding91
Factorio Staff
Factorio Staff
Posts: 14913
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: API call to localize string

Post by Rseding91 »

thenameipicked wrote:4. Add an attribute "localizing_name" which is the name it will use to localize the item.
This has existed since 0.13 :P it's called "localized_name" there's also "localized_description".
If you want to get ahold of me I'm almost always on Discord.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: API call to localize string

Post by Nexela »

Take a look at https://github.com/Nexela/compressor For dynamic localized names
thenameipicked
Burner Inserter
Burner Inserter
Posts: 13
Joined: Wed Mar 15, 2017 12:36 am
Contact:

Re: API call to localize string

Post by thenameipicked »

I think you misunderstand. "Localized_name" isn't what I want, because it doesn't exist on the object at data.lua time. I can set it, but I can't read it. I want a "localizing_name" which basically says to the engine "use this name to localize instead of the attribute name"
thenameipicked
Burner Inserter
Burner Inserter
Posts: 13
Joined: Wed Mar 15, 2017 12:36 am
Contact:

Re: API call to localize string

Post by thenameipicked »

Nexela wrote:Take a look at https://github.com/Nexela/compressor For dynamic localized names
This is really interesting, but I'm not sure I understand what is happening:

You are calling

Code: Select all

localised_name = {"recipe-name.compress-item", loc_key}
which is either

Code: Select all

 {"item-name."..item.name}
item.localised_name
{"entity-name."..item.place_result}
-- or
{"equipment-name."..item.placed_as_equipment_result}
It looks like localised_name can take an array of what to be localised? Why are some of the values strings (item.localised_name), and some of the values arrays? What does the key of "recipe-name.compress-item" do?

Is there some sort of documentation on this?
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: API call to localize string

Post by Nexela »

From the example mod above

myentity.localised_name = {"entity.name-"..theirentity.name}
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1197
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: API call to localize string

Post by Mooncat »

localised_name and localised_description are what you need.
They can be defined in data phase. They accept LocalisedString.

Say, if you want to make a copy of electric furnace, and you want it to display the same name as an electric furnace, you can set

Code: Select all

localised_name = {"entity-name.electric-furnace"}
where "entity-name" is the category name and "electric-furnace" is the key under that category. You can find them in the locale files.

It can be used as an array to fill the "__1__" "__2__" etc in the actual localised string.
For example, you can see in the locale file:

Code: Select all

[modifier-description]
stack-inserter-capacity-bonus=Stack inserter pickup bonus: +__1__
If you set

Code: Select all

localised_name = {"modifier-description.stack-inserter-capacity-bonus", "ABC"}
then you will see "Stack inserter pickup bonus: +ABC" in game.
Note that only the first value is localised. The 2nd value "ABC", and so on, will be used directly.
But nested localised string is possible, e.g.

Code: Select all

localised_name = {"modifier-description.stack-inserter-capacity-bonus", {"entity-name.electric-furnace"}}
You will see "Stack inserter pickup bonus: +Electric furnace".

Edit: another trick. Instead of using the localisation key, you can also use the magic string __ENTITY__(entity name)__.

Code: Select all

localised_name = "__ENTITY__electric-furnace__"
This will be the same as

Code: Select all

localised_name = {"entity-name.electric-furnace"}
thenameipicked
Burner Inserter
Burner Inserter
Posts: 13
Joined: Wed Mar 15, 2017 12:36 am
Contact:

Re: API call to localize string

Post by thenameipicked »

Thank you! This is perfect.
Post Reply

Return to “Implemented mod requests”