我正在尝试为这里描述的Xamarin.Forms实现I18N / Localization:https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/ - 但是,即使我将设备上的语言设置为法语并且有法语的Resx文件,那么就有了' s始终返回默认翻译。我已经尝试过停用快速部署(如前面提到的网站上所推荐的那样),但到目前为止还没有成功。
在这一行:
var translation = ResMgr.Value.GetString(Text, ci);
值ci是fr-CH,但返回的字符串是默认语言而不是法语
// You exclude the 'Extension' suffix when using in Xaml markup
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
readonly CultureInfo ci;
const string ResourceId = "ResxI18N.Resx.Resources";
private static readonly Lazy<ResourceManager> ResMgr = new Lazy<ResourceManager>(() => new ResourceManager(ResourceId
, typeof(TranslateExtension).GetTypeInfo().Assembly));
public TranslateExtension()
{
if (Device.OS == TargetPlatform.iOS || Device.OS == TargetPlatform.Android)
{
ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
}
}
public string Text { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return "";
var translation = ResMgr.Value.GetString(Text, ci);
if (translation == null)
{
}
return translation;
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i18n="clr-namespace:ResxI18N.Helpers;assembly=ResxI18N"
xmlns:local="clr-namespace:ResxI18N" x:Class="ResxI18N.ResxI18NPage">
<Label Text="{i18n:Translate Welcome}" VerticalOptions="Center" HorizontalOptions="Center" />
完整示例可在此处找到:https://github.com/hot33331/ResxI18N