-
-
Notifications
You must be signed in to change notification settings - Fork 0
Improve Enum Localize Class Name & Add Useful Links and Documents #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
56e9550
Add useful links
Jack251970 2bfccc5
Add localization documents
Jack251970 b142344
Update localization-toolkit.md
Jack251970 bb14c8d
Update localization-toolkit.md
Jack251970 6981a8f
Update localization-toolkit.md
Jack251970 cc4405e
Rephrase documentation
Yusyuriv e73aea4
Improve enum class names
Jack251970 d997201
Improve code strings
Jack251970 231ca93
Correct generated enum data class name
Yusyuriv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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 depenedency 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 compabible with Flow 1.20.0 or higher) | ||
|
||
Jack251970 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
## 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 localiztion support. | ||
Jack251970 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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); | ||
} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.