Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pietro228 opened this issue Nov 23, 2023 · 20 comments · Fixed by #48
Open

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

Pietro228 opened this issue Nov 23, 2023 · 20 comments · Fixed by #48
Milestone

Comments

@Pietro228
Copy link

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
Copy link
Author

Pietro228 commented Nov 23, 2023

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

@AndrewKeepCoding
Copy link
Owner

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

@Pietro228
Copy link
Author

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

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

@Pietro228
Copy link
Author

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

This should be InstalledWidgets not MainWindow_InstalledWidgets.Content
obrazek

@Pietro228
Copy link
Author

Pietro228 commented Nov 24, 2023

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
Copy link
Author

Pietro228 commented Nov 24, 2023

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 Pietro228 reopened this Nov 24, 2023
@AndrewKeepCoding
Copy link
Owner

AndrewKeepCoding commented Nov 27, 2023

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.

@Pietro228
Copy link
Author

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
Copy link
Author

Any news about this? :D

@AndrewKeepCoding
Copy link
Owner

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

@Pietro228
Copy link
Author

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

@AndrewKeepCoding
Copy link
Owner

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?

@Pietro228
Copy link
Author

Pietro228 commented Dec 2, 2023

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.

@AndrewKeepCoding
Copy link
Owner

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

@Pietro228
Copy link
Author

Pietro228 commented Dec 3, 2023

I know, you already told me about it here #39 (comment). But I still need to restart the app to change the language

@Pietro228
Copy link
Author

Pietro228 commented Dec 3, 2023

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

@AndrewKeepCoding
Copy link
Owner

AndrewKeepCoding commented Jan 15, 2024

@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 AndrewKeepCoding closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2024
@AndrewKeepCoding
Copy link
Owner

Reopening this for v3.

@AndrewKeepCoding AndrewKeepCoding added this to the v3 milestone May 20, 2024
@Pietro228
Copy link
Author

@AndrewKeepCoding Any news? 🤔

@AndrewKeepCoding
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants