如何在 DatePicker C# WinRT 中转换月份和日期

How to translate the month and day in DatePicker C# WinRT

我在 Windows Phone 8.1 应用程序中有一个默认的日期选择器控件。更改设备语言时,日期选择器字段仍保留英文文本而不是翻译文本。我是否应该添加任何单独的逻辑来翻译这些文本,比如在模板中明确定义月份名称和日期以及必要的翻译逻辑?

要翻译的带有突出显示文本的图片

任何帮助或建议都会有很大帮助。

提前致谢

有语言属性指定语言。

<DatePicker Name="date" />
     CultureInfo ci = new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]);
                date.Language = ci.ToString();

更新

要完成它,您必须在 Package.appmanifest 文件中添加对该特定语言的支持。清单文件中有默认语言字段。 您可以通过编程方式执行此操作

 CultureInfo ci = new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]);
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = ci.Name;
            CultureInfo.DefaultThreadCurrentCulture = ci;
            CultureInfo.DefaultThreadCurrentUICulture = ci; 

DatePicker默认支持Globalization and localizationDatePicker无需设置任何内容即可支持多国语言。

我们需要做的是Specify the supported languages in the app's manifest.

An app developer specifies the list of supported languages in the Resources element of the app's manifest file (typically Package.appxmanifest), or Visual Studio automatically generates the list of languages in the manifest file based on the languages found in the project. The manifest should accurately describe the supported languages at the appropriate level of granularity. The languages listed in the manifest are the languages displayed to users in the Windows Store.

为了确保应用程序支持"fr-FR",我们可以select“Package.appxmanifest”中的“解决方案Explorer”并右击,然后select“查看代码”并使用

<Resources>
  <Resource Language="EN-US" />
  <Resource Language="FR-FR" />
</Resources>

而不是

<Resources>
  <Resource Language="x-generate" />
</Resources>

在此之后,当您将设备的语言更改为 "France" 时,DatePicker 将自动在法国显示。但是这不会立即生效,需要重新启动。

更新:

PrimaryLanguageOverride 是一种简单的覆盖设置,适用于为用户提供自己的独立语言选择的应用程序,或有充分理由覆盖默认语言选择的应用程序。

所以你可以使用PrimaryLanguageOverride来实现你想要的。

The PrimaryLanguageOverride setting is persisted between sessions. It should not be set each time the app is loaded. It should only be set based on user input presented in settings UI. The property can be read at any time. If the property has never been set, it returns an empty string.

您可以在下拉控件中添加 "Automatic" 选项。当用户 select 选择此选项时,您可以将 PrimaryLanguageOverride 设置为空字符串,例如:

ApplicationLanguages.PrimaryLanguageOverride = string.Empty;

然后您的应用程序将根据您支持的语言使用设备语言设置。有关您的应用程序将显示哪种语言的更多信息,请参阅 Create the application language list and Language matching

当您在下拉控件中使用 select 的其他语言时,您可以将 PrimaryLanguageOverride 设置为 selected 语言以强制您的应用以该语言显示。关于PrimaryLanguageOverride的更多使用,可以查看ApplicationLanguages.PrimaryLanguageOverride | primaryLanguageOverride property.

中的备注