Xamarin.Forms本地化多语言

时间:2021-07-24 10:08:34
  1. Forms

右键新建文件夹 Localization

新建文件TranslateExtension.cs

namespace Localization

{

///
<summary>

/// xaml 文件本地化

///
</summary>

[ContentProperty("Text")]

public
class
TranslateExtension : IMarkupExtension

{

///
<summary>

/// 本地资源对象

///
</summary>

readonly
CultureInfo ci;

///
<summary>

/// 反射用命名空间常量

///
</summary>

const
string ResourceId = "BarCode.Localization.TextResources";

 

///
<summary>

/// 构造函数 本地化对象根据抽象实现对象获取

///
</summary>

public TranslateExtension()

{

ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();

}

 

///
<summary>

/// 文本对象属性

///
</summary>

public
string Text { get; set; }

 

///
<summary>

/// 获取数据值显示用于xaml

///
</summary>

///
<param name="serviceProvider"></param>

///
<returns></returns>

public
object ProvideValue(IServiceProvider serviceProvider)

{

if (Text == null)

return
"";

 

ResourceManager resmgr = new
ResourceManager(ResourceId

, typeof(TranslateExtension).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;

}

}

}

新建抽象接口ILocalize.cs

///
<summary>

/// 定义一个接口暴露用户的首选UICulture 本地资源化对象

///
</summary>

public
interface
ILocalize

{

CultureInfo GetCurrentCultureInfo();

}

新建资源文件TextResources.resx

Xamarin.Forms本地化多语言

新建TextResources.en-US.resx

名称相同(key)一致,值区分语言。

新建xaml文件Multilingualism.xaml

<?xml
version="1.0"
encoding="utf-8" ?>

<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"

xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

x:Class="XMD1.Page.Multilingualism"

xmlns:i18n="clr-namespace:Localization">

 

<StackLayout >

<!--<BoxView AbsoluteLayout.LayoutBounds="0.25, 0.25, 0.5, 0.5"

Color="Blue"

AbsoluteLayout.LayoutFlags="All" />-->

<Label
Text="{i18n:Translate About_Context}"
VerticalOptions="Center"
HorizontalOptions="Center"
x:Name="lab"/>

<Button
Text="中文"
Clicked="zhclicked"
HorizontalOptions="Start" />

<Button
Text="英文"
x:Name="enbtn"
HorizontalOptions="End"/>

</StackLayout>

  1. </ContentPage>Android

同目录、实现接口

using Localization;

using Xamarin.Forms;

 

[assembly: Dependency(typeof(Localize))]

namespace Localization

{

///
<summary>

/// Android 本地资源化实现对象

///
</summary>

public
class
Localize : ILocalize

{

public System.Globalization.CultureInfo GetCurrentCultureInfo()

{

var androidLocale = Java.Util.Locale.Default;

var netLanguage = androidLocale.ToString().Replace("_", "-"); // turns pt_BR into pt-BR

return
new System.Globalization.CultureInfo(netLanguage);

}

}

}

  1. IOS

同目录、实现接口

 

using Foundation;

using Localization;

using System;

using Xamarin.Forms;

 

[assembly: Dependency(typeof(Localize))]

namespace Localization

{

///
<summary>

/// ios 本地资源化实现对象

///
</summary>

public
class
Localize: ILocalize

{

public System.Globalization.CultureInfo GetCurrentCultureInfo()

{

var netLanguage = "en";

var prefLanguageOnly = "en";

if (NSLocale.PreferredLanguages.Length > 0)

{

var pref = NSLocale.PreferredLanguages[0];

prefLanguageOnly = pref.Substring(0, 2);

if (prefLanguageOnly == "pt")

{

if (pref == "pt")

pref = "pt-BR"; // get the correct Brazilian language strings from the PCL RESX (note the local iOS folder is still "pt")

else

pref = "pt-PT"; // Portugal

}

netLanguage = pref.Replace("_", "-");

Console.WriteLine("preferred language:" + netLanguage);

}

System.Globalization.CultureInfo ci = null;

try

{

ci = new System.Globalization.CultureInfo(netLanguage);

}

catch

{

// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)

// fallback to first characters, in this case "en"

ci = new System.Globalization.CultureInfo(prefLanguageOnly);

}

return ci;

}

}

}

 

参考一下布局

https://developer.xamarin.com/guides/xamarin-forms/controls/layouts/