Page 1 of 1

API call to localize string

Posted: Thu May 04, 2017 1:42 am
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)

Re: API call to localize string

Posted: Thu May 04, 2017 1:50 am
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".

Re: API call to localize string

Posted: Thu May 04, 2017 1:51 am
by Nexela
Take a look at https://github.com/Nexela/compressor For dynamic localized names

Re: API call to localize string

Posted: Thu May 04, 2017 1:51 am
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"

Re: API call to localize string

Posted: Thu May 04, 2017 2:02 am
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?

Re: API call to localize string

Posted: Thu May 04, 2017 3:00 am
by Nexela
From the example mod above

myentity.localised_name = {"entity.name-"..theirentity.name}

Re: API call to localize string

Posted: Thu May 04, 2017 3:02 am
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"}

Re: API call to localize string

Posted: Thu May 04, 2017 3:31 am
by thenameipicked
Thank you! This is perfect.