[MonoTouch] Multi-lingual

Karl Heinz Brehme Arredondo karl at e-magesolutions.com
Fri Oct 7 11:51:41 EDT 2011


If you just need languages without crossing platforms, the cocoa native way
is good.

In my case, where I'm moving from Windows Mobile to all new devices
nowadays, the best thing would be .NET ways of localization, with resx for
forms and strings. Specially because iOS do not support es-MX, es-CH,
es-VE... You just have an "es" folder for generic spanish. Indeed, on
windows visual studio designer you can put a form field and label that has
some text, size, and also visibility, configured automatically when you
change es-MX to es-CH to .NET handles it to you (yes since old compact
framework). So I need this functionality.

The thing is, this do not execute correctly yet on iOS, and you just get
strings, no integration with Interface Builder.

So what I made to try to use the best things and have cross platform
compatibility was a mix of all this:
- Login screen, that do not have database, uses the native platform way for
localization, no need to es-MX and es-CH for user, password and OKŠ
- After it subscribes to the database, downloading tables and data, it
download a Library table, that on server is has all strings with localized
text and all controls.properties as RESX do on Windows.Forms
- All UI made on Interface Builder if it don't find on runtime a localized
text, it gets the default defined on IB. If it find what is configured on
CurrentCulture, so it shows the localized text, hide controls or not, change
size an position, as on Windows.Forms, I mean, in Brazil person ID called RG
has 2 common info, like a number and another thing, like
Region/Country/State. On Chile they have just RUT, a number. So when
MonTouch will show the view, it id find on sqlite the localized library info
for each control, it will show on chile RUT on ID label and will hide the
second ComboBox (ok UIPicker or whatever) and if you change iOS to pr-BR it
will show RG on ID label and 2 fields. All being automatic.
- All text like messages, or whatever that is write on code, wil be get with
something like Library.GetText("TextID") to get localized text

Now this can run on web (ASP.NET with SQL Server or whatever you want),
Silverlight, Windows.Forms (like Windows Mobile), Windows Phone and AndroidŠ
Ok, I need to discard native Windows localization but it runs in the same
way, and with de advantage if I need to now put italian, you just need to
record that on server, than all devices on next sync will get italian
library without recompilation. Also translators can enter on browser or
silverlight to review or insert translations on the fly and all version will
be translated/corrected on next sync (windows, androis and iOS).

Karl

From:  Göran Halvarsson <goranhalvarsson at gmail.com>
Date:  Fri, 7 Oct 2011 15:35:06 +0200
To:  Mittchel Van Vliet <mittchel at gmail.com>
Cc:  "monotouch at lists.ximian.com" <monotouch at lists.ximian.com>
Subject:  Re: [MonoTouch] Multi-lingual

Hello

I made something similar, but in stead of using DB I used good old
localizable folders:
sv.proj
--Localizable.strings
en.proj
--Localizable.strings

In the "swedish" Localizable.strings:
"Offers" = "Erbjudande";
"Barcode" = "Streckkod"

Display a label:
Locale.GetText ("Offers");

Switch Language, let say you have a settings view where you select a
language:
Locale.ChangeCulture(MyApp.Instance.CurrentUser.Culture);

Don't forget to reload your view after the "switch". I did a flip
animation to spice it up...
Task.Factory.StartNew( () => ReloadView() );


private void ReloadView()
{
InvokeOnMainThread(delegate {
//Flip the view
UIView.BeginAnimations("Flipper");
UIView.SetAnimationDuration(1.25);
UIView.SetAnimationCurve(UIViewAnimationCurve.EaseInOut);
UIView.SetAnimationTransition
(UIViewAnimationTransition.FlipFromRight, _tabbarController.View ,
true);
_tabbarController.ViewWillDisappear(true);
_tabbarController.ViewWillAppear(true);
UIView.CommitAnimations();
});
}


The class:
public static class Locale
{
public static CultureInfo DefaultCultureInfo = new CultureInfo("sv-SE");
private static CultureInfo _currentCultureInfo = DefaultCultureInfo;
public static void ChangeCulture(string cultureInfo)
{
_currentCultureInfo = new CultureInfo(cultureInfo);
}
public static CultureInfo GetPhoneCultureInfo()
{
return new 
CultureInfo(NSLocale.CurrentLocale.LocaleIdentifier.Replace("_","-"));
}
public static string GetText (string str)
{
string path = null;
path = 
NSBundle.MainBundle.PathForResource(_currentCultureInfo.TwoLetterISOLanguage
Name.ToLower(),"lproj");
NSBundle languageBundle = NSBundle.FromPath(path);
return languageBundle.LocalizedString(str,string.Empty);
}

public static string Format (string fmt, params object [] args)
{
fmt = NSBundle.MainBundle.LocalizedString(fmt,"");
return String.Format (fmt, args);
}


    public static CultureInfo GetCultureInfo(string countryCode)
    {
        CultureInfo currentCulture = null;

        foreach (CultureInfo ci in
CultureInfo.GetCultures(CultureTypes.AllCultures))
        {

            if (ci.Name.ToLower().Contains(countryCode.ToLower()))
            {

                currentCulture = ci;
                break;
            }



        }

        return currentCulture;

    }
}
That's it really...
(I must say that I just love the System.Threading.Task.Factory:
http://www.codethinked.com/net-40-and-systemthreadingtasks)

I hope it will help you.

Br
Göran
_______________________________________________
MonoTouch mailing list
MonoTouch at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/monotouch



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/monotouch/attachments/20111007/b435aef8/attachment.html 


More information about the MonoTouch mailing list