Xamarin.Forms本地化多语言

  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

新建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/

转载于:https://www.cnblogs.com/sephiroth-wzc/p/5375783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值