General localization for string setting values

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
rycieos
Manual Inserter
Manual Inserter
Posts: 4
Joined: Wed Feb 27, 2019 8:35 pm
Contact:

General localization for string setting values

Post by rycieos »

Problem
When creating localization for mod settings, you can create locale strings for specific string-setting values. Quoting the wiki (https://wiki.factorio.com/Tutorial:Mod_settings#Locale):
[string-mod-setting]
#<setting-name>-<dropdown-item-name>=<translated dropdown item>
my-mod-string-test-setting-item-1=Item 1 localized string

[string-mod-setting-description]
#<setting-name>-<dropdown-item-name>=<tooltip of dropdown item>
my-mod-string-test-setting-item-1=Item 1 localized tooltip
However, this means that a locale string needs to exist for every single setting, even if every setting shares the same string IDs.
Request
What I want to be able to do is this:

Code: Select all

[string-mod-setting-global]
blue=Blue
green=Green
grey=Grey
none=None
...
But what I am forced to do right now is this:

Code: Select all

[string-mod-setting]
my-mod-available-technology-background-color-grey=Grey
my-mod-available-technology-background-color-blue=Blue
my-mod-available-technology-background-color-green=Green
my-mod-conditionally-available-technology-background-color-grey=Grey
my-mod-conditionally-available-technology-background-color-blue=Blue
my-mod-conditionally-available-technology-background-color-green=Green
my-mod-unavailable-technology-background-color-grey=Grey
my-mod-unavailable-technology-background-color-blue=Blue
my-mod-unavailable-technology-background-color-green=Green
...
My current file is 373 lines long, because I have 72 string settings! This is unmaintainable and impossible to ask translators to work on.

Please add a new localization section named "string-mod-setting-global" or similar that the game will check second after "string-mod-setting" that does not require the setting name as part of the locale ID.
Workaround
This is my current workaround, if it helps to understand what I am requesting. Or if anyone else has this problem, here is how I solved it:

Create locale files with strings at the root for all of your possible option strings:

Code: Select all

# locale/en/setting-options.cfg
blue=Blue
green=Green
grey=Grey
...
Then create a script that loads your mod settings and generates locale strings and execute it like "lua generate-locale.lua locale/*/setting-options.cfg":

Code: Select all

#!/usr/bin/env lua

data = {}
settings = {}
function data:extend(array)
  for _, setting in ipairs(array) do
    table.insert(settings, setting)
  end
end

require("settings")

for _, lang_file in pairs({...}) do
  local file = io.open(lang_file, "r")
  local strings = file:read("*all")
  file:close()

  local locale_strings = {}
  for string in strings:gmatch("[^\r\n]+") do
    local key, value = string:match("([^=]+)=(.+)")
    locale_strings[key] = value
  end

  file = io.open(lang_file, "w")
  file:write("[string-mod-setting]\n")

  for _, setting in ipairs(settings) do
    if setting.type == "string-setting" and setting.allowed_values then
      for _, allowed_value in ipairs(setting.allowed_values) do
        local value = locale_strings[allowed_value]
        if value then
          file:write(setting.name .. "-" .. allowed_value .. "=" .. value .. "\n")
        end
      end
    end
  end
  file:close()
end
Then what gets spit out is this:

Code: Select all

[string-mod-setting]
my-mod-available-technology-background-color-grey=Grey
my-mod-available-technology-background-color-blue=Blue
my-mod-available-technology-background-color-green=Green
my-mod-conditionally-available-technology-background-color-grey=Grey
my-mod-conditionally-available-technology-background-color-blue=Blue
my-mod-conditionally-available-technology-background-color-green=Green
my-mod-unavailable-technology-background-color-grey=Grey
my-mod-unavailable-technology-background-color-blue=Blue
my-mod-unavailable-technology-background-color-green=Green
...
For full details, you can see my commit https://github.com/Rycieos/factorio-col ... 8b8c24ecf5.

curiosity
Filter Inserter
Filter Inserter
Posts: 343
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: General localization for string setting values

Post by curiosity »

rycieos wrote:
Thu May 02, 2024 2:25 pm
Please add a new localization section named "string-mod-setting-global" or similar that the game will check second after "string-mod-setting" that does not require the setting name as part of the locale ID.
No. This is commonly solved by letting you specify a localized string is the prototype. So ask for that instead of introducing a unique special behavior for no reason.

E.g. add a field localised_allowed_values to the string setting prototype. If specified, it must be an array of LocalisedString the same length as the allowed_values array. The localized names are matched to the values in order.

User avatar
rycieos
Manual Inserter
Manual Inserter
Posts: 4
Joined: Wed Feb 27, 2019 8:35 pm
Contact:

Re: General localization for string setting values

Post by rycieos »

curiosity wrote:
Thu May 02, 2024 10:46 pm
E.g. add a field localised_allowed_values to the string setting prototype. If specified, it must be an array of LocalisedString the same length as the allowed_values array. The localized names are matched to the values in order.
Sure, that would work too. It would be a little more work for a mod author, but would also give more flexibility. Still, it would be much better than the current situation.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2633
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: General localization for string setting values

Post by FuryoftheStars »

rycieos wrote:
Thu May 02, 2024 2:25 pm
Please add a new localization section named "string-mod-setting-global" or similar that the game will check second after "string-mod-setting" that does not require the setting name as part of the locale ID.
My concern with this approach is that the locale files are not specific to each mod. There's a reason why the current setup has you specify the setting name in it.

As a rough example, imagine a mod that has a string drop-down selection that has the player choose between a light and dark color option for something. They don't feel as though any localization is needed on the text, so they leave it as just "light" and "dark".

Now imagine another mod with string drop-down options for oil. They use "light", "heavy", and "petrol" for the text on the settings, but they use the proposed global localization option to set "light=Light Oil" (etc).

Now the first mod is going to give players the color options of "Light Oil" and "dark".

Humorous, but obviously not what was intended. :)
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics

User avatar
rycieos
Manual Inserter
Manual Inserter
Posts: 4
Joined: Wed Feb 27, 2019 8:35 pm
Contact:

Re: General localization for string setting values

Post by rycieos »

FuryoftheStars wrote:
Fri May 03, 2024 12:50 am
My concern with this approach is that the locale files are not specific to each mod. There's a reason why the current setup has you specify the setting name in it.
All fair points. I think curiosity's suggestion doesn't have that issue, so again I would be fine with that. Just so I don't need hundreds of duplicated lines long locale files.

Post Reply

Return to “Modding interface requests”