This repository has been archived by the owner on May 1, 2024. It is now read-only.
Replies: 1 comment 1 reply
-
Something like what you propose here would be great as an extension for all of the semi-dynamic things in current systems, like dark/light mode and font sizes for accessibility as well. I've done something a bit like this for dark/light mode handling, and putting it into the ViewModel via bindings works but feels like overkill. It would be much better if the framework would take care of it internally using a kind of app-wide binding mechanism. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm sort of spitballing, hence the length. I'd like to get some input, particularly from those who localize projects a lot.
I feel like Xamarin.Forms localization is tantalizingly close to being awesome, but for some reason, it's not quite there yet. The main issue I have is that resources are not automatically updated if I change the app culture during runtime. This can be problematic for an app that allows users to change language from the app. I'd love to know if we can improve on that. I'll try to explain my thought process. Please let me know if I'm misunderstanding anything along the way.
Currently:
We create
.resx
files to hold multiple sets of localized data (which is notably painful with an editor tool in VS for Mac). I'll focus on string use cases in particular here with the filenameAppResources
for clarity. On app start, the program gets the device locale, loads the correspondingAppResource
file (or fallback), and then the UI will display the strings referenced in the resource (e.g. by usingText="AppResources.TitleText"
). Now, if the user changes the language inside the app, which I would then use to trigger a change toCurrentUICulture
, the new correspondingAppResource
file does get loaded, but the UI display does not change.I guess this makes sense because we're just getting static resource values here, not using a binding. The pages will update if a page is newly loaded or the app is restarted (which I guess is like a mass page reload). However, I have some contrary opinions about this setup.
Now, I'd say the current system as described is functioning as intended at this point. See Xamarin.Forms String and Image Localization (Test Localization) for support of this. It seems the intention is really only to load localization resources based on device settings or CurrentUICulture on app start, but I'd argue that doesn't truly support a variety of localization needs and could be considered an incomplete system.
My Current Workaround:
I'm currently using a somewhat modified version of the system detailed in this blog post: Dynamically binding RESX Resources in Xamarin Forms I may not do this justice in a brief overview, but here it goes:
ResourceManager
where the current resource set is stored.ResourceManager.GetString(key, CurrentCultureInfo);
Text="{Binding MyResource[TitleText]}"
I recommend reading the post I linked for a clearer explanation. But it does work! With this system in place, my localized data is automatically updated and displayed right away because I'm explicitly using bindings. I don't need to be concerned with changing device settings, reloading all pages, or restarting the app.
Unfortunately, this may also come with the overhead of accessing an already loaded resource set and storing it in the view model of each page. If I'm understanding the way this works, then using this method to do runtime culture changes in a large app could be costly for both memory and performance if a lot of pages are loaded at once.
My Ideal System:
The localized resource files are already properly loaded with any culture change, so that work is done. My ideal setup after that would be that anything referencing the data (e.g.
Text="AppResources.TitleText"
) would also be aware of the loaded resource change and update its values without any of the extra work I've done above - sort of an auto-binding situation, I guess.Now, I realize that more dynamic bindings is a massive performance issue and highly recommended against, so you of course wouldn't want to do that when it isn't necessary (i.e. non-localized apps using resource files). In this case, an explicit user setting - I don't know,
localizedResx = true
? - would be more appropriate so the developer would have to actively enable the dynamic binding. Then we would need some kind of intermediary system that creates the dynamic connection to the loaded resource file and updates everything on a source change.I guess my big questions are - What kind of work would that entail? What roadblocks (if any) are in the way of us doing something like that? Is it even possible in the current system??
Beta Was this translation helpful? Give feedback.
All reactions