Resolve LocalisedString to string

swni
Long Handed Inserter
Long Handed Inserter
Posts: 91
Joined: Sat Mar 05, 2016 1:54 am
Contact:

Resolve LocalisedString to string

Post by swni »

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.
chrisgbk
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Mon Jan 02, 2017 4:31 am
Contact:

Re: Resolve LocalisedString to string

Post by chrisgbk »

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.

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"}
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:

Code: Select all

game.print{"message.mod-message-with-parameter", {"message.mod-message-with-parameter", "entity-name.copper-ore"}}
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?
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Resolve LocalisedString to string

Post by eradicator »

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'}})
swni
Long Handed Inserter
Long Handed Inserter
Posts: 91
Joined: Sat Mar 05, 2016 1:54 am
Contact:

Re: Resolve LocalisedString to string

Post by swni »

Using log() is clunky (since now I need to post-process the log) but I guess it works. Thanks for the tip!
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5304
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Resolve LocalisedString to string

Post by Klonan »

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'}})
Something simpler probably won't come along, because locale is inherently non-deterministic.

But something that could work is as follows:

Code: Select all

local string = game.translate({item-name.iron-plate}, game.players[1].locale)
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.

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")
swni
Long Handed Inserter
Long Handed Inserter
Posts: 91
Joined: Sat Mar 05, 2016 1:54 am
Contact:

Re: Resolve LocalisedString to string

Post by swni »

Klonan wrote:

Code: Select all

local string = game.translate({item-name.iron-plate}, game.players[1].locale)
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.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Resolve LocalisedString to string

Post by eradicator »

Klonan wrote:
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'}})
Something simpler probably won't come along, because locale is inherently non-deterministic.
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: But something that could work is as follows:

Code: Select all

local string = game.translate({item-name.iron-plate}, game.players[1].locale)
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.

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")
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.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5304
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Resolve LocalisedString to string

Post by Klonan »

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)
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Resolve LocalisedString to string

Post by Bilka »

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 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Resolve LocalisedString to string

Post by eradicator »

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
This could be significantly reduced if you only load the locales actually used by players in a given game. So:

Code: Select all

local string = player.translate({item-name.iron-plate})
Of course that would require the ability to load new locales when players join...
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.
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.
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?

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.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Resolve LocalisedString to string

Post by eradicator »

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. :(
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Resolve LocalisedString to string

Post by eradicator »

*Necro necro animus!*
Klonan wrote: Wed Feb 21, 2018 5:43 pm 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,
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.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14591
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Resolve LocalisedString to string

Post by Rseding91 »

If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Implemented mod requests”