Is it possible to use localization from web service?

MakMak INMember ✭✭

Is it possible to get language resource files from web service and use them in apps at run time based on local?
The benefit is that strings can be changed anytime on web and reflect in apps without the need of publishing the app again.

Or even is it possible to use resource files from storage without adding them in assembly?

If anybody has tried such a solution, I request the suggestions.

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    You could get the user's language following this thread: https://stackoverflow.com/a/27231721/8354952.
    Then submit a request to your web service with this language parameter via rest api. You could also include other parameters such as where to display this string so that you will get the corresponding string from your service.
    But I really don't recommend you to do that. because when the user's network is not good enough to access these strings, the interface will be messed up.
    Sometimes, publish a new version is necessary. It could tell users you are attempting to update your projects recently and your app is active.
    Try to use this feature to implement the localization: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/

  • MakMak INMember ✭✭

    @LandLu , Thank you for the response.
    I have service ready for getting language resources in json/xml format. And I can easily get it from app using rest. The problem is how to inject it instead of resx files key/values. Is it possible to customize the existing localization functionality in xamarin forms? Is it possible to use key/value from external json/xml file which is not part of the assembly?

    I am very much agree that due to network issues it can create problems, but you can understand developers can't take all the decisions. :smile: And I think once we get it, it can be stored somewhere and used if service is not available next time.

  • MakMak INMember ✭✭

    @RHudson , can you please help me to understand where did you manage loading the translations from web api in app. Because I use a property from user details to choose language and load specific translations. But till then my first page is already loaded!! Also please let me know do you store those translations in files/database in phone? I'll really appreciate your help.

  • RHudsonRHudson CAMember ✭✭✭

    @Mak

    I use Akavache in this as well as projects for storing data. There are tons of threads on this
    https://github.com/reactiveui/Akavache

    Treat the translation data like any other data.

    Don't be confused by the "static" in "{x:Static local:Translator.Foo}"
    This doesn't mean the translations are permanent and unchangeable. The Translator class is static. But its properties, ie the translated values, can be whatever you want. They can be loaded dynamically at runtime.

    Do something like this...
    Display a splash page
    Read your user's language preference
    Fetch the strings from Akavache, if it's empty (first time using this language) download from the web API

    When the translation strings are loaded, display your main nav page

Sign In or Register to comment.