Question

I have a website that reads some of its content from a database, I need this website in both languages, English and Arabic.

the needed content is duplicated in the database in both languages. lets say I have a En_Name and Ar_Name columns in my database.

and for example for the Arabic version of the website a link will display a text from Ar_Name , and with the English one it should display the text from the En_Name.

for the static content in my website I think it is a good idea to use the ASP.NET default localization using (.resx files). but what I don't know is how to do the localization for the dynamic section of the website.

So, how can I make the same hyperlink read once from the Ar_Name field, and then from the En_Name based on the users choice (Localization)?

Was it helpful?

Solution

There are many ways to accomplish this. You've not mentioned which database technology you are using, so my example is with Entity Framework. You may need to customise this to your own situation.

Something similar may be possible with LinqToSql or other ORMs. If you are using something else entirely, then the key is to have a central class that you pass something consistent to (hence the interface) that does the translation.

For example, if I was using Entity Framework, every table in the database that had these two fields I'd add an interface that exposes those fields. Then I'd have a helper class with a method that took any entity with that interface and checked the current localisation and return the correct version of the text.

public interface IBilingualEntity
{
    // Defines a consistent interface that indicates which language version
    // each thing is in.
    string Ar_Name { get; }
    string En_Name { get; }
}

public partial MyEntity : IBilingualEntity
{
    // This is a class generate by Entity Framework. But it could
    // be anything really if you are using some other framework.
    //
    // Nothing to do here as the other side of the partial
    // should already implement the interface with the code
    // generated from Entity Framework. If not, implement the
    // interface and return the correct language version in
    // the property.
}

// This is the helper that works out which language version to use.
public class BilingualHelper
{
    public string GetName(IBilingualEntity entity)
    {
        // NOTE: You may have to strip away the region part of the name
        // but off the top of my head I can't remember the format.
        // If you are using something else to store the culture you'll 
        // have to reference that instead.
        var cultureName = Thread.CurrentThread.CurrentUICulture.Name;
        if (cultureName == "ar")
            return entity.Ar_Name;
        return entity.En_Name;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top