Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Flow Launcher Localization Toolkit

Localization toolkit for Flow Launcher and its plugins

Useful links:

* [Flow Launcher localization toolkit guide](https://www.flowlauncher.com/docs/#/localization-toolkit)
* [.Net plugin development guide](https://www.flowlauncher.com/docs/#/develop-dotnet-plugins)
90 changes: 90 additions & 0 deletions localization-toolkit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Localization toolkit is for Flow C# plugin developers to improve their localization experience.

## Initialization

For C# Plugins, we need to install and reference [Flow.Launcher.Localization](www.nuget.org/packages/Flow.Launcher.Localization) by Nuget.

## Build properties

### `FLLUseDependencyInjection`

Whether to use dependency injection to get `IPublicAPI` instance. Default by false.
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`.

If set to `true`, we can access `IPublicAPI` instance from `PublicApi.Instance` in the project by dependency injection.
And the `Main` class does not need to have a [PluginInitContext](/API-Reference/Flow.Launcher.Plugin/PluginInitContext.md) property.
(Not recommended for plugin projects because this will make plugins only compatible with Flow 1.20.0 or higher)
## Usage

### Main class

`Main` class must implement [IPluginI18n](/API-Reference/Flow.Launcher.Plugin/IPluginI18n.md).

If `FLLUseDependencyInjection` is set to `false`, `Main` class must have a [PluginInitContext](/API-Reference/Flow.Launcher.Plugin/PluginInitContext.md) property like:

```
public class Main : IPlugin, IPluginI18n // Must implement IPluginI18n
{
internal static PluginInitContext Context { get; private set; } = null!; // At least internal static property
private static IPublicAPI API => Context.API;

...
}
```

### Localized strings

With this toolkit, we can replace `Context.API.GetTranslation("flowlauncher_plugin_localization_demo_plugin_name")` with `Localize.flowlauncher_plugin_localization_demo_plugin_name()`.

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)`.

### Localized enums

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 localization support.
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.

```
[EnumLocalize] // Enable localization support
public enum DemoEnum
{
[EnumLocalizeKey("localize_key_1")] // Specific localization key
Value1,

[EnumLocalizeValue("localize_value_2")] // Specific localization value
Value2,

[EnumLocalizeKey("localize_key_3")] // If key and value both exist, will prefer localization key
[EnumLocalizeValue("localize_value_3")]
Value3,

[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_localization_demo_plugin_description))] // Use Localize class
Value4,
}
```

Then you can get `DemoEnumData` class.

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:

```
public List<DemoEnumData> AllDemoEnums { get; } = DemoEnumData.GetValues(); // ItemSource of ComboBox

public DemoEnum SelectedDemoEnum { get; set; } // SelectedValue of ComboBox
```

```
<ComboBox
DisplayMemberPath="Display"
ItemsSource="{Binding AllDemoEnums}"
SelectedValue="{Binding SelectedDemoEnum}"
SelectedValuePath="Value" />
```

If you want to update localization strings when culture info changes, you can call this function to update.

```
private void UpdateEnumDropdownLocalizations()
{
DemoEnumData.UpdateLabels(AllDemoEnums);
}
```
Loading