Resolve LocalisedString to string
Resolve LocalisedString to string
Given a LocalisedString and a locale, I would like to be able to resolve them into an ordinary string. I would also like to be able to look up the locale that each player is using. (Both of those should be deterministic, I would imagine.) (Perhaps these could be combined into a single method, attached to LuaPlayer, that resolves a localized string.) This would allow me to provide a user-friendly interface instead of printing the internal item prototype names for the user to see. (I am writing to an external file.)
Alternatively, if there were a way to read the console text history, I could print a LocalisedString to the console and read it from there.
Alternatively, if there were a way to read the console text history, I could print a LocalisedString to the console and read it from there.
Re: Resolve LocalisedString to string
There shouldn't be a need for this: you can nest localisedstrings and let them take parameters. This lets you display translated names for items/entities/etc. in console messages/GUI/etc.
Would print "Copper ore is an entity." Likewise this can be done for all text strings for your mod.
As mentioned, nesting is allowed, so:
Would print "Copper ore is an entity. is an entity."
Edit: may have misunderstood what you wanted, since you want to write to a file. Maybe a better request would be to let game.write_file accept a localisedstring?
Code: Select all
[message]
mod-message-with-parameter=__1__ is an entity.
game.print{"message.mod-message-with-parameter", "__ENTITY__copper-ore__"}
or
game.print{"message.mod-message-with-parameter", "entity-name.copper-ore"}
As mentioned, nesting is allowed, so:
Code: Select all
game.print{"message.mod-message-with-parameter", {"message.mod-message-with-parameter", "entity-name.copper-ore"}}
Edit: may have misunderstood what you wanted, since you want to write to a file. Maybe a better request would be to let game.write_file accept a localisedstring?
- eradicator
- Smart Inserter
- Posts: 5207
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Resolve LocalisedString to string
log() actually translates localised strings. Though i wouldn't mind if there was something simpler, like "lprint()".
Code: Select all
/c log({'Hi, can i interest you in some __1__?',{'item-name.iron-ore'}})
Re: Resolve LocalisedString to string
Using log() is clunky (since now I need to post-process the log) but I guess it works. Thanks for the tip!
Re: Resolve LocalisedString to string
Something simpler probably won't come along, because locale is inherently non-deterministic.eradicator wrote:log() actually translates localised strings. Though i wouldn't mind if there was something simpler, like "lprint()".
Code: Select all
/c log({'Hi, can i interest you in some __1__?',{'item-name.iron-ore'}})
But something that could work is as follows:
Code: Select all
local string = game.translate({item-name.iron-plate}, game.players[1].locale)
It could also be used to just hard 'translate' to a specific locale:
Code: Select all
local czech_string = game.translate({item-name.iron-plate}, "cz")
Re: Resolve LocalisedString to string
Something like that is exactly what I'd be hoping for. I'll keep my eyes peeled in case that happens! Thanks for the reply.Klonan wrote:Code: Select all
local string = game.translate({item-name.iron-plate}, game.players[1].locale)
- eradicator
- Smart Inserter
- Posts: 5207
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Resolve LocalisedString to string
I meant a version of print() that translates localized strings wtihout affecting the game state. I.e. log() without the decorations (timstamp, etc) for easier processing by outside applications. But...Klonan wrote:Something simpler probably won't come along, because locale is inherently non-deterministic.eradicator wrote:log() actually translates localised strings. Though i wouldn't mind if there was something simpler, like "lprint()".
Code: Select all
/c log({'Hi, can i interest you in some __1__?',{'item-name.iron-ore'}})
If you actually give us modders access to translation within the game state that would be a mighty awesome easter/christmas gift, as it would finally allow locale aware mod GUIs including search functionality. In cross-language multiplayer it would even allow for players to see what items are named in other locales, making communication much easier than in-head translation.Klonan wrote: But something that could work is as follows:
Because if we store which locale each player is using in the save game, and also update it each time they join the game, it could work.Code: Select all
local string = game.translate({item-name.iron-plate}, game.players[1].locale)
It could also be used to just hard 'translate' to a specific locale:
Code: Select all
local czech_string = game.translate({item-name.iron-plate}, "cz")
Re: Resolve LocalisedString to string
So the problem, is that each client would have to have exactly the same locale, and all locales would have to be loaded for all players
So things like, locale only mods, would not work in the non-synchronous way they do now, where you can join a server with your own locale mods on,
And so, all players will have to load all locale for all the languages we support, which isn't insignificant (we have about 3,700 strings / 20,000 words)
So things like, locale only mods, would not work in the non-synchronous way they do now, where you can join a server with your own locale mods on,
And so, all players will have to load all locale for all the languages we support, which isn't insignificant (we have about 3,700 strings / 20,000 words)
Re: Resolve LocalisedString to string
I think this is a acceptable cost for the feature. There have been many threads asking for this, mostly to allow search using localized names in mods like FNEI. To me, solving those issues is more important than slightly increased loading times.Klonan wrote:So the problem, is that each client would have to have exactly the same locale, and all locales would have to be loaded for all players
So things like, locale only mods, would not work in the non-synchronous way they do now, where you can join a server with your own locale mods on,
And so, all players will have to load all locale for all the languages we support, which isn't insignificant (we have about 3,700 strings / 20,000 words)
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
- eradicator
- Smart Inserter
- Posts: 5207
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Resolve LocalisedString to string
This could be significantly reduced if you only load the locales actually used by players in a given game. So:Klonan wrote:So the problem, is that each client would have to have exactly the same locale, and all locales would have to be loaded for all players
Code: Select all
local string = player.translate({item-name.iron-plate})
Without actual data it's of course hard to say but i would be suprised if more than 50% of the games had more than one locale anyway, and probably less than 10% have more than 10 different locales. And while "translate to any locale anytime" sounds fun, it ultimately only has some very narrow usecases when compared to player-only translation.
I have no experience with local-only mods. My gut feeling would be "well, one more mod isn't going to matter much if i already have 20". But ofc public server hosters would have to install those mods before anyone using them joins the server, so...public hosts would be in a situation where they need to install a bunch (the mod portal current has 20) of locale mods. Private servers imho wouldn't be affected because they already know whos going to be playing (or can install things on personal requests). Hm. Unless ofc you can just sync the missing locale keys over to everyone. The chinese locale mod is 460kB. How much is that compared to the usual bandwidth usage of factorio MP?Klonan wrote:So things like, locale only mods, would not work in the non-synchronous way they do now, where you can join a server with your own locale mods on.
So simplified end-user perspective:
<Locale aware mod guis for everyone> vs <Some public servers will need to install a few more mods>
I know on which side of that i'd like to stand (especially after recently trying to use FNEI and being frustrated about having to guess mod-internal item names when searching).
Though the mod portal should definetly support automatic mod-syncing on server joining to ease the transition. And maybe the ingame mod-menu could get an extra section for locale-only mods to make them easier to manage, for example by sorting them all to the end of the list.
- eradicator
- Smart Inserter
- Posts: 5207
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Resolve LocalisedString to string
I just noticed that using the current localized string system has another large flaw: The lua side can not even be aware if a locale string exists at all.
I was trying to make a GUI that has a few sprite-buttons displaying various items/technologies. And the tooltip was supposed to show the description of the displayed item/technology. This works just fine with things that have a description. But not all items/technlogies have one, and for things without a description the tooltip shows the infamous "Unknown key:" warning instead, which is of course not something i can display to the average player and leave them to guess what it means. Which means that in conclusion i can not use prototype.localized_description at all. This makes me very sad indeed. :(
I was trying to make a GUI that has a few sprite-buttons displaying various items/technologies. And the tooltip was supposed to show the description of the displayed item/technology. This works just fine with things that have a description. But not all items/technlogies have one, and for things without a description the tooltip shows the infamous "Unknown key:" warning instead, which is of course not something i can display to the average player and leave them to guess what it means. Which means that in conclusion i can not use prototype.localized_description at all. This makes me very sad indeed. :(
- eradicator
- Smart Inserter
- Posts: 5207
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Resolve LocalisedString to string
*Necro necro animus!*
Is this actually still possible? I noticed that in 0.17 the distinction between "control only" and "has data" mods seems to have disappeared and activating new mods now unconditionally requires a restart.
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Resolve LocalisedString to string
If you want to get ahold of me I'm almost always on Discord.