使用.NET Xamarin的iOS本地化 [英] Xamarin iOS localization using .NET

查看:230
本文介绍了使用.NET Xamarin的iOS本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用.NET本地化便携式类库了Xamarin的iOS / Android项目。我遵循的步骤为:

I'm trying to use .NET localization in a portable class library for a Xamarin iOS/Android project. I've followed the steps at:

如何使用定位在C#

和有code看起来如下:

And have code which looks as follows:

string sText = strings.enter_movie_name;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
sText = strings.enter_movie_name;
lblEnterMovieName.Text = sText;

我也试过:

ResourceManager resman = new ResourceManager(typeof(MyApplication.strings));
string sText = resman.GetString("enter_movie_name", new CultureInfo("fr"));

我创建strings.resx与enter_movie_name等于输入电影名称:和strings.fr.resx等于恩特雷里奥斯洛杉矶电影名称:

I've created strings.resx with enter_movie_name equal to "Enter movie name:" and strings.fr.resx equal to "Entre la movie name:"

不过,STEXT最终总是为输入电影名称:。我似乎无法获得恩特雷里奥斯洛杉矶电影名称:版本出来

However, sText always ends up as "Enter movie name:". I can't seem to get the "Entre la movie name:" version out.

我也看了后在 Xamarin跨平台的本地化,但不能工作了。我也不想用特定的iOS版本本地化的Xamarin.iOS 的我倒是希望能够得到我的琴弦,从一个独立于平台的库。

I also saw the post at Xamarin cross platform localization but couldn't work it out. I also don't want to use the iOS specific version at Localization on Xamarin.iOS as I'd like to be able to get at my strings from a platform independent library.

任何人都可以指出我要去哪里错了?

Can anyone point out where I'm going wrong?

推荐答案

我创建了一个有点丑陋的解决方案,但它的工作原理。我所做的是由一个叫我可移植类库(PCL),并在该名为创建的文件串文件夹:

I've created a bit of an ugly solution, but it works. What I've done is made a folder called 'strings' in my Portable Class Library (PCL) and inside that created files called:

  • strings.resx
  • strings_fr.resx
  • strings_ja_JP.resx

我给自己定的所有的这些作为嵌入的资源自定义工具,ResXFile codeGenerator。这意味着我必须为每种语言单独的资源DLL。

I've set all of these as Embedded Resource with custom tool as ResXFileCodeGenerator. This means I have a separate resource DLL for each language.

在iOS的,我可以再拿到现场致电:

In iOS I can then get the locale by calling:

string sLocale = NSLocale.CurrentLocale.LocaleIdentifier;

我会想有一个Android相当于使用地点,但我不知道这是什么是。

I would guess there's an Android equivalent using Locale but I don't know what it is.

这让我像Ja_JP表示或者fr_FR目录或EN_GB的字符串(注意他们是下划线,而不是破折号)。然后我用用下面的静态类我创建获取合适的资源管理器,并从中获取字符串。

This gives me a string like "ja_JP" or "fr_FR" or "en_GB" (note they're underscores, not dashes). I then use this with the following static class I created to retrieve an appropriate resource manager and get the string from it.

如果给定的语言环境aa_BB首先查找strings_aa_BB,然后strings_aa,然后字符串。

If given a locale aa_BB it first looks for strings_aa_BB, then for strings_aa, then for strings.

public static class Localise
{
    private const string STRINGS_ROOT = "MyPCL.strings.strings";

    public static string GetString(string sID, string sLocale)
    {
        string sResource = STRINGS_ROOT + "_" + sLocale;
        Type type = Type.GetType(sResource);
        if (type == null)
        {
            if (sLocale.Length > 2) {
                sResource = STRINGS_ROOT + "_" + sLocale.Substring(0, 2); // Use first two letters of region code
                type = Type.GetType(sResource);
            }
        }
        if (type == null) {
            sResource = STRINGS_ROOT;
            type = Type.GetType(sResource);
            if (type == null)
            {
                System.Diagnostics.Debug.WriteLine("No strings resource file when looking for " + sID + " in " + sLocale);
                return null; // This shouldn't ever happen in theory
            }
        }
        ResourceManager resman = new ResourceManager(type);
        return resman.GetString(sID);
    }
}

如何,这将被使用(参照上述$ C $三)的一个例子是:

An example of how this would be used (referring to the above code) would be:

string sText = Localise.GetString("enter_movie_name", sLocale);
lblEnterMovieName.Text = sText;

这方面的一个显著的缺点是,所有的观点都需要有自己的文字设定编程,但确实有翻译可以做一次,然后重复使用在许多平台上的上涨空间。它们也仍然独立于主code在自己的DLL,因此可单独进行重新编译如果必要的。

A significant downside of this is that all views will need to have their text set programatically, but does have the upside that the translations can be done once and then reused on many platforms. They also remain separate from the main code in their own DLLs and therefore can be recompiled individually if necessary.

这篇关于使用.NET Xamarin的iOS本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆