WinUI3Localizer
WinUI3Localizer copied to clipboard
Resource key without suffix ".content", ".text" etc.
Hi,
is there any way to use keys without the suffix? I would like to use a highly used word in more controls, but without creating two variants ".Content" and ".Text".
I can make it work by using a resource loader, but it doesn't update runtime and you have to restart the app :/
Would it be possible to create something like this? It's really time consuming to use the suffixes
Hi @Pietro228 !
You can get a localized string using the GetLocalizedString method:
string localizedText = WinUI3Localizer.Localizer.Get().GetLocalizedString("SomeUid");
and if you need to update the text when the selected language is changed, you can use the OnLanguageChanged event:
public SomePage()
{
InitializeComponent();
WinUI3Localizer.Localizer.Get().LanguageChanged += OnLanguageChanged;
}
private void OnLanguageChanged(object? sender, LanguageChangedEventArgs e)
{
string localizedText = WinUI3Localizer.Localizer.Get().GetLocalizedString("SomeUid");
}
Thank you for your help, but it always returns "null" :/
I have only replaced the resourceLoader.GetString(Name); for your method.
I found the issue. It did not copy new string resource files.
This should be InstalledWidgets not MainWindow_InstalledWidgets.Content
This condition is not that good for prototyping, because it doesn't copy your resource files. I wasn't paying attention to that code when I was trying your plugin, but now when I was looking at the code, I found it. You should mention it in your usage documentation.
Btw. Is there any way that you could make it so that I can specify which property should receive my translated string? Something like this: <NavigationViewItem l:Uids.Uid="Dashboard" l:Uids.Target="Content"/>
I'm asking you because using MarkupExtension doesn't support updating value and it always shows me an error, but it builds without any problems :D
Thanks for the suggestion. I guess there are 2 considerable options here:
-
Add
l:Uids.Target:<NavigationViewItem l:Uids.Uid="SomeItem" l:Uids.Target="Content" /> -
Make it work within
l:Uids.Uid:<NavigationViewItem l:Uids.Uid="SomeItem.Content" />This was actually suggested a very long time ago but wasn't implemented. Microsoft/xaml-standard#199
I'm afraid, I'm going to need time to think about this.
The first one would be easier to understand, but the second one is shorter and faster to write.
My opinion is that it's better to go with the 2nd option.
Btw. Interesting post. I didn't know that some people already wanted to make it easier
Any news about this? :D
I'm not sure if I can find time to consider and implement this anytime soon. 😵
That's OK :D For now I'll just prompt the user to restart the app.
I don't understand. If you have access to the target element, you should be able to localize strings. The new feature about extending the x:Uid is a different thing. Can you be more specific about your localization again?
I'm able to localize them, but only by naming the keys like this Dashboard.Content = Name.Target. I don't want to inlude target in a key, so for now, while I'm waiting for your implementation of the extended x:Uid, I'll be using my own solution using the ResourceString despite the need to restart the app for language changes to take effect.
By the way, you can still use the WinUI3Localizer in your markup extension:
[MarkupExtensionReturnType(ReturnType = typeof(string))]
public class LocalizedString : MarkupExtension
{
public string Key { get; set; } = string.Empty;
protected override object ProvideValue() => Localizer.Get().GetLocalizedString(Key);
}
or you can also do this with a simple helper class:
public static class WinUI3LocalizerHelper
{
public static string GetLocalizedString(string key)
{
return Localizer.Get().GetLocalizedString(key);
}
}
<TextBlock Text="{x:Bind local:WinUI3LocalizerHelper.GetLocalizedString('SomeKey')}" />
I know, you already told me about it here https://github.com/AndrewKeepCoding/WinUI3Localizer/issues/39#issuecomment-1825012916. But I still need to restart the app to change the language
Idk why, but VS says that it can't find my class, however it builds without any problems :D
That's why I look forward to your solution which wouldn't give me unnecessary errors
@Pietro228 In order to keep consistentcy with x:Uid, I'm afraid I'm going to go with the 3rd option which is leave it as it is. I hope you understand.
Reopening this for v3.
@AndrewKeepCoding Any news? 🤔
Sorry for the delay. I just published v2.3.0-alpha. Can you give it a try if it works for your requirements? (Make sure to check the Include prerelease checkbox.)
I will work on this project again in a month, so I'll check it and tell you if it works. Thank you so much for implementing it! :)
It works great! Exactly as I wanted. Thank you so much
Thanks @Pietro228 ! Now I just need to make time for v3.🙂