10

Adding Multilanguage Support To Your Objects

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

Introduction

In todays world localization became a must requirement in applications that we develop. We can easily achieve this goal by using recource files for static resources such as exception messages, label texts etc. However using localized data fetched from a data source will be a problem. Since the data is dynamic this problem cannot be solved with resource files.

Using the code

Most developers solve this problem by using a design like below.

1000002697_1.jpg 

 

If we look at the design we see that we are creating a new object for each property that contains multilanguage data. All these objects cause developer to write more code, also will make the application code less managable. Also if the developer is using an ORM tool s/he will end up creating lots of mapping.

 

Now let’s think from another perspective and try to implement a design like below.

1000002697_2.jpg 

 

As you can see this design is much simpler to understand and has high reusability. You can use the MultilanguageProperty to store multilanguage data at a property of an object. 

Now let’s see how we can write this code from design. 

First of all let’s code the Language class that we will use in MultilanguageProperty. 

 

Collapse | Copy Code
    public class Language
    {
        public enum ApplicationLanguage
        {
            Turkish,
            English,
        }
        public static ApplicationLanguage GetDefaultLanguage()
        {
            return ApplicationLanguage.Turkish;
        }
    }

 

 

 

 

 

As you can see we define the languages we want to use in an enum. And we implement a method that is responsible for returning the default language. This method can return default language per session or per user based on out implementation. 

Let’s start coding MultilanguageProperty object. 

 

Collapse | Copy Code
    public class MultilanguageProperty<T>:IEnumerable<KeyValuePair<Language.ApplicationLanguage,T>>
    {
        public IDictionary<Language.ApplicationLanguage, T> Values { get; set; }
        public MultilanguageProperty()
        {
            Values = new Dictionary<Language.ApplicationLanguage, T>();
        }

 

 

First of all to use our object with any type we are making the object generic. And to allow users iterate through the child items in this object we implement IEnumerable interface as above.  

Declaring a property whose type is IDictionary is the main idea of this implementation. This Dictionary will store the data based on the language value. As you can see IDictinary is a generic type of ApplicationLanguage enum as key and T generic type as value. And in the constructor of our code we create an instance of this Dictionary. 

Now let’s implement some code to access the data stored in Values property. 

 

Collapse | Copy Code
        public T GetValue(Language.ApplicationLanguage language)
        {
            return (T)Values[language];
        }
        public T GetValue()
        {
            Language.ApplicationLanguage applicationLanguage = Language.GetDefaultLanguage();
            return (T)Values[applicationLanguage];
        }
        public T this[Language.ApplicationLanguage l]
        {
            get
            {
                return Values[l];
            }
            set
            {
                Values[l] = value;
            }
        }
        public static implicit operator T(MultilanguageProperty<T> instance)
        {
            if (instance == null)
            {
                return default(T);
            }
            return instance.GetValue();
        }
        public static implicit operator List<KeyValuePair<Language.ApplicationLanguage,T>>(MultilanguageProperty<T> instance)
        {
            if (instance == null)
            {
                return null;
            }
            return instance.Values.ToList();
        }
        #region IEnumerable<KeyValuePair<ApplicationLanguage,T>> Members

        public IEnumerator<KeyValuePair<Language.ApplicationLanguage, T>> GetEnumerator()
        {
            return Values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Values.GetEnumerator();
        }

        #endregion
    }
}

 

As you can see two overloads of GetValue method is developed. One of them returns the value for default language and the other returns the value for the language passed as parameter. 

Also there is an indexer property to access values by an indexer. And there is an implicit operator that is defined to access the default language value without using any cast or convert function. 

Now let’s create a test application. 

 

Collapse | Copy Code
    public class TestObject
    {
        public TestObject()
        {
            this.Name = new MultilanguageProperty<string>();
        }
        public int Id { get; set; }
        public string UniversalCode { get; set; }
        public MultilanguageProperty<string> Name { get; set; }
    }
   class Program
    {
        public static List<TestObject> GetSampleDataForTestObject()
        {
            List<TestObject> listTestObject = new List<TestObject>();

            TestObject testObject1 = new TestObject { Id = 1, UniversalCode = "TR" };
            testObject1.Name.Values.Add(Language.ApplicationLanguage.Turkish, "Türkiye");
            testObject1.Name.Values.Add(Language.ApplicationLanguage.English, "Turkey");

            TestObject testObject2 = new TestObject { Id = 2, UniversalCode = "USA" };
            testObject2.Name.Values.Add(Language.ApplicationLanguage.Turkish, "Amerika Birleşik Devletleri");
            testObject2.Name.Values.Add(Language.ApplicationLanguage.English, "United States Of America");

            TestObject testObject3 = new TestObject { Id = 3, UniversalCode = "FR" };
            testObject3.Name.Values.Add(Language.ApplicationLanguage.Turkish, "Fransa");
            testObject3.Name.Values.Add(Language.ApplicationLanguage.English, "France");

            listTestObject.Add(testObject1);
            listTestObject.Add(testObject2);
            listTestObject.Add(testObject3);

            return listTestObject;
        }
        static void Main(string[] args)
        {

            List<TestObject> listTestObject = GetSampleDataForTestObject();
            WriteAllList(listTestObject);
            Console.WriteLine();
            WriteForSpecificLanguageByIndexer(listTestObject);
            Console.WriteLine();
            WriteForSpecificLanguageByGetValue(listTestObject);
            Console.WriteLine();
            WriteForDefaultLanguageByIndexer(listTestObject);
            Console.WriteLine();
            WriteForDefaultLanguageByGetValue(listTestObject);
            Console.WriteLine();
            WriteForDefaultLanguageByImplicit(listTestObject);
            Console.Read();
        }
        private static void WriteAllList(List<TestObject> listTestObject)
        {
            Console.WriteLine("Writing All List");
            foreach (TestObject to in listTestObject)
            {
                Console.WriteLine("Id:" + to.Id + " Code:" + to.UniversalCode);
                foreach (KeyValuePair<Language.ApplicationLanguage, string> kvp in to.Name)
                {
                    Console.WriteLine("     Language:" + kvp.Key.ToString() + " Name:" + kvp.Value.ToString());
                }
            }
        }
        private static void WriteForSpecificLanguageByIndexer(List<TestObject> listTestObject)
        {
            Console.WriteLine("Writing Specific Language Value By Indexer");
            string s = listTestObject[0].Name[Language.ApplicationLanguage.English];
            Console.WriteLine(s);
        }
        private static void WriteForSpecificLanguageByGetValue(List<TestObject> listTestObject)
        {
            Console.WriteLine("Writing Specific Language Value By GetValue Method");
            string s = listTestObject[0].Name.GetValue(Language.ApplicationLanguage.English);
            Console.WriteLine(s);
        }
        private static void WriteForDefaultLanguageByIndexer(List<TestObject> listTestObject)
        {
            Console.WriteLine("Writing Default Language Value By Indexer");
            string s = listTestObject[1].Name[Language.GetDefaultLanguage()];
            Console.WriteLine(s);
        }
        private static void WriteForDefaultLanguageByGetValue(List<TestObject> listTestObject)
        {
            Console.WriteLine("Writing Default Language Value By GetValue Method");
            string s = listTestObject[1].Name.GetValue();
            Console.WriteLine(s);
        }
        private static void WriteForDefaultLanguageByImplicit(List<TestObject> listTestObject)
        {
            Console.WriteLine("Writing Default Language Value By Implicit Operator");
            string s = listTestObject[2].Name;
            Console.WriteLine(s);
        }

    }

And here is the output for the test application.

1000002697_3.jpg 

Please feel free to contact me for any questions.

prajapat