Picker values localization #25329
-
What is a right way to localize picker values in MAUI? So far I have only one idea: Use LocalizationResourceManager.Maui. But for picker values it doesn't work when app localization has been changed until app restart. <Picker
x:Name="NativeLanguagePicker"
ItemsSource="{Binding NativeLanguageSetting.Options}"
SelectedItem ="{Binding NativeLanguageSetting.Picked, Mode=TwoWay}"
ItemDisplayBinding="{localization:TranslateBinding Localization, TranslateValue=True}"
Style="{DynamicResource SettingPicker}">
</Picker> Is there any way to do it right in MAUI? Because the whole app follows culture change and translates accordingly except of pickers. It looks like there should be a way to do it elegantly. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Interesting problem. I worked on TranslateBinding SirJohnK/LocalizationResourceManager.Maui#16 but if there's an issue with it, I can definite look at fixing it. But, outside of TranslateBinding, the main issue is will the Picker respond to MVVM changes on the ItemDisplayBinding whilst the Picker is active? Having an answer to that question will help narrow down the problem. If you're not sure what I'm talking about. I could find time to create a repro case myself, but, it would be helpful if you are able to do that investigative work. |
Beta Was this translation helpful? Give feedback.
-
I believe the real issue is:
To work around the problem, I moved the TranslateBinding to your view model, by changing the Picker.ItemDisplayBinding to ItemDisplayBinding="{Binding DisplayBinding}" and then implementing DisplayBinding in your view model: public BindingBase DisplayBinding { get; } = new Binding("Localization", converter: new TranslateConverter());
internal class TranslateConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> (value is string s) ? PickerLocalizationIssue.Resources.Localization.AppResources.ResourceManager.GetString(s) : null;
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotImplementedException();
} Whenever you change languages I call |
Beta Was this translation helpful? Give feedback.
-
Hi @stephenquan, just looked into your PR. Thank you for your time and effort! But when I run your PR on iOS device/emulator I see that pickers cannot pass initialization: If a go back to initial commit, I don't have this issue. Did you have successful test on your side? I wouldn't really want to waste your time to fix it. Since I don't have enough competency to make myself the workaround you've shared, I hoped miss something simple. Because the whole situation looks a bit strange: the whole app can be localized with a single line of code except of pickers. But I gues we have what we have, it's juts not implemented on the design level. |
Beta Was this translation helpful? Give feedback.
-
Hi @AncientLust, I tested mainly on Windows. I will run an iOS test when I get a chance. In the meantime, I reverted my code to one of my earlier versions which, hopefully, would be more easier to edit. Also, to improve localization support and to simplify the code-behind we have to write, I have raised two enhancements and their corresponding PR on the LocalizationResourceManager.Maui repo: |
Beta Was this translation helpful? Give feedback.
Hi @AncientLust, I tested mainly on Windows. I will run an iOS test when I get a chance. In the meantime, I reverted my code to one of my earlier versions which, hopefully, would be more easier to edit.
Also, to improve localization support and to simplify the code-behind we have to write, I have raised two enhancements and their corresponding PR on the LocalizationResourceManager.Maui repo: