2017-08-21 67 views
0

我想實現Xamarin.Forms的I18N /本地化,如下所述:https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/ - 然而,即使我將設備上的語言設置爲法語並且具有法語的Resx文件,總是返回默認翻譯。我已經嘗試停用快速部署(按照前述網站的建議),但目前尚未取得成功。Xamarin.Forms本地化總是返回默認語言

在這一行:

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

+0

我想你可以分享演示,它可以重現此問題。 –

回答

1

您的資源應設置爲嵌入式

更改下圖中的選項。

enter image description here

+0

謝謝科爾夏! – hot33331