官網(wǎng)看到關(guān)于Forms應(yīng)用程序國(guó)際化的相關(guān)內(nèi)容,便一邊練習(xí)一邊總結(jié)一下,雖然這部分知識(shí)點(diǎn)很可能永遠(yuǎn)用不到!!!
原文地址:https://developer.xamarin.com/zh-cn/guides/xamarin-forms/advanced/localization/
Xamarin.Forms PCL 國(guó)際化
Forms PCL中的國(guó)際化借助資源文件(RESX)實(shí)現(xiàn)。
準(zhǔn)備資源文件
在可移植項(xiàng)目中添加資源文件:
會(huì)在項(xiàng)目中生成如下兩個(gè)文件:
同時(shí)添加Resources.en.resx
文件,提供英文環(huán)境下使用的字符串。記得修改文件的生成操作為EmbeddedResource
。
Xamarin Studio添加名稱為Resources.en資源文件時(shí)提示Resources文件已經(jīng)存在,此時(shí)可以先添加名稱為Resources1的資源文件,然后修改Resources1.resx和Resources1.Designer.resx文件的名稱為Resources.en.resx和Resources.en.Designer.resx
Designer.cs文件不做修改,編輯.resx文件。
Resources.resx 添加如下data元素:
<data name="home" xml:space="preserve">
<value>首頁(yè)</value>
<comment>home page title</comment>
</data>
Resources.en.resx 添加如下data元素:
<data name="home" xml:space="preserve">
<value>Home</value>
<comment>home page title</comment>
</data>
生成項(xiàng)目Designer.cs文件生成如下代碼:
為了獲取name為home對(duì)應(yīng)的值需要調(diào)用如下代碼
Resource.Resources.ResourceManager.GetString("home")
其中Resource
為resx文件所在文件夾,Resources
為根據(jù)文件名生成的類。
簡(jiǎn)化資源文件中字符串資源的獲取,resx文件右鍵—>屬性。
修改自定義工具對(duì)應(yīng)的屬性值:ResXFileCodeGenerator
修改為PublicResXFileCodeGenerator
。
ResXFileCodeGenerator,PublicResXFileCodeGenerator,他們的區(qū)別是前者生成的Internal的,后者生成的是Public的資源。
重新生成項(xiàng)目,此時(shí)可以通過如下代碼Resource.Resources.home
獲取home對(duì)應(yīng)的值。
確定當(dāng)前運(yùn)行平臺(tái)的語言環(huán)境
Forms本身并沒有提供獲取運(yùn)行平臺(tái)語言環(huán)境的功能,我們需要借助dependency service 實(shí)現(xiàn)。
在可移植項(xiàng)目中定義如下接口:
/// <summary>
/// Implementations of this interface MUST convert iOS and Android
/// platform-specific locales to a value supported in .NET because
/// ONLY valid .NET cultures can have their RESX resources loaded and used.
/// </summary>
/// <remarks>
/// Lists of valid .NET cultures can be found here:
/// http://www.localeplanet.com/dotnet/
/// http://www.csharp-examples.net/culture-names/
/// You should always test all the locales implemented in your application.
/// </remarks>
public interface ILocalize
{
/// <summary>
/// This method must evaluate platform-specific locale settings
/// and convert them (when necessary) to a valid .NET locale.
/// </summary>
CultureInfo GetCurrentCultureInfo();
/// <summary>
/// CurrentCulture and CurrentUICulture must be set in the platform project,
/// because the Thread object can't be accessed in a PCL.
/// </summary>
void SetLocale(CultureInfo ci);
}
同時(shí)定義幫助類PlatformCulture
:
/// <summary>
/// Helper class for splitting locales like
/// iOS: ms_MY, gsw_CH
/// Android: in-ID
/// into parts so we can create a .NET culture (or fallback culture)
/// </summary>
public class PlatformCulture
{
public PlatformCulture(string platformCultureString)
{
if (String.IsNullOrEmpty(platformCultureString))
throw new ArgumentException("Expected culture identifier", "platformCultureString"); // in C# 6 use nameof(platformCultureString)
PlatformString = platformCultureString.Replace("_", "-"); // .NET expects dash, not underscore
var dashIndex = PlatformString.IndexOf("-", StringComparison.Ordinal);
if (dashIndex > 0)
{
var parts = PlatformString.Split('-');
LanguageCode = parts[0];
LocaleCode = parts[1];
}
else
{
LanguageCode = PlatformString;
LocaleCode = "";
}
}
public string PlatformString { get; private set; }
public string LanguageCode { get; private set; }
public string LocaleCode { get; private set; }
public override string ToString()
{
return PlatformString;
}
}
在App類中構(gòu)造方法中添加如下代碼,如果當(dāng)前運(yùn)行系統(tǒng)為ios或android需要獲取當(dāng)前系統(tǒng)對(duì)應(yīng).net表示的語言環(huán)境并設(shè)置為當(dāng)前的語言環(huán)境:
if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
{
var localize = DependencyService.Get<ILocalize>();
var ci = localize.GetCurrentCultureInfo();
//Resource.Resources.Culture = ci;// set the RESX for resource localization
localize.SetLocale(ci); // set the Thread for locale-aware methods
}
ios下 ILocalize 實(shí)現(xiàn)代碼
[assembly: Xamarin.Forms.Dependency(typeof(demo.iOS.Localize))]
namespace demo.iOS
{
public class Localize : ILocalize
{
public void SetLocale(CultureInfo ci)
{
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
Console.WriteLine("CurrentCulture set: " + ci.Name);
}
public CultureInfo GetCurrentCultureInfo()
{
var netLanguage = "en";
if (NSLocale.PreferredLanguages.Length > 0)
{
var pref = NSLocale.PreferredLanguages[0];
netLanguage = iOSToDotnetLanguage(pref);
}
// this gets called a lot - try/catch can be expensive so consider caching or something
System.Globalization.CultureInfo ci = null;
try
{
ci = new System.Globalization.CultureInfo(netLanguage);
}
catch (CultureNotFoundException e1)
{
// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)
// fallback to first characters, in this case "en"
try
{
var fallback = ToDotnetFallbackLanguage(new PlatformCulture(netLanguage));
Console.WriteLine(netLanguage + " failed, trying " + fallback + " (" + e1.Message + ")");
ci = new System.Globalization.CultureInfo(fallback);
}
catch (CultureNotFoundException e2)
{
// iOS language not valid .NET culture, falling back to English
Console.WriteLine(netLanguage + " couldn't be set, using 'en' (" + e2.Message + ")");
ci = new System.Globalization.CultureInfo("en");
}
}
return ci;
}
string iOSToDotnetLanguage(string iOSLanguage)
{
Console.WriteLine("iOS Language:" + iOSLanguage);
var netLanguage = iOSLanguage;
//certain languages need to be converted to CultureInfo equivalent
switch (iOSLanguage)
{
case "ms-MY": // "Malaysian (Malaysia)" not supported .NET culture
case "ms-SG": // "Malaysian (Singapore)" not supported .NET culture
netLanguage = "ms"; // closest supported
break;
case "gsw-CH": // "Schwiizertüütsch (Swiss German)" not supported .NET culture
netLanguage = "de-CH"; // closest supported
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
}
Console.WriteLine(".NET Language/Locale:" + netLanguage);
return netLanguage;
}
string ToDotnetFallbackLanguage(PlatformCulture platCulture)
{
Console.WriteLine(".NET Fallback Language:" + platCulture.LanguageCode);
var netLanguage = platCulture.LanguageCode; // use the first part of the identifier (two chars, usually);
switch (platCulture.LanguageCode)
{
//
case "pt":
netLanguage = "pt-PT"; // fallback to Portuguese (Portugal)
break;
case "gsw":
netLanguage = "de-CH"; // equivalent to German (Switzerland) for this app
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
}
Console.WriteLine(".NET Fallback Language/Locale:" + netLanguage + " (application-specific)");
return netLanguage;
}
}
}
Android下 ILocalize 實(shí)現(xiàn)代碼
[assembly: Xamarin.Forms.Dependency(typeof(demo.Droid.Localize))]
namespace demo.Droid
{
public class Localize : ILocalize
{
public void SetLocale(CultureInfo ci)
{
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
Console.WriteLine("CurrentCulture set: " + ci.Name);
}
public CultureInfo GetCurrentCultureInfo()
{
var netLanguage = "en";
var androidLocale = Java.Util.Locale.Default;
netLanguage = AndroidToDotnetLanguage(androidLocale.ToString().Replace("_", "-"));
// this gets called a lot - try/catch can be expensive so consider caching or something
System.Globalization.CultureInfo ci = null;
try
{
ci = new System.Globalization.CultureInfo(netLanguage);
}
catch (CultureNotFoundException e1)
{
// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)
// fallback to first characters, in this case "en"
try
{
var fallback = ToDotnetFallbackLanguage(new PlatformCulture(netLanguage));
Console.WriteLine(netLanguage + " failed, trying " + fallback + " (" + e1.Message + ")");
ci = new System.Globalization.CultureInfo(fallback);
}
catch (CultureNotFoundException e2)
{
// iOS language not valid .NET culture, falling back to English
Console.WriteLine(netLanguage + " couldn't be set, using 'en' (" + e2.Message + ")");
ci = new System.Globalization.CultureInfo("en");
}
}
return ci;
}
string AndroidToDotnetLanguage(string androidLanguage)
{
Console.WriteLine("Android Language:" + androidLanguage);
var netLanguage = androidLanguage;
//certain languages need to be converted to CultureInfo equivalent
switch (androidLanguage)
{
case "ms-BN": // "Malaysian (Brunei)" not supported .NET culture
case "ms-MY": // "Malaysian (Malaysia)" not supported .NET culture
case "ms-SG": // "Malaysian (Singapore)" not supported .NET culture
netLanguage = "ms"; // closest supported
break;
case "in-ID": // "Indonesian (Indonesia)" has different code in .NET
netLanguage = "id-ID"; // correct code for .NET
break;
case "gsw-CH": // "Schwiizertüütsch (Swiss German)" not supported .NET culture
netLanguage = "de-CH"; // closest supported
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
}
Console.WriteLine(".NET Language/Locale:" + netLanguage);
return netLanguage;
}
string ToDotnetFallbackLanguage(PlatformCulture platCulture)
{
Console.WriteLine(".NET Fallback Language:" + platCulture.LanguageCode);
var netLanguage = platCulture.LanguageCode; // use the first part of the identifier (two chars, usually);
switch (platCulture.LanguageCode)
{
case "gsw":
netLanguage = "de-CH"; // equivalent to German (Switzerland) for this app
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
}
Console.WriteLine(".NET Fallback Language/Locale:" + netLanguage + " (application-specific)");
return netLanguage;
}
}
}
代碼設(shè)置Page的Title屬性Title = Resource.Resources.home;
。
更改系統(tǒng)語言環(huán)境會(huì)有如下顯示效果:
Doesn't work in DEBUG mode (Android only)
If the translated strings are working in your RELEASE Android builds but not while debugging, right-click on the Android Project and select Options > Build > Android Build and ensure that the Fast assembly deployment is NOT ticked. This option causes problems with loading resources and should not be used if you are testing localized apps.
XAML中實(shí)現(xiàn)國(guó)際化
很多時(shí)候我們會(huì)直接在Xaml中設(shè)置文本值的顯示,如:<Label Text="Notes:" />
。但是Xaml并沒有提供翻譯功能,需要我們通過自定義擴(kuò)展標(biāo)記(markup extension)實(shí)現(xiàn)。
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
readonly CultureInfo ci;
//指定為項(xiàng)目中默認(rèn)資源文件ID
const string ResourceId = "demo.Resource.Resources";
public TranslateExtension()
{
if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
{
ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
}
}
public string Text { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return "";
ResourceManager resmgr = new ResourceManager(ResourceId, GetType().GetTypeInfo().Assembly);
var translation = resmgr.GetString(Text, ci);
if (translation == null)
{
#if DEBUG
throw new ArgumentException(
String.Format("Key '{0}' was not found in resources '{1}' for culture '{2}'.", Text, ResourceId, ci.Name), "Text");
#else
translation = Text; // HACK: returns the key, which GETS DISPLAYED TO THE USER
#endif
}
return translation;
}
}
Xaml中通過如下代碼設(shè)置標(biāo)題:
xmlns:local="clr-namespace:demo" Title="{local:Translate home}"
中英文顯示效果:
平臺(tái)元素國(guó)際化
如應(yīng)用程序的名稱、圖標(biāo)和啟動(dòng)屏中涉及的元素國(guó)際化時(shí)需要在單獨(dú)的平臺(tái)項(xiàng)目中設(shè)置,不需要編寫代碼故不在記錄。
http://blog.csdn.net/watermelon_/article/details/53418615
http://blog.csdn.net/chenliguan/article/details/50678678
項(xiàng)目地址:https://github.com/MyueX/xamarin.forms_news_demo