WinUI3Localizer icon indicating copy to clipboard operation
WinUI3Localizer copied to clipboard

Resource key without suffix ".content", ".text" etc.

Open Pietro228 opened this issue 1 year ago • 20 comments

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".

Pietro228 avatar Nov 23 '23 13:11 Pietro228

I can make it work by using a resource loader, but it doesn't update runtime and you have to restart the app :/

obrazek obrazek

Would it be possible to create something like this? It's really time consuming to use the suffixes

Pietro228 avatar Nov 23 '23 13:11 Pietro228

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");
}

AndrewKeepCoding avatar Nov 24 '23 00:11 AndrewKeepCoding

Thank you for your help, but it always returns "null" :/

I have only replaced the resourceLoader.GetString(Name); for your method. obrazek

Pietro228 avatar Nov 24 '23 10:11 Pietro228

I found the issue. It did not copy new string resource files.

This should be InstalledWidgets not MainWindow_InstalledWidgets.Content obrazek

Pietro228 avatar Nov 24 '23 17:11 Pietro228

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.

obrazek

Pietro228 avatar Nov 24 '23 18:11 Pietro228

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 obrazek

Pietro228 avatar Nov 24 '23 20:11 Pietro228

Thanks for the suggestion. I guess there are 2 considerable options here:

  1. Add l:Uids.Target:

    <NavigationViewItem l:Uids.Uid="SomeItem" l:Uids.Target="Content" />
    
  2. 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.

AndrewKeepCoding avatar Nov 27 '23 01:11 AndrewKeepCoding

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

Pietro228 avatar Nov 27 '23 01:11 Pietro228

Any news about this? :D

Pietro228 avatar Nov 29 '23 14:11 Pietro228

I'm not sure if I can find time to consider and implement this anytime soon. 😵

AndrewKeepCoding avatar Dec 01 '23 11:12 AndrewKeepCoding

That's OK :D For now I'll just prompt the user to restart the app.

Pietro228 avatar Dec 01 '23 15:12 Pietro228

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?

AndrewKeepCoding avatar Dec 02 '23 04:12 AndrewKeepCoding

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.

Pietro228 avatar Dec 02 '23 21:12 Pietro228

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')}" />

AndrewKeepCoding avatar Dec 03 '23 12:12 AndrewKeepCoding

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

Pietro228 avatar Dec 03 '23 17:12 Pietro228

Idk why, but VS says that it can't find my class, however it builds without any problems :D obrazek

That's why I look forward to your solution which wouldn't give me unnecessary errors

Pietro228 avatar Dec 03 '23 17:12 Pietro228

@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.

AndrewKeepCoding avatar Jan 15 '24 05:01 AndrewKeepCoding

Reopening this for v3.

AndrewKeepCoding avatar May 20 '24 01:05 AndrewKeepCoding

@AndrewKeepCoding Any news? 🤔

Pietro228 avatar Sep 10 '24 15:09 Pietro228

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.)

AndrewKeepCoding avatar Sep 13 '24 05:09 AndrewKeepCoding

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! :)

Pietro228 avatar Jan 22 '25 15:01 Pietro228

It works great! Exactly as I wanted. Thank you so much

Pietro228 avatar Feb 20 '25 16:02 Pietro228

Thanks @Pietro228 ! Now I just need to make time for v3.🙂

AndrewKeepCoding avatar Feb 21 '25 00:02 AndrewKeepCoding