API call to localize string
-
- Burner Inserter
- Posts: 13
- Joined: Wed Mar 15, 2017 12:36 am
- Contact:
API call to localize string
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)
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
This has existed since 0.13thenameipicked wrote:4. Add an attribute "localizing_name" which is the name it will use to localize the item.
![Razz :P](./images/smilies/icon_razz.gif)
If you want to get ahold of me I'm almost always on Discord.
Re: API call to localize string
Take a look at https://github.com/Nexela/compressor For dynamic localized names
-
- Burner Inserter
- Posts: 13
- Joined: Wed Mar 15, 2017 12:36 am
- Contact:
Re: API call to localize string
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"
-
- Burner Inserter
- Posts: 13
- Joined: Wed Mar 15, 2017 12:36 am
- Contact:
Re: API call to localize string
This is really interesting, but I'm not sure I understand what is happening:Nexela wrote:Take a look at https://github.com/Nexela/compressor For dynamic localized names
You are calling
Code: Select all
localised_name = {"recipe-name.compress-item", loc_key}
Code: Select all
{"item-name."..item.name}
item.localised_name
{"entity-name."..item.place_result}
-- or
{"equipment-name."..item.placed_as_equipment_result}
Is there some sort of documentation on this?
Re: API call to localize string
From the example mod above
myentity.localised_name = {"entity.name-"..theirentity.name}
myentity.localised_name = {"entity.name-"..theirentity.name}
Re: API call to localize string
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
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:
If you set
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.
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)__.
This will be the same as
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"}
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__
Code: Select all
localised_name = {"modifier-description.stack-inserter-capacity-bonus", "ABC"}
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"}}
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__"
Code: Select all
localised_name = {"entity-name.electric-furnace"}
-
- Burner Inserter
- Posts: 13
- Joined: Wed Mar 15, 2017 12:36 am
- Contact:
Re: API call to localize string
Thank you! This is perfect.