5
votes

I am trying to use .resx files for localization in xamarin.mac

Inside AppDelegate I changed current culture of the thread :

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

also I have two resource files inside my app:

enter image description here

But strings are always shown from the default resource file... Any solution?

I also use native language change, here is whole AppDelegate constructor :

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) doesn't allow me to add resource.ru.resx if I have resource.ru in my project , kinda bug !

I have created resource-ru.resx file and than renamed it .

2

2 Answers

1
votes

You need to set the app culture after retrieving it from the OS, something like this (warning, this is from Xamarin iOS, but for the documentation it works the same way, if not advice me and I will delete the post):

    //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
votes

Likely you are running into

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

You can work around it using a custom msbuild step. Shown here (due to length):

<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>

A full sample can be found here: https://github.com/xamarin/mac-samples/tree/master/XMLocalizationSample