0

我有一個適用於iOS的PCL項目,我正在嘗試創建Windows Phone 8.1版本。如何將帶有Resx本地化功能的Windows Phone項目添加到Xamarin PCL解決方案?

我下面這個教程:https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/

而在檢查應用程序:https://github.com/xamarin/xamarin-forms-samples/tree/master/UsingResxLocalization

但爲時已過時。即使是git項目也不同於教程,並且它們都不起作用。

的ILocalize界面的Windows應該是這樣的:

[assembly: Dependency(typeof(UsingResxLocalization.WinPhone.Localize))] 

namespace UsingResxLocalization.WinPhone 
{ 
    public class Localize : UsingResxLocalization.ILocalize 
    { 
     public System.Globalization.CultureInfo GetCurrentCultureInfo() 
     { 
      return System.Threading.Thread.CurrentThread.CurrentUICulture; 
     } 
    } 
} 

System.Threading.Thread.CurrentThread.CurrentUICulture根本不存在。我發現我可以用Windows.System.UserProfile.GlobalizationPreferences.Languages[0].ToString()代替。

它適用於本地化的語言資源,但默認的資源不工作或者默認語言「en」或類似「RU」任何其他非本地化語言。我得到另一個錯誤:

在TranslateExtention類ProvideValue()方法獲得:

Key 'Start' was not found in resources 'AppNameSpace.AppResources' for culture 'en'

存在「開始」嘗試從資源獲得第一個關鍵。它發生在項目上的所有其他鍵上。

AppNameSpace.AppResources將是正確的文件,「en」是我設置的區域,所以它應該工作。但事實並非如此。

編譯時,我也越來越以下警告:

The assembly "MyApp.dll" does not have a NeutralResourcesLanguageAttribute on it. To be used in an app package, portable libraries must define a NeutralResourcesLanguageAttribute on their main assembly (ie, the one containing code, not a satellite assembly). 4>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\AppxPackage\Microsoft.AppXPackage.Targets(1216,5): warning APPX2002: Cannot find the neutral resource language for the resources for portable library 'MyApp'. Verify that the portable library defines a NeutralResourcesLanguageAttribute. The build is continuing assuming the project's default culture of 'en-US' correctly identifies the portable library's neutral resources. 4>MakePRI : warning 0xdef00522: Resources found for language(s) 'de, es, fr, pt' but no resources found for default language(s): 'en-US'. Change the default language or qualify resources with the default language. http://go.microsoft.com/fwlink/?LinkId=231899

但我不知道如何解決它。

在本教程中,它也說:

Windows Phone projects must be properly configured for localized text to be displayed. Supported languages must be selected in the Project Options and the WMAppManifest.xml files. If these settings are not updated the localized RESX resources will not be loaded.

精細,不過那些已經不存在了。至少應該在哪裏。我甚至在我的項目中發現了一個Package.appxmanifest文件,但它沒有這些區域選項。

所以,我需要有一個更新的方式做到這一點幫助。

感謝

回答

0

所以我發現,當你添加了Windows Phone項目,沒有Windows手機項目的解決方案,它不添加它需要的一切。

而且教程不顯示的一切,是必要的(沒有大的消息出現)。

我所有的項目缺少我的PCL AssemblyInfo.cs文件是[assembly: NeutralResourcesLanguage("en-US")]

的RESX教程也說,你應該使用:

 if (Device.OS == TargetPlatform.iOS || Device.OS == TargetPlatform.Android) 
    { 
      ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo(); 
    } 

在TranslateExtention.cs文件,因爲Windows手機並不需要它。那麼,這是錯誤的。至少爲了讓仿真器獲得正確的語言,它需要使用DependencyService並以這種方式獲取CultureInfo:

System.Globalization.CultureInfo ci = null; 
    ci = new System.Globalization.CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0].ToString()); 
    return ci; 
相關問題