From 78a504750fb0e03fe165c895d4dba2b262bfb3c3 Mon Sep 17 00:00:00 2001 From: art0007i Date: Wed, 24 Sep 2025 18:18:27 +0200 Subject: [PATCH 1/2] add config locale helpers --- .gitignore | 3 ++- ConfigLocale.cs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 92f6db9..299dcd5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ obj/ /packages/ riderModule.iml /_ReSharper.Caches/ -build/ \ No newline at end of file +build/ +.vs/ \ No newline at end of file diff --git a/ConfigLocale.cs b/ConfigLocale.cs index 52230f8..20ceef8 100644 --- a/ConfigLocale.cs +++ b/ConfigLocale.cs @@ -1,4 +1,5 @@ -using Elements.Core; +using BepInEx.Configuration; +using Elements.Core; namespace BepisLocaleLoader; @@ -19,4 +20,62 @@ public ConfigLocale(LocaleString name, LocaleString description) public LocaleString Name; public LocaleString Description; +} + +public static class ConfigLocaleHelper +{ + /// + /// Create a new setting with localized name and description. The resulting locale keys will be: Name = Settings.{guid}.{section}.{key}, Description = Settings.{guid}.{section}.{key}.Description + /// + /// Type of the value contained in this setting. + /// The file that the setting will be bound to. + /// The GUID of your mod, this is used to make the locale key unique from other plugins. + /// Section/category/group of the setting. Settings are grouped by this. + /// Name of the setting. + /// Value of the setting if the setting was not created yet. + /// The english description of your config key. This will be visible when editing config files via mod managers or manually. + public static ConfigEntry BindLocalized(this ConfigFile config, string guid, string section, string key, T defaultValue, string englishDescription) + { + return config.BindLocalized(guid, new ConfigDefinition(section, key), defaultValue, new ConfigDescription(englishDescription)); + } + + /// + /// Create a new setting with localized name and description. The resulting locale keys will be: Name = Settings.{guid}.{section}.{key}, Description = Settings.{guid}.{section}.{key}.Description + /// + /// Type of the value contained in this setting. + /// The file that the setting will be bound to. + /// The GUID of your mod, this is used to make the locale key unique from other plugins. + /// Section/category/group of the setting. Settings are grouped by this. + /// Name of the setting. + /// Value of the setting if the setting was not created yet. + /// Description and other metadata of the setting. The text description will be visible when editing config files via mod managers or manually. + public static ConfigEntry BindLocalized(this ConfigFile config, string guid, string section, string key, T defaultValue, ConfigDescription configDescription = null) + { + return config.BindLocalized(guid, new ConfigDefinition(section, key), defaultValue, configDescription); + } + + /// + /// Create a new setting with localized name and description. The resulting locale keys will be: Name = Settings.{guid}.{Section}.{Key}, Description = Settings.{guid}.{Section}.{Key}.Description + /// + /// Type of the value contained in this setting. + /// The file that the setting will be bound to. + /// The GUID of your mod, this is used to make the locale key unique from other plugins. + /// Section and Key of the setting. + /// Value of the setting if the setting was not created yet. + /// Description and other metadata of the setting. The text description will be visible when editing config files via mod managers or manually. + public static ConfigEntry BindLocalized(this ConfigFile config, string guid, ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null) + { + var localeName = $"Settings.{guid}.{configDefinition.Section}.{configDefinition.Key}"; + var localeDescription = localeName + ".Description"; + var locale = new ConfigLocale(localeName, localeDescription); + if(configDescription == null) + { + configDescription = new ConfigDescription(string.Empty, null, locale); + } + else + { + configDescription = new ConfigDescription(configDescription.Description, configDescription.AcceptableValues, [..configDescription.Tags, locale]); + } + return config.Bind(configDefinition, defaultValue, configDescription); + } } \ No newline at end of file From 1f31495f81ba0677961037b70ca56bc9253bf4ea Mon Sep 17 00:00:00 2001 From: art0007i Date: Fri, 26 Sep 2025 02:56:15 +0200 Subject: [PATCH 2/2] readme changes --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1023da3..3b04ac0 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,33 @@ A [Resonite](https://resonite.com/) mod that Loads locale files for Plugins. # Example config using ConfigDescription +You can add locale to a config entry by adding a `ConfigLocale` in your `ConfigDescription` like this: + ```cs private static ConfigEntry exampleConfigItem; public override void Load() { - exampleConfigItem = Config.Bind("General", "exampleConfigItem", false, new ConfigDescription("exampleConfigItem Description", null, new ConfigLocale("Settings.dev.author.myPlugin.config.Key", "Settings.dev.author.myPlugin.config.Description"))); + exampleConfigItem = Config.Bind("General", "exampleConfigItem", false, new ConfigDescription("exampleConfigItem Description", null, new ConfigLocale("Settings.dev.author.myPlugin.General.exampleConfigItem", "Settings.dev.author.myPlugin.General.exampleConfigItem.Description"))); } ``` +Or you can use the `ConfigLocaleHelper` class which provides the `BindLocalized` extension method like this: + +```cs +private static ConfigEntry exampleConfigItem; +public override void Load() +{ + // Generates the following locale keys: + // Name: "Settings.dev.author.myPlugin.General.exampleConfigItem" + // Description: "Settings.dev.author.myPlugin.General.exampleConfigItem.Description" + exampleConfigItem = Config.BindLocalized("dev.author.myPlugin", "General", "exampleConfigItem", false, "exampleConfigItem Description"); + // NOTE: If you are using the ResoniteModding plugin template you can use `PluginMetadata.GUID` instead of `"dev.author.myPlugin"` +} +``` + +When defining config entries with locale, it's still recommended to provide the entry's english description ("exampleConfigItem Description" in this case), as this will be visible when editing the config file manually or via mod managers. + + ## Adding Locale Strings Manually You can register a single localized string in code with: