Last active
December 26, 2022 12:45
-
-
Save BrandonStudio/33a6e148147264f90eb7cdbf79786cce to your computer and use it in GitHub Desktop.
.NET MAUI Globalized String Markup Extension.
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
/// <summary> | |
/// Xaml extension providing globalized strings. | |
/// </summary> | |
/// <remarks> | |
/// Providing key itself if specified string is not found, | |
/// </remarks> | |
public class StringsExtension : IMarkupExtension<string> | |
{ | |
/// <summary> | |
/// Key of globalized strings. | |
/// </summary> | |
public string Key { get; set; } | |
/// <summary> | |
/// Culture in which strings is shown. | |
/// </summary> | |
/// <remarks> | |
/// Default is <see cref="CultureInfo.CurrentUICulture" />. | |
/// </remarks> | |
[TypeConverter(typeof(CultureInfoConverter))] | |
public CultureInfo Culture { get; set; } = CultureInfo.CurrentUICulture; | |
private static readonly ResourceManager s_resourceManager = new( | |
$"{nameof(ns)}.Resources.Strings", typeof(StringsExtension).Assembly); // Replace "ns" with your own project/namespace name | |
public string ProvideValue(IServiceProvider serviceProvider) => s_resourceManager.GetString(Key, Culture) ?? Key; | |
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) => ProvideValue(serviceProvider); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See Resources in text files.
Assuming files are organized like
where resource text files are named "Strings" and stored in
Resources
folder just as line 24.Make sure your resource text files ends with ".restext" and you add them to csproj:
See my example.