Asma Gheisari
Asma Gheisari

Reputation: 6294

asp.net MVC source files for internationalization

Based on this tutorial I'm trying to have Internationalization capability in my project. I have these resources files for saving words in different languages:

resources.resx --> for default language (en-US)

resources.fa.resx --> for farsi language

resources.es.resx --> for spanish language

words like fa and es shows the culture.

in views I have replaced words with their equal in resource files in this way :

<a href="#" >@Resources.IranNewsStand</a>

Edit: I've implemented all of the logic based on the tutorial.but I have one view for all of the languages and in this view I'm using resources.resx . Is it a correct logic?

My question is that how my project knows to load which resource file based on the value of Thread.CurrentThread.CurrentCulture ? What did I miss?

Edit: I've implemented these steps:

1-I have a Class Library Project named Resources containing three mentioned resx files(resources.resx,resources.fa.resx,resources.es.resx).

2-Resource project is added in my mvc application as a reference.

3-controllers inherits this Base Controller :

public class BaseController : Controller
{
    protected override void ExecuteCore()
    {
        string cultureName;
        HttpCookie cultureCookie = Request.Cookies["_culture"];
        if (cultureCookie != null)
            cultureName = cultureCookie.Value;
        else
            cultureName = Request.UserLanguages[0];
        cultureName = utilities.CultureHelper.GetValidCulture(cultureName);
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
        base.ExecuteCore();
    }
}

4-as I said in view I have used resource strings from resource.resx file that contains the default language(en-US) strings:

<p>@Resources.Resources.Home</p>

5-in view I have a link that when clicking on it,this jquery code is run:

    <script type="text/javascript">
        $("#btnChangeLanguage").click(function () {
            $.cookie("_culture", "fa-IR", { expires: 365, path: '/' });
            window.location.reload(); // reload 
        })
    </script>

fa-IR is a culture selected by user.but when clicking on this link the language doesn't change.

Edit:Solution

I found 2 problems in my project that solving them made everything ok:

1-jQuery cookie plugin was required to have jquery code work correctely:

<script type="text/javascript" src="~/Scripts/jquery.cookie.js" ></script>

2-the ExecuteCore event in BaseController wouldn't fire and that sounds like it was a problem in asp.net MVC 4 .So based on this question I tryed to override OnActionExecuted instead.

Upvotes: 0

Views: 973

Answers (1)

Eilon
Eilon

Reputation: 25704

The logic of pulling the resource based on the current culture is built into .NET itself.

There are two steps to this:

  1. The current culture needs to be set appropriately in the request. ASP.NET has some built-in mechanisms to do this, such as what is described in the article you linked to.
  2. Once that is set, the .NET Framework will use the request's current culture to load the appropriate resource. If the requested locale is not available, the culture neutral resource will be loaded (the "fallback").

Upvotes: 2

Related Questions