Localization

Oct 30, 2011 at 7:52 PM
Edited Oct 31, 2011 at 8:35 AM

I am going to introduce localization of your usercontrol.

First of all I introduced in the project a resx file and in Calendar.cs I modified the code as follows:

 private string GetMonthName() {
            string returnValue = "";
            switch (_month) {
                case 1:
                    returnValue = ControlResources.January;
                    break;
.....

or

          returnValue = LocalizedStrings.GetString("January");

using this helper class:
namespace WPControls.LocalizedResources {
        public class LocalizedStrings {
        // for- XAML use
private static ControlResources localizedResources = new ControlResources(); //proprietà che restituisce prorio una classe AppResources
        internal ControlResources LocalizedResources { //era public
            get { 
                return localizedResources;
            }
        }

        // Shortcut to Resource Manager: for C# use
        public static String GetString(String resourceKey) {
            return ControlResources.ResourceManager.GetString(resourceKey);
        }
    }
}
 
However I have found trouble to localize the Generic.xaml file. I tried as follows but it did't work (the string is not shown): any idea? I'll post the code with the .it.resx ...

 

 

<ResourceDictionary

...  

xmlns:res ="clr-namespace:WPControls.LocalizedResources">

 

<ResourceDictionary.MergedDictionaries >

<ResourceDictionary >

<res:LocalizedStrings x:Key ="LocalizedStrings" />

</ResourceDictionary>

</ResourceDictionary.MergedDictionaries >

....

 

 
<TextBlock Text="{Binding LocalizedResources.Sunday, Source={StaticResource LocalizedStrings}}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column ="1"/>
..... ....
Jan 12, 2012 at 6:13 AM
Edited Jan 12, 2012 at 6:15 AM

Probably simplest way is just change GetMonthName() method

 

private string GetMonthName()
{
    return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(_month);
}

Jan 12, 2012 at 7:14 AM
Edited Jan 12, 2012 at 7:17 AM

Thank you cieszak.

I wrote that post before I found a solution that I put already in the patches of this project. In the following my code: probably instead of using the switch in the GetMonthName is better to use your method ;-) ... I'm going to change that lines in my project!!

 

private DateTimeFormatInfo dtfi;

...

public Calendar() {
...
         dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
 }

...

 private string GetMonthName() {
	//NOTE - better your code: return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(_month);
            string returnValue = "";

            switch (_month) {
                case 1:
                    returnValue = dtfi.MonthNames[0];//ControlResources.January;
                    break;
                case 2:
                    returnValue = dtfi.MonthNames[1];//ControlResources.February;
                    break;
                case 3:
                    returnValue = dtfi.MonthNames[2];//ControlResources.March;
                    break;
                case 4:
                    returnValue = dtfi.MonthNames[3];//ControlResources.April;
                    break;
                case 5:
                    returnValue = dtfi.MonthNames[4];//ControlResources.May;
                    break;
                case 6:
                    returnValue = dtfi.MonthNames[5];//ControlResources.June;
                    break;
                case 7:
                    returnValue = dtfi.MonthNames[6];//ControlResources.July;
                    break;
                case 8:
                    returnValue = dtfi.MonthNames[7];//ControlResources.August;
                    break;
                case 9:
                    returnValue = dtfi.MonthNames[8];//ControlResources.September;
                    break;
                case 10:
                    returnValue = dtfi.MonthNames[9];//LocalizedStrings.GetString("October");//ControlResources.October;
                    break;
                case 11:
                    returnValue = dtfi.MonthNames[10];//ControlResources.November;
                    break;
                case 12:
                    returnValue = dtfi.MonthNames[11];//ControlResources.December;
                    break;
            }
            return returnValue;
        }

....

/// 
        /// Apply default template and perform initialization
        /// 
        public override void OnApplyTemplate() {
            base.OnApplyTemplate();
            var previousButton = GetTemplateChild("PreviousMonthButton") as Button;
            if (previousButton != null) previousButton.Click += PreviousButtonClick;
            var nextButton = GetTemplateChild("NextMonthButton") as Button;
            if (nextButton != null) nextButton.Click += NextButtonClick;
            _itemsGrid = GetTemplateChild("ItemsGrid") as Grid;

            //localization
            //http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx
            //Debug.WriteLine("Days of the week for the {0} culture:", currentCulture.Name);
            //for (int ctr = 0; ctr < dtfi.DayNames.Length; ctr++){
            //    Debug.WriteLine("   {0,-2}{1}", dtfi.DayNames[ctr], dtfi.DayNames[ctr] == dtfi.DayNames[(int)dtfi.FirstDayOfWeek] ? "(First Day of Week)" : "");
            //}
            //for (int ctr = 0; ctr < dtfi.MonthNames.Length; ctr++) {
            //    Debug.WriteLine("   {0,-12}", dtfi.MonthNames[ctr]);
            //}
            var txtSunday = GetTemplateChild("txtSunday") as TextBlock;
            if (txtSunday != null) txtSunday.Text = dtfi.DayNames[0].Substring(0, 2);
            var txtMonday = GetTemplateChild("txtMonday") as TextBlock;
            if (txtMonday != null) txtMonday.Text = dtfi.DayNames[1].Substring(0, 2);
            var txtTuesday = GetTemplateChild("txtTuesday") as TextBlock;
            if (txtTuesday != null) txtTuesday.Text = dtfi.DayNames[2].Substring(0, 2);
            var txtWednesday = GetTemplateChild("txtWednesday") as TextBlock;
            if (txtWednesday != null) txtWednesday.Text = dtfi.DayNames[3].Substring(0, 2);
            var txtThursday = GetTemplateChild("txtThursday") as TextBlock;
            if (txtThursday != null) txtThursday.Text = dtfi.DayNames[4].Substring(0, 2);
            var txtFriday = GetTemplateChild("txtFriday") as TextBlock;
            if (txtFriday != null) txtFriday.Text = dtfi.DayNames[5].Substring(0, 2);
            var txtSaturday = GetTemplateChild("txtSaturday") as TextBlock;
            if (txtSaturday != null) txtSaturday.Text = dtfi.DayNames[6].Substring(0, 2);

            BuildDates();
            SetYearMonthLabel();
        }
Coordinator
Mar 25, 2012 at 3:25 PM

This is all correction in version 1.3.6.

Thanks for your feedback