本地化 Xamarin Forms
Localizing Xamarin Forms
我正在尝试本地化 Xamarin Forms 应用程序。我遵循了此处包含的所有步骤:https://developer.xamarin.com/guides/xamarin-forms/localization/
但是,在 Android 模拟器中,更改默认语言后,字符串不会显示,它们仍然是默认语言。在设备上,该应用甚至无法启动。
到目前为止采取的步骤:
1) 尝试在main launcher下断点(BP),它跳过了BP进入了主页面
2) 在 Localize class 上尝试了 BP,它没有达到它
3) 在 Device.OS 检查中尝试了 BP,跳过它。
Visual Studio中的资源文件如下:
AppResources.resx
AppResources.fr.resx
AppResources.es.resx
有人曾成功本地化 Xamarin Forms 应用程序吗?我需要一些帮助,因为 Visual Studio 不会引发任何异常 ...
更新 1:
Visual Studio中的资源文件如下:
AppResources.resx
AppResources.fr_FR.resx
AppResources.es_ES.resx
更新 2:
在最近的建议和修改之后,我遇到了以下异常:未找到标记扩展。
这令人不安,因为存在标记扩展 class。
Doesn't work in DEBUG mode (Android only)
If the translated strings are working in your RELEASE Android builds
but not while debugging, right-click on the Android Project and select
Options > Build > Android Build and ensure that the Fast assembly
deployment is NOT ticked. This option causes problems with loading
resources and should not be used if you are testing localized apps.
已解决:
问题出在扩展的声明中。在 PCL 项目中,发生了以下情况:
a) TranslateExtension class 位于名为 Pages
的子文件夹中
b) 初始声明是
xmlns:languages="clr-namespace:MyApp;assembly=MyApp"
c) 以下声明解决了问题:
xmlns:languages="clr-namespace:MyApp.Pages;assembly=MyApp"
以及上述 .resx 文件的语言代码命名约定
2021 年更新
当前所有答案均无效!您需要添加资源。右键单击您的 xamarin 表单项目(不是 xamarin android 或 ios)=> 转到项目属性 ==> 资源 ==> 添加资源。
这将创建一个“Properties”文件夹和一个 resources.resx 文件,您可以根据需要删除这些文件。
然后通过创建 AppResources.resx 和其他语言文件(例如阿拉伯语的 AppResources.ar.resx 文件)来遵循 official microsoft documentation。所有这些文件必须在属性文件夹中。
然后,在您的 XAML 页面中,将命名空间声明为:
xmlns:res="clr-namespace:YourProject.Properties"
然后只需使用:
<Label Text="{x:Static res:AppResources.Address}" >
这将根据设备使用的语言使用相应的语言文件。如果不支持该语言,它将使用 defulat 文件 (AppResources.res)
呃!!!浪费了一个小时来解决这个问题!!!
我正在尝试本地化 Xamarin Forms 应用程序。我遵循了此处包含的所有步骤:https://developer.xamarin.com/guides/xamarin-forms/localization/
但是,在 Android 模拟器中,更改默认语言后,字符串不会显示,它们仍然是默认语言。在设备上,该应用甚至无法启动。
到目前为止采取的步骤: 1) 尝试在main launcher下断点(BP),它跳过了BP进入了主页面 2) 在 Localize class 上尝试了 BP,它没有达到它 3) 在 Device.OS 检查中尝试了 BP,跳过它。
Visual Studio中的资源文件如下:
AppResources.resx
AppResources.fr.resx
AppResources.es.resx
有人曾成功本地化 Xamarin Forms 应用程序吗?我需要一些帮助,因为 Visual Studio 不会引发任何异常 ...
更新 1:
Visual Studio中的资源文件如下:
AppResources.resx
AppResources.fr_FR.resx
AppResources.es_ES.resx
更新 2:
在最近的建议和修改之后,我遇到了以下异常:未找到标记扩展。
这令人不安,因为存在标记扩展 class。
Doesn't work in DEBUG mode (Android only)
If the translated strings are working in your RELEASE Android builds but not while debugging, right-click on the Android Project and select Options > Build > Android Build and ensure that the Fast assembly deployment is NOT ticked. This option causes problems with loading resources and should not be used if you are testing localized apps.
已解决:
问题出在扩展的声明中。在 PCL 项目中,发生了以下情况:
a) TranslateExtension class 位于名为 Pages
的子文件夹中b) 初始声明是
xmlns:languages="clr-namespace:MyApp;assembly=MyApp"
c) 以下声明解决了问题:
xmlns:languages="clr-namespace:MyApp.Pages;assembly=MyApp"
以及上述 .resx 文件的语言代码命名约定
2021 年更新
当前所有答案均无效!您需要添加资源。右键单击您的 xamarin 表单项目(不是 xamarin android 或 ios)=> 转到项目属性 ==> 资源 ==> 添加资源。
这将创建一个“Properties”文件夹和一个 resources.resx 文件,您可以根据需要删除这些文件。
然后通过创建 AppResources.resx 和其他语言文件(例如阿拉伯语的 AppResources.ar.resx 文件)来遵循 official microsoft documentation。所有这些文件必须在属性文件夹中。
然后,在您的 XAML 页面中,将命名空间声明为:
xmlns:res="clr-namespace:YourProject.Properties"
然后只需使用:
<Label Text="{x:Static res:AppResources.Address}" >
这将根据设备使用的语言使用相应的语言文件。如果不支持该语言,它将使用 defulat 文件 (AppResources.res)
呃!!!浪费了一个小时来解决这个问题!!!