Localization not working in .NET Maui Android project in combination with installing nuget package.

huangapple go评论64阅读模式
英文:

Localization not working in .NET Maui Android project in combination with installing nuget package

问题

我有2个项目:

  • 一个库项目(Home.BudgetPlanner.Data),其中包含位于Localization文件夹中的一些带有字符串的资源文件。例如:TransactionType.resx 和 TransactionType.nl-BE.resx,其中都包含字符串 Expense 和 Income。该项目被导出为一个包。
  • 一个 .NET Maui 项目(Home.BudgetPlanner.Mobile),包含 Android 和 Windows 平台。在该项目中安装了库包。

但是,当我在我的 .NET Maui 项目中将文化设置为 nl-BE,并且想要检索 Home.BudgetPlanner.Data.Localization.TransactionType.Expense 的荷兰语值时,我得到了默认的英语字符串。

cultureInfo = new CultureInfo("nl-BE");

Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

var x = Home.BudgetPlanner.Data.Localization.TransactionType.Expense; // 英语字符串,但期望荷兰语值

这个问题只发生在 Android 平台上。当我部署到 Windows 平台时,没有问题。

此外,看起来这是一个与 Android 结合使用的包相关的问题。只有当我在我的 .NET Maui 项目中将 Home.BudgetPlanner.Data 安装为一个包时,才会出现问题。当我手动添加对导出的 Home.BudgetPlanner.Data.dll 的引用(使用项目引用)时,它可以正常工作。

当我在 .NET Maui 项目本身中添加资源文件时,本地化也可以正常工作。所以我认为这个问题特定于 Android 和包中的本地化组合。

是否有其他人遇到相同的问题?

我正在使用 Microsoft Visual Studio Professional 2022(64 位)版本 17.6.2。

我已经检查了资源文件的名称和内容是否正确。

在 csproj 文件中添加了以下内容:

<GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore>

在 Android MainActivity 类中添加了 ConfigChanges.Locale:

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, AllowEmbedded = true, ConfigurationChanges = ConfigChanges.Locale | ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity

我还在物理设备上进行了测试,结果相同。我还将我的设备语言更改为荷兰语,但没有结果。我还添加了 nl 资源文件,可能是因为我的手机设置为 nl 而不是 nl-BE。但那也没有帮助。

英文:

I have 2 projects:

  • A library project (Home.BudgetPlanner.Data) containing some resource files with strings in the folder Localization. For example: TransactionType.resx and TransactionType.nl-BE.resx containing both the strings Expense and Income. This project is exported as a package.
  • A .NET Maui project (Home.BudgetPlanner.Mobile) with platforms Android and Windows. In this project the library package is installed.

But, when when the culture is set to nl-BE in my .NET Maui project, and I want to retrieve the dutch value of Home.BudgetPlanner.Data.Localization.TransactionType.Expense, I get the default english string.

cultureInfo = new CultureInfo(&quot;nl-BE&quot;);

Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

var x = Home.BudgetPlanner.Data.Localization.TransactionType.Expense; // english string but expecting dutch value

This problem happens only on the Android platform. When I deploy to the Windows platform there is no problem.

Also, it looks like it is a package related problem in combination with Android. The problem only happens when I install Home.BudgetPlanner.Data as a package in my .NET Maui project. When I manually add a reference to the exported Home.BudgetPlanner.Data.dll (using project reference) it works.

When I add resource files in .NET Maui project itself, localization works also. So I think this problem is specific a combination of Android and localization in a package.

Does anyone else have this same problem?

I am using Microsoft Visual Studio Professional 2022 (64-bit) Version 17.6.2.

I have checked if resource file names and content are correct.

Added in csproj file:

&lt;GenerateSatelliteAssembliesForCore&gt;true&lt;/GenerateSatelliteAssembliesForCore&gt;

Added ConfigChanges.Locale in Android MainActivity class:

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, AllowEmbedded = true, ConfigurationChanges = ConfigChanges.Locale | ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity

Tested it also on a physical device, with the same result.
I also changed the language of my device to dutch without any result. I have added also next to nl-BE, nl resource files, maybe because my phone was set to nl instead of nl-BE. But that did not help either.

答案1

得分: 0

这是 Github 上的一个问题,您可以参考这个问题 .Net MAUI Localization 并跟进。

此外,有人遇到了这个问题,提到在调试模式下构建时本地化不起作用。您可以查看这个问题 通过卫星程序集进行本地化在调试模式下不适用于 Android 构建的应用

英文:

This is an issue on the Github and you can refer to this issue .Net MAUI Localization and follow it up.

In addition, someone who met this problem mentioned that the Localization did not work when he built it in the debug mode. You can check this isssue about Localization via satellite assemblies doesn't work for android when the app built in debug mode.

huangapple
  • 本文由 发表于 2023年6月9日 04:51:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定