0

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.

5
  • 1
    Are you saying that the internationalization is not working? The loaded resource will be selected based on the culture string provided by your browser. Commented Apr 9, 2013 at 17:33
  • but I'm using strings in resources.resx in views,that means the default language.Is it correct? Commented Apr 9, 2013 at 17:39
  • Can you check if the cookie is being seen by the server? Also, in the view page try rendering @System.Diagnostics.Thread.CurrentThread.CurrentCulture just to check if it is still set there too. Commented Apr 10, 2013 at 6:09
  • @Eilon I checked It out, System.Threading.Thread.CurrentThread.CurrentCulture always returns en-US , even after clicking on the link that runs the jquery code to save another culture in cookie. Commented Apr 10, 2013 at 6:48
  • I just found out that the cookie value is always set by preffered language in browser options!!! Commented Apr 10, 2013 at 7:05

1 Answer 1

2

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").
Sign up to request clarification or add additional context in comments.

1 Comment

@asma nothing looks wrong in what you;re doing. If something isn't working, please edit the question to state exactly what isn't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.