官網(wǎng)看到關(guān)于Forms應(yīng)用程序國際化的相關(guān)內(nèi)容栓始,便一邊練習(xí)一邊總結(jié)一下屡贺,雖然這部分知識點(diǎn)很可能永遠(yuǎn)用不到=炝肌1丛堋盗誊!
原文地址:https://developer.xamarin.com/zh-cn/guides/xamarin-forms/advanced/localization/
Xamarin.Forms PCL 國際化
Forms PCL中的國際化借助資源文件(RESX)實現(xiàn)。
準(zhǔn)備資源文件
在可移植項目中添加資源文件:
會在項目中生成如下兩個文件:
同時添加Resources.en.resx
文件隘弊,提供英文環(huán)境下使用的字符串哈踱。記得修改文件的生成操作為EmbeddedResource
。
Xamarin Studio添加名稱為Resources.en資源文件時提示Resources文件已經(jīng)存在,此時可以先添加名稱為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>首頁</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>
生成項目Designer.cs文件生成如下代碼:
為了獲取name為home對應(yīng)的值需要調(diào)用如下代碼
Resource.Resources.ResourceManager.GetString("home")
其中Resource
為resx文件所在文件夾串结,Resources
為根據(jù)文件名生成的類哑子。
簡化資源文件中字符串資源的獲取,resx文件右鍵—>屬性肌割。
修改自定義工具對應(yīng)的屬性值:ResXFileCodeGenerator
修改為PublicResXFileCodeGenerator
卧蜓。
ResXFileCodeGenerator,PublicResXFileCodeGenerator把敞,他們的區(qū)別是前者生成的Internal的弥奸,后者生成的是Public的資源。
重新生成項目奋早,此時可以通過如下代碼Resource.Resources.home
獲取home對應(yīng)的值盛霎。
確定當(dāng)前運(yùn)行平臺的語言環(huán)境
Forms本身并沒有提供獲取運(yùn)行平臺語言環(huán)境的功能,我們需要借助dependency service 實現(xiàn)耽装。
在可移植項目中定義如下接口:
/// <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);
}
同時定義幫助類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)對應(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 實現(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 實現(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)境會有如下顯示效果:
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中實現(xiàn)國際化
很多時候我們會直接在Xaml中設(shè)置文本值的顯示掉奄,如:<Label Text="Notes:" />
规个。但是Xaml并沒有提供翻譯功能,需要我們通過自定義擴(kuò)展標(biāo)記(markup extension)實現(xiàn)姓建。
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
readonly CultureInfo ci;
//指定為項目中默認(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}"
中英文顯示效果:
平臺元素國際化
如應(yīng)用程序的名稱诞仓、圖標(biāo)和啟動屏中涉及的元素國際化時需要在單獨(dú)的平臺項目中設(shè)置,不需要編寫代碼故不在記錄速兔。
http://blog.csdn.net/watermelon_/article/details/53418615
http://blog.csdn.net/chenliguan/article/details/50678678