Xamarin.mac资源本地化不会因当前文化而改变

时间:2016-10-21 16:35:47

标签: c# localization resx culture xamarin.mac

我正在尝试使用.resx文件在xamarin.mac中进行本地化

在AppDelegate中我改变了线程的当前文化:

System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo ("ru");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo ("ru");

我的应用程序中还有两个资源文件:

enter image description here

但字符串始终显示在默认资源文件中...任何解决方案?

我也使用本地语言更改,这里是整个AppDelegate构造函数:

public AppDelegate ()
        {

            string [] lang = { "ru", "en" };
            NSUserDefaults.StandardUserDefaults.SetValueForKey (NSArray.FromObjects (lang), (Foundation.NSString)"AppleLanguages");
            NSUserDefaults.StandardUserDefaults.Synchronize ();

            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo ("ru");
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo ("ru");

            Resources.Culture = GetCurrentCultureInfo ();

        }
BTW,xamarin studio 6.1.1(build 15)不允许我添加resource.ru.resx,如果我在我的项目中有resource.ru,有点错误!

我创建了resource-ru.resx文件,然后重命名了它。

2 个答案:

答案 0 :(得分:1)

您需要在从操作系统中检索后设置应用程序文化,类似这样(警告,这是来自Xamarin iOS,但对于文档,它的工作方式相同,如果没有建议我,我将删除帖子) :

    //Add this somewhere
    public System.Globalization.CultureInfo GetCurrentCultureInfo ()
    {
        var netLanguage = "en";

        if (NSLocale.PreferredLanguages.Length > 0) {
            var pref = NSLocale.PreferredLanguages [0];
            netLanguage = pref.Replace ("_", "-"); // turns en_US into en-US
        }

        try
        {
            return new System.Globalization.CultureInfo(netLanguage);
        }catch{

            try{

                return new System.Globalization.CultureInfo(netLanguage.Substring(0, netLanguage.IndexOf("-")));

            }
            catch{

                return new System.Globalization.CultureInfo("en");

            }

        }
    }

    //And finally add this at the start of the app
    AppResources.Culture = GetCurrentCultureInfo();

答案 1 :(得分:0)

可能你正在遇到

https://bugzilla.xamarin.com/show_bug.cgi?id=45696

您可以使用自定义msbuild步骤解决此问题。这里显示(由于长度):



<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
	<PropertyGroup>
	    <CreateAppBundleDependsOn>$(CreateAppBundleDependsOn);WorkAroundLocalizationBug-ES;WorkAroundLocalizationBug-FR</CreateAppBundleDependsOn>
	</PropertyGroup>

	<Target Name="WorkAroundLocalizationBug-ES" Inputs="$(OutputPath)es/XMLocalizationSample.resources.dll" Outputs="$(AppBundleDir)/Contents/MonoBundle/es/XMLocalizationSample.resources.dll">
	    <Copy SourceFiles="$(OutputPath)es/XMLocalizationSample.resources.dll" DestinationFiles="$(AppBundleDir)/Contents/MonoBundle/es/XMLocalizationSample.resources.dll" />
	</Target>
	<Target Name="WorkAroundLocalizationBug-FR" Inputs="$(OutputPath)fr/XMLocalizationSample.resources.dll" Outputs="$(AppBundleDir)/Contents/MonoBundle/fr/XMLocalizationSample.resources.dll">
	    <Copy SourceFiles="$(OutputPath)fr/XMLocalizationSample.resources.dll" DestinationFiles="$(AppBundleDir)/Contents/MonoBundle/fr/XMLocalizationSample.resources.dll" />
	</Target>
</Project>
&#13;
&#13;
&#13;

可在此处找到完整示例:https://github.com/xamarin/mac-samples/tree/master/XMLocalizationSample