LuaPlayer :: request_locale_name()

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
raiguard
Factorio Staff
Factorio Staff
Posts: 452
Joined: Wed Dec 13, 2017 8:29 pm
Contact:

LuaPlayer :: request_locale_name()

Post by raiguard »

In order to enable localised search in a mod, one must use the player.request_translation() function. However, since locale is non-deterministic, this function can't just return the translated text - you must wait for a corresponding event to fire with the result. Because of this, you need to build the entire dictionary at once, for every possible thing that you will be searching.

I wrote a library to handle all of this. However, there is a problem: performance.

Image

Just requesting 10 translations per tick makes the time usage soar to over 2 milliseconds! As it is right now, you need to re-build the dictionary every time a player joins the game, in case their locale changed since last time. This would obviously wreak havoc on performance on large servers, where players are connecting and disconnecting often. It turns out that the translation function isn't the issue, it's my logic... ugh.

This is why I request a player.request_locale_name() function. This function, through the corresponding event, would return the name of the locale that the player is using(en, zh-CN, es-ES, etc). This significantly cuts down on the amount of times you need to build a dictionary.

Using this function, I could tie dictionaries not to specific players, but to languages. When a player joins a game, if the dictionary for that language has already been made, it just sets that player to using that language. If not, it uses that player to assemble the dictionary for whatever language they are using. The only time after that that you would need to rebuild that dictionary would be after on_configuration_changed.
Don't forget, you're here forever.

Honktown
Smart Inserter
Smart Inserter
Posts: 1026
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: LuaPlayer :: request_locale_name()

Post by Honktown »

Couldn't you build dictionaries, then use a unique answer/reply which indicates what languages they use? I would write you a non-lua script that reads through the locale folders, stores all locales+strings, and puts them into Lua tables if you want it. Make a mod that just... has all the translation strings stored if someone needed them for some reason. Would need to validate multi-byte characters somehow.
I have mods! I guess!
Link

Post Reply

Return to “Modding interface requests”