|
| 1 | +Localization toolkit is for Flow C# plugin developers to improve their localization experience. |
| 2 | + |
| 3 | +## Initialization |
| 4 | + |
| 5 | +For C# Plugins, we need to install and reference [Flow.Launcher.Localization](www.nuget.org/packages/Flow.Launcher.Localization) by Nuget. |
| 6 | + |
| 7 | +## Build properties |
| 8 | + |
| 9 | +### `FLLUseDependencyInjection` |
| 10 | + |
| 11 | +Whether to use depenedency injection to get `IPublicAPI` instance. Default by false. |
| 12 | + |
| 13 | +If set to `false`, the `Main` class that implements **[IPlugin](/API-Reference/Flow.Launcher.Plugin/IPlugin.md)** or **[IAsyncPlugin](/API-Reference/Flow.Launcher.Plugin/IAsyncPlugin.md)** must have a [PluginInitContext](/API-Reference/Flow.Launcher.Plugin/PluginInitContext.md) property which must be at least `internal static`. |
| 14 | + |
| 15 | +If set to `true`, we can access `IPublicAPI` instance from `PublicApi.Instance` in the project by dependency injection. |
| 16 | +And the `Main` class does not need to have a [PluginInitContext](/API-Reference/Flow.Launcher.Plugin/PluginInitContext.md) property. |
| 17 | +(Not recommended for plugin projects because this will make plugins only compabible with Flow 1.20.0 or higher) |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Main class |
| 22 | + |
| 23 | +`Main` class must implement [IPluginI18n](/API-Reference/Flow.Launcher.Plugin/IPluginI18n.md). |
| 24 | + |
| 25 | +If `FLLUseDependencyInjection` is set to `false`, `Main` class must have a [PluginInitContext](/API-Reference/Flow.Launcher.Plugin/PluginInitContext.md) property like: |
| 26 | + |
| 27 | +``` |
| 28 | +public class Main : IPlugin, IPluginI18n // Must implement IPluginI18n |
| 29 | +{ |
| 30 | + internal static PluginInitContext Context { get; private set; } = null!; // At least internal static property |
| 31 | + private static IPublicAPI API => Context.API; |
| 32 | +
|
| 33 | + ... |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Localized strings |
| 38 | + |
| 39 | +With this toolkit, we can replace `Context.API.GetTranslation("flowlauncher_plugin_localization_demo_plugin_name")` with `Localize.flowlauncher_plugin_localization_demo_plugin_name()`. |
| 40 | + |
| 41 | +And we can also replace `string.Format(Context.API.GetTranslation("flowlauncher_plugin_localization_demo_plugin_used"), string.Empty, null, string.Empty)` with `Localize.flowlauncher_plugin_localization_demo_plugin_used(string.Empty, null, string.Empty)`. |
| 42 | + |
| 43 | +### Localized enums |
| 44 | + |
| 45 | +If you have enum types like `DemoEnum` that needs localization in displaying them on a combo box control. You can add `EnumLocalize` attribute to enable localiztion support. |
| 46 | +For all fields in this `EnumType`, if you want to specific one localization key for this field, you can use `EnumLocalizeKey` attribute; or if you want to specific one constant value for this field, you can use `EnumLocalizeValue` attribute. |
| 47 | + |
| 48 | +``` |
| 49 | +[EnumLocalize] // Enable localization support |
| 50 | +public enum DemoEnum |
| 51 | +{ |
| 52 | + [EnumLocalizeKey("localize_key_1")] // Specific localization key |
| 53 | + Value1, |
| 54 | +
|
| 55 | + [EnumLocalizeValue("localize_value_2")] // Specific localization value |
| 56 | + Value2, |
| 57 | +
|
| 58 | + [EnumLocalizeKey("localize_key_3")] // If key and value both exist, will prefer localization key |
| 59 | + [EnumLocalizeValue("localize_value_3")] |
| 60 | + Value3, |
| 61 | +
|
| 62 | + [EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_localization_demo_plugin_description))] // Use Localize class |
| 63 | + Value4, |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +Then you can get `DemoEnumData` class. |
| 68 | + |
| 69 | +In view model class which needs to display it on a combo box control, you can add two fields for binding `ItemSource` and `SelectedValue` like: |
| 70 | + |
| 71 | +``` |
| 72 | +public List<DemoEnumData> AllDemoEnums { get; } = DemoEnumData.GetValues(); // ItemSource of ComboBox |
| 73 | +
|
| 74 | +public DemoEnum SelectedDemoEnum { get; set; } // SelectedValue of ComboBox |
| 75 | +``` |
| 76 | + |
| 77 | +``` |
| 78 | +<ComboBox |
| 79 | + DisplayMemberPath="Display" |
| 80 | + ItemsSource="{Binding AllDemoEnums}" |
| 81 | + SelectedValue="{Binding SelectedDemoEnum}" |
| 82 | + SelectedValuePath="Value" /> |
| 83 | +``` |
| 84 | + |
| 85 | +If you want to update localization strings when culture info changes, you can call this function to update. |
| 86 | + |
| 87 | +``` |
| 88 | +private void UpdateEnumDropdownLocalizations() |
| 89 | +{ |
| 90 | + DemoEnumData.UpdateLabels(AllDemoEnums); |
| 91 | +} |
| 92 | +``` |
0 commit comments