Localization for mobile cross platform using xamarin and issue with iOS only

本小妞迷上赌 提交于 2019-12-09 04:39:07

问题


I have a project in Xamarin which targets Android, iOS and windows phone. I used core (PCL library) to share common code between different platform. I added Resource files (.net resource) .Resx in my core library and to read the culture specific string i used following code snippet in one of my ViewModel:

public string GetString() 
{  
    // CommonResources is the name of my resource file   
    ResourceManager resManager = new ResourceManager(typeof(CommonResources));   
    return resManager.GetString("LoginLabel",CultureInfo.CurrentCulture); 
}

"LoginLabel" is my resource key and its value is "Sign in" (in english) and "inloggen" in dutch.

I created two resource files CommonResources for English and dutch in my PCL project. CommonResources.resx
CommonResources.nl-NL.resx

in android, iOS and windows phone, I set culture as follow:

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl-NL");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl-NL");

This works fine for Android and windows phone.

But for iOS it does not work. It always return resource string from English file. The culture is properly set and it display in debug mode. but somehow it is not able to load the resource string from the dutch resource.

So the question is, it is possible to localize string(.Net way) using PCL for all platform? anyone has any idea? Thanks in advance.


回答1:


For localization on our Xamarin projects I've used the Multilingual App Toolkit (detailed here) from Microsoft and T4 templates to transform the output from the toolkit to useable formats for Android and iOS.

This tutorial has a fantastic overview of the process and it's based on this code project.




回答2:


Either:

  • You're not actually setting the CurrentCulture on iOS.
  • You're setting the CurrentCulture on thread A, and retrieving it on thread B.

Try this:

public string GetString() 
{
    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl-NL");
    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl-NL");

    // CommonResources is the name of my resource file   
    ResourceManager resManager = new ResourceManager(typeof(CommonResources));   
    return resManager.GetString("LoginLabel",CultureInfo.CurrentCulture); 
}



回答3:


Do you test it on a real device? In the simulator it is not possible to change the culture. I had a similar issue with my language files.



来源:https://stackoverflow.com/questions/23004414/localization-for-mobile-cross-platform-using-xamarin-and-issue-with-ios-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!