using System;
using System.Globalization;
+using System.Runtime.InteropServices;
using System.Text;
using System.Timers;
using Tizen.Applications.CoreBackend;
/// <since_tizen> 3 </since_tizen>
protected virtual void OnCreate()
{
+ string locale = ULocale.GetDefaultLocale();
+ ChangeCurrentUICultureInfo(locale);
+ ChangeCurrentCultureInfo(locale);
+
Created?.Invoke(this, EventArgs.Empty);
}
/// <since_tizen> 3 </since_tizen>
protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
{
- ChangeCurrentCultureInfo(e.Locale);
+ ChangeCurrentUICultureInfo(e.Locale);
LocaleChanged?.Invoke(this, e);
}
/// <since_tizen> 3 </since_tizen>
protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
{
+ ChangeCurrentCultureInfo(e.Region);
RegionFormatChanged?.Invoke(this, e);
}
base.Dispose(disposing);
}
- private void ChangeCurrentCultureInfo(string locale)
+ private CultureInfo ConvertCultureInfo(string locale)
{
ULocale pLocale = new ULocale(locale);
- CultureInfo currentCultureInfo = null;
try
{
- currentCultureInfo = new CultureInfo(pLocale.Locale.Replace("_", "-"));
+ return new CultureInfo(pLocale.LCID);
+ }
+ catch (ArgumentOutOfRangeException)
+ {
+ return GetFallbackCultureInfo(pLocale);
}
catch (CultureNotFoundException)
{
- currentCultureInfo = GetFallbackCultureInfo(pLocale);
+ return GetFallbackCultureInfo(pLocale);
+ }
+ }
+
+ private void ChangeCurrentCultureInfo(string locale)
+ {
+ CultureInfo.CurrentCulture = ConvertCultureInfo(locale);
+ }
+
+ private void ChangeCurrentUICultureInfo(string locale)
+ {
+ CultureInfo.CurrentUICulture = ConvertCultureInfo(locale);
+ }
+
+ private bool ExistCultureInfo(string locale)
+ {
+ foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
+ {
+ if (cultureInfo.Name == locale)
+ {
+ return true;
+ }
}
- CultureInfo.CurrentCulture = currentCultureInfo;
+ return false;
}
private CultureInfo GetCultureInfo(string locale)
{
- CultureInfo cultureInfo = null;
+ if (!ExistCultureInfo(locale))
+ {
+ return null;
+ }
try
{
- cultureInfo = new CultureInfo(locale);
+ return new CultureInfo(locale);
}
catch (CultureNotFoundException)
{
return null;
}
-
- return cultureInfo;
}
private CultureInfo GetFallbackCultureInfo(ULocale uLocale)
{
- string locale = string.Empty;
- CultureInfo fallbackCultureInfo = null;
+ string locale = uLocale.Locale.Replace("_", "-");
+ CultureInfo fallbackCultureInfo = GetCultureInfo(locale);
- if (uLocale.Script != null && uLocale.Country != null)
+ if (fallbackCultureInfo == null && uLocale.Script != null && uLocale.Country != null)
{
locale = uLocale.Language + "-" + uLocale.Script + "-" + uLocale.Country;
fallbackCultureInfo = GetCultureInfo(locale);
Script = GetScript(Locale);
Country = GetCountry(Locale);
Variant = GetVariant(Locale);
+ LCID = GetLCID(Locale);
}
internal string Locale { get; private set; }
internal string Script { get; private set; }
internal string Country { get; private set; }
internal string Variant { get; private set; }
+ internal int LCID { get; private set; }
private string Canonicalize(string localeName)
{
int err = 0;
// Get the country name from ICU
- StringBuilder sb = new StringBuilder(ULOC_SCRIPT_CAPACITY);
+ StringBuilder sb = new StringBuilder(ULOC_COUNTRY_CAPACITY);
if (Interop.BaseUtilsi18n.GetCountry(locale, sb, sb.Capacity, out err) <= 0)
{
return null;
return sb.ToString();
}
+
+ private int GetLCID(string locale)
+ {
+ // Get the LCID from ICU
+ uint lcid = Interop.BaseUtilsi18n.GetLCID(locale);
+ return (int)lcid;
+ }
+
+ internal static string GetDefaultLocale()
+ {
+ IntPtr stringPtr = IntPtr.Zero;
+ if (Interop.BaseUtilsi18n.GetDefault(out stringPtr) != 0)
+ {
+ return string.Empty;
+ }
+
+ if (stringPtr == IntPtr.Zero)
+ {
+ return string.Empty;
+ }
+
+ return Marshal.PtrToStringAnsi(stringPtr);
+ }
}
}