Regional format and language in Windows Phone 10 UWP app - c#

I'm working on a localized Windows (Phone) 10 UWP app.
I have implemented support for 2 language: en-US and nl-NL (Dutch-Netherlands). This works fine: When the user has selected English in the phone settings the app starts in English and when the user has selected Dutch in the phone settings the app starts in Dutch.
To get this working I had to make some changes in the package.appxmanifest because my language resources are in a different DLL:
<Resources>
<Resource Language="nl-NL"/>
<Resource Language="en-US"/>
</Resources>
But I cannot find any way to get the regional format from the user for date, time and number formatting.
When the user has selected English as language but Dutch (Netherlands) for regional format, my app starts with both
System.Globalization.CultureInfo.CurrentUICulture and System.Globalization.CultureInfo.CurrentCulture set to "en-US", where System.Globalization.CultureInfo.CurrentCulture should be "nl-NL".
I have been searching all of the documentation but I cannot find a way to retrieve the phone regional format (except Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion which is something different).
Is there a way to retrieve the phone regional format?

I only know the following hack mentioned 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);

Peter Meinl's answer works, but is a little confusing because you do not need the RegionInfo.
Pedro Lamas describes the hack using the ResolvedLanguage of DateTimeFormatter just using "US".
DateTimeFormatter dtf = new DateTimeFormatter("longdate", new[] { "US" });
string regionInfoName = dtf.ResolvedLanguage;
CultureInfo regionFormatCultureInfo = new CultureInfo(regionInfoName);
The ResolvedLanguage property of the DateTimeFormatter will contain the regional format id of the phone, in this case "nl-NL".
Mind that you DO need a language in the constructor of the DateTimeFormatter, just new DateTimeFormatter("longdate") or DateTimeFormatter.LongDate won't work.

Related

How can i get locale info in Unity C#?

I am developing a project with Unity 2019.2b, it is expected to work at all platforms (android, ios, windows, macos) , i am trying to get device locale info(e.g. en-us, en-au, en-bz) but all i can find is Application.systemLanguage definition. Even i decide to use this information i will still need region info. Are there any sample or way to get locale info in Unity or to get device region info? (If the solution for only region info; I dont want to use device location or dont want to use user ip address, i want to get it from settings)
I tried to get locale or at least device region info with RegionInfo,CultureInfo,System.Threading.Thread.CurrentThread.CurrentUICulture and System.Threading.Thread.CurrentThread.CurrentCulture definitions, but it didn't work.
string regionName = System.Globalization.RegionInfo.CurrentRegion.Name;
string cultureName = System.Globalization.CultureInfo.CurrentCulture.Name;
string cname = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
string uiname = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
Depending on the platform (ios, android, windows,macos) sometimes the result comes as InvariantCulture, sometimes always en-US even device locale is not en-US.
The issue is coming from Mono, used by Unity.
I found a few solutions in this forum thread.
If you are working with Windows a workaround might be using PInvoke:
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
private static extern int GetSystemDefaultLCID();
And then check the global culture like so:
var currentCulture = new CultureInfo(GetSystemDefaultLCID());
This might be used to set the culture:
Thread.CurrentThread.CurrentCulture = new CultureInfo(GetSystemDefaultLCID());
Thread.CurrentThread.CurrentUICulture = new CultureInfo(GetSystemDefaultLCID());

Why C# CultureInfo.CurrentCulture give en_US in Window-7 Japanese culture?

I have window-7 Ultimate OS.I written below code for get current culture info.
void TestMessage()
{
CultureInfo culture = CultureInfo.CurrentCulture;
CultureInfo culture1 = Thread.CurrentThread.CurrentCulture;
}
it is working fine with window-8,server 2012 Japanese OS. But it is not working in window-7 only. Please look below image of my computer region setting
Can any anyone guide me to get correct culture name?
Thanks,
There's CurrentCulture and CurrentUICulture.
CurrentCulture: formatting of data (numbers, dates), it is configured in Windows using the tab visible in your screenshot
CurrentUICulture: the language to speak/write to your user, it is configured in Windows using one of the other tabs showing in your screenshot. ("Keyboard & Languages" I think)
Your screenshot:
... is showing american numeric notation because CurrentCulture is set to en-US
... is talking Japanese because CurrentUICulture is very likely set to Japanese
So, if you need to know what language to use for localization, as is probably your case, you should be using CurrentUICulture.

ChangeCulture: Culture name is not supported in C# Winform [duplicate]

This question already has answers here:
Why do I get, "Culture is not supported" and What, if Anything, Should I Do about it?
(2 answers)
Closed 5 years ago.
I have a windows form application developed in VB.net with Target Framework 3.5. Application is running good in windows 7,8,8.1,10. But in windows XP, its showing an error :
Culture name 'en-in' is not supported
I have checked the code and find the line causing error is :
My.Application.ChangeCulture("en-IN")
when i tried to remove this line, many forms and reports are showing error or wrong information. So i can not remove this line.
Is there any way to install or load this Culture in windows ?
en-IN is not a known culture code.
You can check all existing codes in this list.
You can create a custom culture, using CultureAndRegionInfoBuilder, but it's highly unrecommended.
// Create a new Culture, with the name you desire
CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder("en-IN", CultureAndRegionModifiers.None);
// Load all defaults from en-US
CultureInfo ci = new CultureInfo("en-US");
cib.LoadDataFromCultureInfo(ci);
// Populate the new CultureAndRegionInfoBuilder object with region information.
RegionInfo ri = new RegionInfo("US");
cib.LoadDataFromRegionInfo(ri);
// Now you can make changes, or finish.
// Changes can be currency, RegionName, etc.
// Finish
cib.Register();
this article explains how to do it.
Or you can set custom culture as :
Dim customCulture As Globalization.CultureInfo = New Globalization.CultureInfo("en-US")
customCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy"
customCulture.DateTimeFormat.LongDatePattern = "dd-MMM-yyyy HH:mm:ss"
customCulture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"
customCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss"
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture
System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture

Adding a new Language to SpeechSynthesizer

So I'am trying to add a new language, spesifically norwegian, to SpeechSynthesizer, but it doesn't seem to get installed.
Found this:
Add another voice into .NET Speech
(But here the problem is that Czech isn't supported)
I have installed the norwegian pack from here:
http://www.microsoft.com/en-us/download/details.aspx?id=27224
In my code I use this to check if it is installed:
foreach (var voice in speaker.GetInstalledVoices())
{
Console.WriteLine(voice.VoiceInfo.Description);
}
But it only outputs:
Microsoft Zira Desktop - English (United States)
Have checked the text-to-Speech tool were this is also the only option.
Have also tried to log off/log on and restart the computer.
Anyone know how to fix this?
You may need to add a Speech Language to Windows 10 and set your Locale, Country, Windows display language and Speech language so they are all aligned with one of Cortana's supported locale configurations.
To confirm the settings are set correctly:
Open Settings. Select Time& language, and then Region & Language.
Check the Language (set as default) setting for your Windows display language. If your desired language is not available, add your desired language:
Click Add Language.
Select your desired language from the list.
Select the desired locale, which is the language/country combination.
Click on the newly selected locale and select Options.
Under Download language pack, click Download.
Under Speech, click Download.
After the downloads are complete (this could take several minutes), return to the Time & Language settings.
Click on your new language and select Set as Default.
NOTE: IF you changed languages, you must sign out of your account and back in for the new setting to take effect.
Check the Country or region setting. Make sure the country selected corresponds with the Windows display language set in the Language setting.
Return to Settings and Time & language, and then select Speech. Check the Speech language setting, and make sure it’s aligned with the previous settings.
After you have correctly done the above, your language should appear in the SpeechSynthesizer.AllVoices collection. You should then be able to assign this voice to your SpeechSynthesizer instance's Voice property:
private async void SpeakText(MediaElement audioPlayer, string TTS)
{
SpeechSynthesizer ttssynthesizer = new SpeechSynthesizer();
//Set the Voice/Speaker to Spanish
using (var speaker = new SpeechSynthesizer())
{
speaker.Voice = (SpeechSynthesizer.AllVoices.First(x => x.Gender == VoiceGender.Female && x.Language.Contains("ES")) );
ttssynthesizer.Voice = speaker.Voice;
}
SpeechSynthesisStream ttsStream = await ttssynthesizer.SynthesizeTextToStreamAsync(TTS);
audioPlayer.SetSource(ttsStream, "");
}
http://answers.microsoft.com/en-us/windows/forum/windows_10-other_settings/speech-language-in-windows-10-home/3f04bc02-9953-40b1-951c-c1d262fc3f63?auth=1

Internationalize Windows Phone 7 App

I am currently working on a windows phone application that takes some information from the user and returns some other information based on the user input.
The application works great if the specific device has its region settings set to US. If the region settings of the device are set to Greek or German, some problems occur. For example, the US decimal point character "." is considered as "," and vice versa. As a result, all the calculations are false.
What I want to do is internationalize the application so that it works exactly the same no matter what the regional settings are. Is this possible?
If you want your app allways to show number and dates in one specify format you can force the app to allways run in one specify culture like this.
You just have to set the current thread of your app to one specify culture (add to the App.cs file)!
public App()
{
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Set the current thread to US!
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

Categories

Resources