Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
build/
build/
.vs/
61 changes: 60 additions & 1 deletion ConfigLocale.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Elements.Core;
using BepInEx.Configuration;
using Elements.Core;

namespace BepisLocaleLoader;

Expand All @@ -19,4 +20,62 @@ public ConfigLocale(LocaleString name, LocaleString description)

public LocaleString Name;
public LocaleString Description;
}

public static class ConfigLocaleHelper
{
/// <summary>
/// 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
/// </summary>
/// <typeparam name="T">Type of the value contained in this setting.</typeparam>
/// <param name="config">The file that the setting will be bound to.</param>
/// <param name="guid">The GUID of your mod, this is used to make the locale key unique from other plugins.</param>
/// <param name="section">Section/category/group of the setting. Settings are grouped by this.</param>
/// <param name="key">Name of the setting.</param>
/// <param name="defaultValue">Value of the setting if the setting was not created yet.</param>
/// <param name="englishDescription">The english description of your config key. This will be visible when editing config files via mod managers or manually.</param>
public static ConfigEntry<T> BindLocalized<T>(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));
}

/// <summary>
/// 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
/// </summary>
/// <typeparam name="T">Type of the value contained in this setting.</typeparam>
/// <param name="config">The file that the setting will be bound to.</param>
/// <param name="guid">The GUID of your mod, this is used to make the locale key unique from other plugins.</param>
/// <param name="section">Section/category/group of the setting. Settings are grouped by this.</param>
/// <param name="key">Name of the setting.</param>
/// <param name="defaultValue">Value of the setting if the setting was not created yet.</param>
/// <param name="configDescription">Description and other metadata of the setting. The text description will be visible when editing config files via mod managers or manually.</param>
public static ConfigEntry<T> BindLocalized<T>(this ConfigFile config, string guid, string section, string key, T defaultValue, ConfigDescription configDescription = null)
{
return config.BindLocalized(guid, new ConfigDefinition(section, key), defaultValue, configDescription);
}

/// <summary>
/// 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
/// </summary>
/// <typeparam name="T">Type of the value contained in this setting.</typeparam>
/// <param name="config">The file that the setting will be bound to.</param>
/// <param name="guid">The GUID of your mod, this is used to make the locale key unique from other plugins.</param>
/// <param name="configDefinition">Section and Key of the setting.</param>
/// <param name="defaultValue">Value of the setting if the setting was not created yet.</param>
/// <param name="configDescription">Description and other metadata of the setting. The text description will be visible when editing config files via mod managers or manually.</param>
public static ConfigEntry<T> BindLocalized<T>(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);
}
}
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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<bool> 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:
Expand Down
Loading