×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Egypt
    Posts
    12

    Question Localizing List of Objects!

    Hi,

    I have A class called Station as follows:

    Code:
        public class Station
        {
    
            private int StationID;
            public int ID
            {
                get { return StationID; }
            }
    
            private string nameArabic;
            public string NameArabic
            {
                get { return nameArabic; }
                set { nameArabic= value; }
            }
    
           private string nameEnglish;
            public string NameEnglish
            {
                get { return nameEnglish; }
                set { nameEnglish= value; }
            }
    
        }
    and I have a list of Stations in ViewModel
    Code:
            public List<Station> Stations{set; get;}
    and I have a ListPicker that binds data from this list and ItemTemplate is:

    HTML Code:
    <DataTemplate x:Name="PickerItemTemplate"> 
                    <Grid> 
                            <TextBlock FontFamily="Segoe WP Light" Text="{Binding NameEnglish}" FontSize="26" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="8,0,0,2" /> 
                    </Grid> 
                </DataTemplate>

    I wanna localize this, it should get NameArabic or NameEnglish based on the language.

    Can I do this using LocalizedStrings?!

    or do you have a better solution ?!


    Thanks in advance

  2. #2
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    297

    Re: Localizing List of Objects!

    Can I know how do you get the stations data ? there might be a better solutions than what you're doing.

    If you don't want to start you code over
    Here is a solution

    Add a new class name it LanguageConverter.cs
    don't forget to replace "YourNamespace" with the one in your app
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Data;
    
    namespace YourNamespace
    {
        public class LanguageConverter: IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var station = value as Station;
                return System.Globalization.CultureInfo.CurrentCulture.Name.Contains("ar") ? station.NameArabic : station.NameEnglish;
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return value;
            }
        }
    }
    in the xaml where your adding the list
    add a to the page or a parent control (in my example I put it in the Grid which is my LayoutRoot )
    Code:
    <Grid.Resources>
                <local:LanguageConverter x:Name="LanguageConverter" />
    </Grid.Resources>
    then in your datatemplate
    Code:
    <DataTemplate x:Name="PickerItemTemplate"> 
                    <Grid> 
                            <TextBlock FontFamily="Segoe WP Light" Text="{Binding, Converter={StaticResource LanguageConverter}}" 
                                             FontSize="26" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="8,0,0,2" /> 
                    </Grid> 
    </DataTemplate>
    try this a give me a feedback.

    PS: didn't try it

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: Localizing List of Objects!

    Why not just use resources?

    Globalization and localization for Windows Phone
    Tips for Localizing Windows Phone 8 XAML Apps - Part1
    Tips for localizing Windows Phone apps – Part 2

    Also, beware of magic strings. They can hurt you.

    I just ran this query:

    Code:
    from c in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    where c.Name.IndexOf("ar", StringComparison.OrdinalIgnoreCase) >= 0
    select new {c.Name, c.DisplayName }
    in LINQPad/ on Windows 8 and got this result:

    Name DisplayName
    ar Arabic
    arn Mapudungun
    ar-SA Arabic (Saudi Arabia)
    arn-CL Mapudungun (Chile)
    ku-Arab-IQ Central Kurdish (Iraq)
    ar-IQ Arabic (Iraq)
    pa-Arab-PK Punjabi (Islamic Republic of Pakistan)
    sd-Arab-PK Sindhi (Islamic Republic of Pakistan)
    ar-EG Arabic (Egypt)
    ar-LY Arabic (Libya)
    ar-DZ Arabic (Algeria)
    ar-MA Arabic (Morocco)
    ar-TN Arabic (Tunisia)
    ar-OM Arabic (Oman)
    ar-YE Arabic (Yemen)
    ar-SY Arabic (Syria)
    ar-JO Arabic (Jordan)
    es-AR Spanish (Argentina)
    ar-LB Arabic (Lebanon)
    ar-KW Arabic (Kuwait)
    ar-AE Arabic (U.A.E.)
    ar-BH Arabic (Bahrain)
    ar-QA Arabic (Qatar)
    pa-Arab Punjabi
    sd-Arab Sindhi (Arabic)
    ku-Arab Central Kurdish (Arabic)

    I know that not all of them are available in Windows Phone - yet - and I'm intentionally using case insensitive comparison, but it's always dangerous.

    This query:

    Code:
    from c in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    where c.Name == "ar" || c.Parent.Name == "ar"
    select new {c.Name, c.DisplayName }
    gives the intended result:

    Name DisplayName
    ar Arabic
    ar-SA Arabic (Saudi Arabia)
    ar-IQ Arabic (Iraq)
    ar-EG Arabic (Egypt)
    ar-LY Arabic (Libya)
    ar-DZ Arabic (Algeria)
    ar-MA Arabic (Morocco)
    ar-TN Arabic (Tunisia)
    ar-OM Arabic (Oman)
    ar-YE Arabic (Yemen)
    ar-SY Arabic (Syria)
    ar-JO Arabic (Jordan)
    ar-LB Arabic (Lebanon)
    ar-KW Arabic (Kuwait)
    ar-AE Arabic (U.A.E.)
    ar-BH Arabic (Bahrain)
    ar-QA Arabic (Qatar)

    And this query:

    Why not just use resources?

    Globalization and localization for Windows Phone
    Tips for Localizing Windows Phone 8 XAML Apps - Part1
    Tips for localizing Windows Phone apps – Part 2

    Also, beware of magic strings. They can hurt you.

    I just ran this query:

    Code:
    from c in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    where c.Name.IndexOf("ar", StringComparison.OrdinalIgnoreCase) >= 0
    select new {c.Name, c.DisplayName }
    in LINQPad/ on Windows 8 and got this result:

    Name DisplayName
    ar Arabic
    arn Mapudungun
    ar-SA Arabic (Saudi Arabia)
    arn-CL Mapudungun (Chile)
    ku-Arab-IQ Central Kurdish (Iraq)
    ar-IQ Arabic (Iraq)
    pa-Arab-PK Punjabi (Islamic Republic of Pakistan)
    sd-Arab-PK Sindhi (Islamic Republic of Pakistan)
    ar-EG Arabic (Egypt)
    ar-LY Arabic (Libya)
    ar-DZ Arabic (Algeria)
    ar-MA Arabic (Morocco)
    ar-TN Arabic (Tunisia)
    ar-OM Arabic (Oman)
    ar-YE Arabic (Yemen)
    ar-SY Arabic (Syria)
    ar-JO Arabic (Jordan)
    es-AR Spanish (Argentina)
    ar-LB Arabic (Lebanon)
    ar-KW Arabic (Kuwait)
    ar-AE Arabic (U.A.E.)
    ar-BH Arabic (Bahrain)
    ar-QA Arabic (Qatar)
    pa-Arab Punjabi
    sd-Arab Sindhi (Arabic)
    ku-Arab Central Kurdish (Arabic)

    I know that not all of them are available in Windows Phone - yet - and I'm intentionally using case insensitive comparison, but it's always dangerous.

    This query:

    Code:
    from c in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    where c.Name == "ar" || c.Parent.Name == "ar"
    select new {c.Name, c.DisplayName }
    gives the intended result:

    Name DisplayName
    ar Arabic
    ar-SA Arabic (Saudi Arabia)
    ar-IQ Arabic (Iraq)
    ar-EG Arabic (Egypt)
    ar-LY Arabic (Libya)
    ar-DZ Arabic (Algeria)
    ar-MA Arabic (Morocco)
    ar-TN Arabic (Tunisia)
    ar-OM Arabic (Oman)
    ar-YE Arabic (Yemen)
    ar-SY Arabic (Syria)
    ar-JO Arabic (Jordan)
    ar-LB Arabic (Lebanon)
    ar-KW Arabic (Kuwait)
    ar-AE Arabic (U.A.E.)
    ar-BH Arabic (Bahrain)
    ar-QA Arabic (Qatar)

    And this query:

    Code:
    from c in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    where c.TwoLetterISOLanguageName == "ar"
    select new {c.Name, c.DisplayName }
    gives the same results.

    So, what you should be testing is if System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "ar".

    Noticed CurrentUICulture instead of CurrentCulture? CurrentUICulture should be used to localize text and CurrentCulture for data formatting (numbers, dates, currency).

    All of this is provided to you by using resources.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Egypt
    Posts
    12

    Re: Localizing List of Objects!

    Quote Originally Posted by Loukt View Post
    Can I know how do you get the stations data ? there might be a better solutions than what you're doing.

    If you don't want to start you code over
    Here is a solution

    Add a new class name it LanguageConverter.cs
    don't forget to replace "YourNamespace" with the one in your app
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Data;
    
    namespace YourNamespace
    {
        public class LanguageConverter: IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var station = value as Station;
                return System.Globalization.CultureInfo.CurrentCulture.Name.Contains("ar") ? station.NameArabic : station.NameEnglish;
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return value;
            }
        }
    }
    in the xaml where your adding the list
    add a to the page or a parent control (in my example I put it in the Grid which is my LayoutRoot )
    Code:
    <Grid.Resources>
                <local:LanguageConverter x:Name="LanguageConverter" />
    </Grid.Resources>
    then in your datatemplate
    Code:
    <DataTemplate x:Name="PickerItemTemplate"> 
                    <Grid> 
                            <TextBlock FontFamily="Segoe WP Light" Text="{Binding, Converter={StaticResource LanguageConverter}}" 
                                             FontSize="26" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="8,0,0,2" /> 
                    </Grid> 
    </DataTemplate>
    try this a give me a feedback.

    PS: didn't try it


    Thanks Loukt for your answer.

    till now, the data will be static.

    Is there a faster way than Converters, because Converters are slow!

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Egypt
    Posts
    12

    Re: Localizing List of Objects!

    Quote Originally Posted by paulo.morgado View Post
    Why not just use resources?

    Globalization and localization for Windows Phone
    Tips for Localizing Windows Phone 8 XAML Apps - Part1
    Tips for localizing Windows Phone apps – Part 2

    Also, beware of magic strings. They can hurt you.

    I just ran this query:

    Code:
    from c in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    where c.Name.IndexOf("ar", StringComparison.OrdinalIgnoreCase) >= 0
    select new {c.Name, c.DisplayName }
    in LINQPad/ on Windows 8 and got this result:

    Name DisplayName
    ar Arabic
    arn Mapudungun
    ar-SA Arabic (Saudi Arabia)
    arn-CL Mapudungun (Chile)
    ku-Arab-IQ Central Kurdish (Iraq)
    ar-IQ Arabic (Iraq)
    pa-Arab-PK Punjabi (Islamic Republic of Pakistan)
    sd-Arab-PK Sindhi (Islamic Republic of Pakistan)
    ar-EG Arabic (Egypt)
    ar-LY Arabic (Libya)
    ar-DZ Arabic (Algeria)
    ar-MA Arabic (Morocco)
    ar-TN Arabic (Tunisia)
    ar-OM Arabic (Oman)
    ar-YE Arabic (Yemen)
    ar-SY Arabic (Syria)
    ar-JO Arabic (Jordan)
    es-AR Spanish (Argentina)
    ar-LB Arabic (Lebanon)
    ar-KW Arabic (Kuwait)
    ar-AE Arabic (U.A.E.)
    ar-BH Arabic (Bahrain)
    ar-QA Arabic (Qatar)
    pa-Arab Punjabi
    sd-Arab Sindhi (Arabic)
    ku-Arab Central Kurdish (Arabic)

    I know that not all of them are available in Windows Phone - yet - and I'm intentionally using case insensitive comparison, but it's always dangerous.

    This query:

    Code:
    from c in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    where c.Name == "ar" || c.Parent.Name == "ar"
    select new {c.Name, c.DisplayName }
    gives the intended result:

    Name DisplayName
    ar Arabic
    ar-SA Arabic (Saudi Arabia)
    ar-IQ Arabic (Iraq)
    ar-EG Arabic (Egypt)
    ar-LY Arabic (Libya)
    ar-DZ Arabic (Algeria)
    ar-MA Arabic (Morocco)
    ar-TN Arabic (Tunisia)
    ar-OM Arabic (Oman)
    ar-YE Arabic (Yemen)
    ar-SY Arabic (Syria)
    ar-JO Arabic (Jordan)
    ar-LB Arabic (Lebanon)
    ar-KW Arabic (Kuwait)
    ar-AE Arabic (U.A.E.)
    ar-BH Arabic (Bahrain)
    ar-QA Arabic (Qatar)


    And this query:

    Code:
    from c in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    where c.TwoLetterISOLanguageName == "ar"
    select new {c.Name, c.DisplayName }
    gives the same results.

    So, what you should be testing is if System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "ar".

    Noticed CurrentUICulture instead of CurrentCulture? CurrentUICulture should be used to localize text and CurrentCulture for data formatting (numbers, dates, currency).

    All of this is provided to you by using resources.
    What is the relation between your answer and my questions ?!!


  6. #6
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    297

    Re: Localizing List of Objects!

    the static data is stored in xml files or hardcoded?

    if it's stored in xml and you can still edit it then make two files one for english and another for arabic

    stations.ar.xml and stations.en.xml

    and when loading your files
    (assuming you have ResourceLanguage identifier in your AppResouces which is mandatory in WP8)
    Code:
    var stationsElements = XDocument.Load("YourPath/stations." + AppResources.ResourceLanguage + ".xml");
    Your Station class code will be

    Code:
    public class Station
        {
    
            private int StationID;
            public int ID
            {
                get { return StationID; }
            }
    
            private string name;
            public string Name
            {
                get { return name; }
                set { name= value; }
            }
    
        }
    and you DataTemplate
    Code:
    <DataTemplate x:Name="PickerItemTemplate"> 
                    <Grid> 
                            <TextBlock FontFamily="Segoe WP Light" Text="{Binding Name}" FontSize="26" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="8,0,0,2" /> 
                    </Grid> 
                </DataTemplate>

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: Localizing List of Objects!

    You might not see the immediate relation if you haven't gone through the links at the top.

    It might not be the answer to your question, but it's the recommend answer to your problem.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Location
    Egypt
    Posts
    12

    Re: Localizing List of Objects!

    Thanks, paulo.morgado

    Thanks, Loukt

Similar Threads

  1. Localizing non-gui application
    By StasV in forum Symbian
    Replies: 2
    Last Post: 2009-03-25, 08:19
  2. Localizing an application
    By janekw_ in forum Symbian
    Replies: 4
    Last Post: 2007-10-06, 17:25
  3. Sending List Objects using Bluetooth
    By seanmcgowan in forum Mobile Java General
    Replies: 6
    Last Post: 2007-03-19, 14:37
  4. Bubble sort a list of linked objects!
    By hclemson in forum Symbian
    Replies: 2
    Last Post: 2005-05-15, 13:39
  5. Sending List Objects using Bluetooth
    By seanmcgowan in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2004-11-01, 15:12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×