LuaPlayer :: request_locale_name()
Posted: Mon Dec 23, 2019 10:16 pm
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.

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.
I wrote a library to handle all of this. However, there is a problem: performance.

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.