3
votes

I have an ASP.NET MVC Razor website which is supposed to be live in multiple countries having different cultures and hence the languages. My Development team is concerned about english only and whole text on UI pages is also written in plain english. I want this english text to be converted into culture specific language. I'm using resource file to manage the strings on my website.

One way is to create multiple resource files based on each language and then using each file based on specific culture. This thing needs to be managed manually. If someone has done this thing please come up with any reference or any sample code for this implementation.

If there is any way where I can automate this thing then it will be best way to go for a multi lingual website. Like as culture can be easily detected by user IP address and based on the culture I should be able to convert all english based text into current culture specific language.

1

1 Answers

3
votes

1. Automatically Use User-Culture

Have the user culture be set automatically on the current Thread/HttpContext. In your Web.Config:

<system.web>
    ...
    <globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto" />
    ...
</system.web>

2. Helper Function

Introduce a global method that will translate input text using the appropriate resource:

public static class Resources
{
    public static string GetResource(string key, params object[] data)
    {
        if (String.IsNullOrEmpty(key))
            return key;

        // the actual call to your Resources
        var res = Texts.ResourceManager.GetString(key.ToUpper(), Thread.CurrentThread.CurrentUICulture);

        if (String.IsNullOrEmpty(res))
            return data != null && data.Length > 0 ? String.Format(key.ToUpper(), data) : key;

        if (data != null && data.Length > 0)
            return String.Format(res, data);

        return res;
    }
}

The method also lets you pass an additional (optional) parameters to be used in a String.Format way. For example:

// in your Resource file (Texts.es.resx)
GREETING_TEXT: "Hola amigo {0}, el tiempo es {1}"

// invocation
Resources.GetResource("GREETING_TEXT", "Chaim", DateTime.Now);

3. Controller Helper:

Introducing a _ methods that will let you translate texts quickly in the Controller:

public class BaseController : Controller
{
    public string _(string key, params object[] data)
    {
        return Resources.GetResource(key, null, data);
    }
}

In your controller you have to make sure you're inheriting your BaseController and use it as follows:

public HomeController : BaseController:
{
    public ActionResult GreetMe()
    {
        var msg = _("GREETING_TEXT", this.User, DateTime.Now);
        return Content(msg);
    }
}

4. Razor Helper

And for your Razor pages:

// non-generic version (for model-less pages)
public abstract class BaseWebPage : WebViewPage
{
    public string _(string key, params object[] data)
    {
        return Resources.GetResource(key, null, data);
    }
}

// generic version (for model defined pages)
public abstract class BaseWebPage<T> : WebViewPage<T>
{
    public string _(string key, params object[] data)
    {
        return Resources.GetResource(key, null, data);
    }
}

Now we have to set this new base WebPage as the base type for our pages in ~/Views/Web.Config:

<system.web.webPages.razor>
    ...
    <pages pageBaseType="Full.NameSpace.ToYour.BaseWebPage">
    ...
</system.web.webPages.razor>

(If you're using Areas, you'll have to modify each ~/Areas/AREA_NAME/Views/Web.Config as well)

You can now use it in your Razor pages:

<h1>@_("HELLO")</h1>