This is a simple F# type provider to help with localizing Avalonia applications that are safe with respect to changes in the resource file.
The type provider reads an .axaml file containing a resource dictionary and provides a type with string properties for each of the string resources contained in the file.
Such a file would look as follows:
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=System.Runtime">
<system:String x:Key="Loc.WindowTitle">Application Main Window</system:String>
<system:String x:Key="Loc.ShowingXFiles.Format">Showing {0} files</system:String>
<system:String x:Key="Log.LoadingComplete">LoadingComplete</system:String>
</ResourceDictionary>The provided type Localizer has three parameters:
FileNameis the Avalonia XAML file used to determine the available localization string resources. The path must be relative to the code file in which the provided type is instantiated.ListStructuredetermines the type structure in which the resources are presented.ListStructure.Flatpresents all of the resources in a single flat list, with all dots removed from the names.- In the above example, the localization type would have the properties
LocWindowTitle,LocShowingXFilesFormat, andLogLoadingComplete.
- In the above example, the localization type would have the properties
ListStructure.Groupedgroups the resources by the part before the first dot, with all subsequent dots replaced by underscores. This allows for a better logical organization of a non-trivial number of string resources.- In the above example, the localization type would have two properties:
Locwith sub-propertiesWindowTitleandShowingXFiles_FormatLogwith sub-propertyLoadingComplete
- In the above example, the localization type would have two properties:
ReturnModedetermines whether the properties return the resource keys or the actual current resource values.ReturnMode.Keyscauses the resource keys to be returned.ReturnMode.Valuescauses the resource values to be returned.
The recommended parameterization is using ListStructure.Grouped and ReturnMode.Values:
type Loc = Localizer< @"Localization\LocStrings.axaml", ListStructure.Grouped, ReturnMode.Values >This is used as such:
let message = String.Format(Loc.Loc.ShowingXFiles_Format, numberOfFiles)When using ReturnMode.Keys, the actual resource values can be retrieved using the following function:
let locString key =
match Application.Current.TryGetResource(key, null) with
| true, resource -> resource :?> string
| false, _ -> failwith "Resource not found"With ReturnMode.Keys and e.g. ListStructure.Flat, the above usage then becomes:
let message = String.Format(locString Loc.LocShowingXFilesFormat, numberOfFiles)Building:
dotnet tool restore
dotnet paket update
dotnet build -c release
dotnet paket pack nuget --version 0.0.1