Controlling the UI Culture from the LocalizabilityViewModel

,

The viewmodel exposes a list of CultureName objects via its SupportedCultures property. The list of supported cultures is initialized as shown:

readonly List<CultureInfo> supportedCultures = new List<CultureInfo>
                                {
                                    new CultureInfo("en-US"),
                                    new CultureInfo("fr-FR"),
                                    new CultureInfo("ar-AE")
                                };

A method called SetCulture is used to change the CurrentCulture and CurrentUICulture of the UI thread, as shown in the following excerpt:

void SetCulture(CultureInfo cultureInfo)
{
    Thread.CurrentThread.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;

    BindableResources.RaiseNotifyPropertyChanged();
    Message = string.Empty;

    SetCurrentCultureValues();
}

CurrentCulture affects the formatting of currency values, dates, and so on, whereas CurrentUICulture determines the language and consequently the primary resource dictionary that is used by your app.

Not all culture names are supported in Windows Phone. Attempting to instantiate a CultureInfo class using an unsupported culture name raises a PlatformNotSupportedException (see Figure 19.5).

Image

FIGURE 19.5 A PlatformNotSupportedException is raised if a specific culture name is not supported.

The viewmodel contains a CurrentCulture property that controls the active culture of the app. The CurrentCulture set accessor calls the SetCulture method if assignment succeeds. If a PlatformNotSupportedException is raised, the value falls back to the previous value. See the following excerpt:

CultureInfo currentCulture;

public CultureInfo CurrentCulture
{
    get
    {
        return currentCulture;
    }
    set
    {
        CultureInfo temp = currentCulture;
        AssignmentResult result
            = Assign(ref currentCulture, value);

        if (result == AssignmentResult.Success && value != null)
        {
            try
            {
                SetCulture(value);
            }
            catch (PlatformNotSupportedException)
            {
                CurrentCulture = temp;
                Message = "Platform not supported";
                return;
            }
        }
    }
}

The viewmodel contains various properties that indicate the culture-specific date, time, and currency formatting. These properties are initialized in the SetCurrentCultureValues method, which is shown in the following excerpt:

void SetCurrentCultureValues()
{
    CultureInfo culture = Thread.CurrentThread.CurrentCulture;
    CultureInfo uiCulture = Thread.CurrentThread.CurrentUICulture;
    DisplayLanguage = uiCulture.DisplayName;
    RegionalFormat = culture.NativeName;
    DateTime date = DateTime.Now;

    try
    {
        DateFormatLong = culture.DateTimeFormat.LongDatePattern
                                    + " " + date.ToString("D");
        DateFormatShort = culture.DateTimeFormat.ShortDatePattern
                                    + " " + date.ToString("d");
        TimeFormat = culture.DateTimeFormat.LongTimePattern + " "
                                    + date.ToString("T");
    }
    catch (IndexOutOfRangeException)
    {
        Message = "DateTimeFormat is not defined for culture.";
    }
    const int monetaryAmount = 123456789;
    CurrencyFormat = monetaryAmount.ToString("C");

    if (Equals(culture, supportedCultures[2]))
    {
        FlowDirection = FlowDirection.RightToLeft;
    }
    else
    {
        FlowDirection = FlowDirection.LeftToRight;
    }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.94.152