Windows Phone 10 UWP应用程序

时间:2016-04-29 12:56:16

标签: c# win-universal-app windows-10-mobile

我正在使用本地化的Windows(电话)10 UWP应用程序。

我已经实施了对2种语言的支持:en-US和nl-NL(Dutch-Netherlands)。这很好用:当用户在手机设置中选择英语时,应用程序以英语启动,当用户在手机设置中选择荷兰语时,应用程序将以荷兰语启动。

为了实现这一点,我必须在package.appxmanifest中进行一些更改,因为我的语言资源位于不同的DLL中:

  <Resources>
    <Resource Language="nl-NL"/>
    <Resource Language="en-US"/>
  </Resources>

但我无法找到任何方法从用户那里获取日期,时间和数字格式的区域格式。

当用户选择英语作为语言但荷兰语(荷兰)选择区域格式时,我的应用程序从两者开始 System.Globalization.CultureInfo.CurrentUICulture和System.Globalization.CultureInfo.CurrentCulture设置为&#34; en-US&#34;,其中System.Globalization.CultureInfo.CurrentCulture应为&#34; nl-NL&#34;。< / p>

我一直在搜索所有文档,但我找不到检索手机区域格式的方法(Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion除外)。

有没有办法检索手机区域格式?

2 个答案:

答案 0 :(得分:2)

我只知道以下黑客提到的here

 var systemLanguage = GlobalizationPreferences.Languages.First();
 var regionInfo = new RegionInfo(systemLanguage);
 var dtf = new DateTimeFormatter("longdate", new[] { regionInfo.TwoLetterISORegionName });
 var regionInfoName = dtf.ResolvedLanguage;
 var regionFormatCultureInfo = new CultureInfo(regionInfoName);

答案 1 :(得分:2)

Peter Meinl的回答有效,但有点令人困惑,因为你不需要RegionInfo。

Pedro Lamas使用&#34; US&#34;来描述使用DateTimeFormatter的ResolvedLanguage的hack。

    DateTimeFormatter dtf = new DateTimeFormatter("longdate", new[] { "US" });
    string regionInfoName = dtf.ResolvedLanguage;
    CultureInfo regionFormatCultureInfo = new CultureInfo(regionInfoName);

DateTimeFormatter的ResolvedLanguage属性将包含手机的区域格式ID,在本例中为&#34; nl-NL&#34;。

请注意,你需要在DateTimeFormatter的构造函数中使用一种语言,只需要新的DateTimeFormatter(&#34; longdate&#34;)或DateTimeFormatter.LongDate就无法工作。