Predefined strings in localization files

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1646
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Predefined strings in localization files

Post by Pi-C »

Hi! I've read the Localization tutorial and I know now how to use localized strings -- even those with parameters -- in my code. What I'm looking for is something similar for use in the localization files.

Consider this example:

Code: Select all

[GCKI-messages]
dead=Your __1__ is destroyed, use your keys to claim a new vehicle!
miner_bot=Robots mined your __1__, use your keys to claim a new vehicle!
miner_player=__1__ mined your __2__, use your keys to claim a new vehicle!
As you can see, the string "use your keys to claim a new vehicle!" is used multiple times. It would be easier if predefined strings could be used instead (Made a typo? Fix it in one place, instead of the complete file!). I'm thinking of something along these lines:

Code: Select all


@STRING{CLAIM="use your keys to claim a new vehicle!"}

[GCKI-messages]
dead=Your __1__ is destroyed, #CLAIM
miner_bot=Robots mined your __1__, #CLAIM
miner_player=__1__ mined your __2__, #CLAIM
Could you implement something like this, please?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Predefined strings in localization files

Post by Klonan »

You can do this already:

Code: Select all

claim=use your keys.
dead=Your __1__ is destroyed, __2__
Then in code:

Code: Select all

game.print({"dead", "marriage", {"claim"}})

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

Re: Predefined strings in localization files

Post by Pi-C »

Klonan wrote:
Tue Sep 10, 2019 9:43 am
You can do this already:

Code: Select all

claim=use your keys.
dead=Your __1__ is destroyed, __2__
Then in code:

Code: Select all

game.print({"dead", "marriage", {"claim"}})
Thanks, but I think you misunderstood me. Concatenating the strings with game.print is not the issue here, what I meant is more like a preprocessor.

Let me explain:

Code: Select all

dead=Your __1__ is destroyed, __2__
This is perfect if you want to use this in game, e.g. when acting on an event. Something has been destroyed (we don't know what when writing the code), so this code inserts the value of a variable into the localized string at runtime. It's flexible, but you have to call game.print with the name of the complete localized string (here: {"dead"}) and with the names of localized strings for the arguments (here: {"claim"}).

If I know that parameter _1_ is the only thing that will ever change in the game, it makes little sense to use a variable for it. However, if I want to define a lot of localized strings that have the same phrase in common, defining rather than using the strings could be made more flexible.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Predefined strings in localization files

Post by Deadlock989 »

It's not just about game.print. You can use the __1__, __2__ etc. syntax in any localisation string, without using concatenation at all. For example, there are 5 of them in this tooltip, including the recipe name. The things substituted for __1__ can themselves be localised strings that you have defined, which can themselves contain substitutions, effectively ad infinitum.
locale.png
locale.png (158.09 KiB) Viewed 1524 times
Image

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Predefined strings in localization files

Post by eradicator »

(+1)

I tripped over this when polishing up my stockpile mod the other day. It generates a bunch of entities, all of which have the same [entity-description]. But the limits of the cross-referencing system do not allow access to descriptions at "localise-stage".

For example it's possible to references entity/item names:

Code: Select all

[entity-name]
example=Example
big_example=Big __ENTITY__example__
but not descriptions:

Code: Select all

[entity-description]
example=unsuspicious
big_example=really __ENTITY-DESCRIPTION__example__
It's also not possible to cross-reference custom categories

Code: Select all

[er]
example-word=foobar
[entity-name]
large-example=large __ER__EXAMPLE-WORD__
If this system was extended to allow cross-referencing any key from any category (or even just a "fixed string" category) that would be quite useful. Essentially the idea is to keep *static* compositions inside the locale files and only do *dynamic* composition at runtime. (Kinda hoping this works already and i just don't know how to use it properly...)
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

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

Re: Predefined strings in localization files

Post by Pi-C »

eradicator wrote:
Tue Sep 10, 2019 10:25 am
For example it's possible to references entity/item names:

Code: Select all

[entity-name]
example=Example
big_example=Big __ENTITY__example__
Yes, got that one to work. This seems to be a case of a built-in parameter.
but not descriptions:

Code: Select all

[entity-description]
example=unsuspicious
big_example=really __ENTITY-DESCRIPTION__example__
Apparently, ENTITY-DESCRIPTION is not a built-in parameter.
It's also not possible to cross-reference custom categories

Code: Select all

[er]
example-word=foobar
[entity-name]
large-example=large __ER__EXAMPLE-WORD__
Reading this, I thought you could cross-reference strings of the same custom category, but neither

Code: Select all

[GCKI-messages]
claim=use your keys to claim a new vehicle!
dead=Your __1__ is destroyed, __GCKI-messages__claim__
nor

Code: Select all

[GCKI-messages]
claim=use your keys to claim a new vehicle!
dead=Your __1__ is destroyed, __claim__
seem to work.
If this system was extended to allow cross-referencing any key from any category (or even just a "fixed string" category) that would be quite useful. Essentially the idea is to keep *static* compositions inside the locale files and only do *dynamic* composition at runtime. (Kinda hoping this works already and i just don't know how to use it properly...)
That's a better summary of what I meant than I made in my last post. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Predefined strings in localization files

Post by Optera »

Expanding built-in parameters would be nice to have for descriptions with constantly repeating text parts.

Post Reply

Return to “Modding interface requests”