AnyBar.Localization
0.0.2
dotnet add package AnyBar.Localization --version 0.0.2
NuGet\Install-Package AnyBar.Localization -Version 0.0.2
<PackageReference Include="AnyBar.Localization" Version="0.0.2" />
<PackageVersion Include="AnyBar.Localization" Version="0.0.2" />
<PackageReference Include="AnyBar.Localization" />
paket add AnyBar.Localization --version 0.0.2
#r "nuget: AnyBar.Localization, 0.0.2"
#:package AnyBar.Localization@0.0.2
#addin nuget:?package=AnyBar.Localization&version=0.0.2
#tool nuget:?package=AnyBar.Localization&version=0.0.2
AnyBar Localization Toolkit
Localization toolkit for AnyBar and its plugins, which helps AnyBar plugin developers make the localization process easier.
Getting Started
Install and reference AnyBar.Localization via NuGet.
Build Properties
These are properties you can configure in your .csproj file to customize the localization process. You can set them in the <PropertyGroup> section. For example, to set the AnyBarUseDependencyInjection property to true, add the following lines:
<PropertyGroup>
<AnyBarUseDependencyInjection>true</AnyBarUseDependencyInjection>
</PropertyGroup>
AnyBarUseDependencyInjection
This flag specifies whether to use dependency injection to obtain an IPublicAPI instance. The default is false.
- If set to
false, the main class (which must implementIPluginorIAsyncPlugin) must have aPluginInitContextproperty that is at leastinternal static. - If set to
true, you can access theIPublicAPIinstance viaPublicApi.Instanceusing dependency injection, and the main class does not need to include aPluginInitContextproperty.
Usage
Main Class
The main class must implement IPluginI18n.
If AnyBarUseDependencyInjection is false, include a PluginInitContext property, for example:
// Must implement IPluginI18n
public class Main : IPlugin, IPluginI18n
{
// Must be at least internal static
internal static PluginInitContext Context { get; private set; } = null!;
}
Localized Strings
You can simplify your code by replacing calls like:
Context.API.GetTranslation("AnyBar_Plugin_Localization_Demo_Plugin_Name")
with:
Localize.AnyBar_Plugin_Localization_Demo_Plugin_Name()
If your localization string uses variables, it becomes even simpler! From this:
string.Format(Context.API.GetTranslation("AnyBar_Plugin_Localization_Demo_Value_With_Keys"), firstName, lastName);
To this:
Localize.AnyBar_Plugin_Localization_Demo_Value_With_Keys(firstName, lastName);
If you would like to add summary for functions of localization strings, you need to comment strings in xaml files like this:
<system:String x:Key="AnyBar_Plugin_Localization_Demo_Plugin_Name">Demo</system:String>
Or if you would like to change the default types or names of variables in localization strings, you need to comment strings in xaml file like this:
<system:String x:Key="AnyBar_Plugin_Localization_Demo_Value_With_Keys">Demo {2:00}, {1,-35:D} and {0}</system:String>
Localized Enums
For enum types (e.g., DemoEnum) that need localization in UI controls such as combo boxes, use the EnumLocalize attribute to enable localization. For each enum field:
- Use
EnumLocalizeKeyto provide a custom localization key. - Use
EnumLocalizeValueto provide a constant localization string.
Example:
[EnumLocalize] // Enable localization support
public enum DemoEnum
{
// Specific localization key
[EnumLocalizeKey("localize_key_1")]
Value1,
// Specific localization value
[EnumLocalizeValue("This is my enum value localization")]
Value2,
// Key takes precedence if both are present
[EnumLocalizeKey("localize_key_3")]
[EnumLocalizeValue("Localization Value")]
Value3,
// Using the Localize class. This way, you can't misspell localization keys, and if you rename
// them in your .xaml file, you won't forget to rename them here as well because the build will fail.
[EnumLocalizeKey(nameof(Localize.Anybar_Plugin_Localization_Demo_Plugin_Description))]
Value4,
}
Then, use the generated DemoEnumLocalized class within your view model to bind to a combo box control:
// ComboBox ItemSource
public List<DemoEnumLocalized> AllDemoEnums { get; } = DemoEnumLocalized.GetValues();
// ComboBox SelectedValue
public DemoEnum SelectedDemoEnum { get; set; }
In your XAML, bind as follows:
<ComboBox
DisplayMemberPath="Display"
ItemsSource="{Binding AllDemoEnums}"
SelectedValue="{Binding SelectedDemoEnum}"
SelectedValuePath="Value" />
To update localization strings when the language changes, you can call:
DemoEnumLocalize.UpdateLabels(AllDemoEnums);
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.