How to detect the current language in a UWP App
If you are making yet another awesome app and want to make it big at the global level, your App has to support multiple languages or has to support Localization as said by many. You can't target a global audience if you don't take care of their local language. But to do so you should be able to detect the language on the device and then only you can show relevant content in that specific language. This post analyzes how the language feature behaves in a Windows 10 Phone (Lumia Device ) and how to detect the language in a UWP App.
These were my observations :
1) Currently the language is set to English (United States).
2) Long tap cestina and click move up or move whatever language you want to use to the top.
As you can see , it says "Restart phone". Till you restart the phone, you won't be able to detect the changes. You can test the same by using the UWP sample project at the end of the page.
3) If you move the current language (that was being used) to the top again, then the "Restart phone" option disappears as shown below :
The moral of the story is that you will be able to see everything in the new language only after you restart. I kept cestina at the top and restarted the phone as suggested. Then I could see everything in cestina (as shown below) which were earlier in English.
I tried almost 4 approaches to get the language of the phone just to see if any way gives me the latest language that I have moved to top. Unfortunately, even if you move up any new language, each approach still gives the current language that is being used. If you restart the device then all the approaches give the new language that was set. Things will be more clear when you download the sample project and play around with it.
I tried almost 4 approaches to get the language of the phone just to see if any way gives me the latest language that I have moved to top. Unfortunately, even if you move up any new language, each approach still gives the current language that is being used. If you restart the device then all the approaches give the new language that was set. Things will be more clear when you download the sample project and play around with it.
Let's see the code that does the magic. This is the code in my MainPage.xaml.cs which does the trick:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Windows.Globalization; using Windows.Globalization.DateTimeFormatting; using System.Globalization; namespace LangPOC { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public static string lang; public MainPage() { this.InitializeComponent(); this.Loaded += MainPage_Loaded; } private void MainPage_Loaded(object sender, RoutedEventArgs e) { var cultureName = new DateTimeFormatter("longdate", new[] { "US" }).ResolvedLanguage; textBlock.Text = cultureName.ToString(); CultureInfo cultureInfo = CultureInfo.CurrentCulture; CultureInfo currentUIInfo = CultureInfo.CurrentUICulture; //String LanguageCode = cultureInfo.TwoLetterISOLanguageName; textBlock.Text = "Getting Lang from DateTimeFormatter - " + textBlock.Text + Environment.NewLine + "CultureInfo - " + cultureInfo + Environment.NewLine + "Getting the topmost lang in settings - " + lang + Environment.NewLine + "CurrentUIInfo - " + currentUIInfo; } } }
My current language is cestina and here is the output that the sample project gives:
Now even if you move English (United States) to the top and run the sample project again, you will still get the above output i.e. cestina. But If you restart your phone and then run the sample project, you will get the language as English (United States). I have not gone into the details of the methods used in Code to get the language. Explanation of CultureInfo is all you need and is readily available. The idea was to analyze the behavior of the Windows Phone using a UWP App for language changes.
You can download the UWP Sample App from the link given below (hosted on Google Drive) :
Hope this article helped you.
Thanks!!
No comments:
Post a Comment
Thanks for your valuable comment