0

I had simple controller with name HomeController and some actions like

public ActionResult Index()
{
    return View();
}

and my index page is

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Test
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
Hello World,<br/>
Test
</asp:Content>

I want replace words in returned result its means if my index page returned "Hello World", i want change it to "Hola Mundo" or something like this..

What should i do for globalize my project? i seen http://afana.me/post/aspnet-mvc-internationalization.aspx and i don`t like create view for every single culture.. i want create only one index view and page translate to selected culture.

Mehdi Yeganeh
  • 2,019
  • 2
  • 24
  • 42
  • possible duplicate of [Globalization in ASP.Net MVC 3](http://stackoverflow.com/questions/5509965/globalization-in-asp-net-mvc-3) – Robert Levy May 11 '13 at 12:28

3 Answers3

1

You're forgetting about the "Model" of Model-View-Controller.

Essentially you set up a simple Model class that has the variables you want to set. You set them somewhere in your controller and your View displays those variables.

Very simplistic example to follow...

public class MyModel
{
   public string sayingOne { get; set; }
   public string sayingTwo { get; set; }
}

in your controller

public ActionResult Index()
{
    MyModel model = new MyModel();
    if (someCondition)
    {
        model.sayingOne = "Hello";
        model.sayingTwo = "Mundo";
    }
    return View(model);
}

then in your View

@Model MyModel
...    
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
@MyModel.sayingOne @MyModel.sayingTwo
</asp:Content>
Kyle C
  • 1,627
  • 12
  • 25
  • tanx .. its true but i want to globalize my project and show result for selected language by user and i don`t want make a view for every culture i only want to make resource for every culture and all my actions views show results with selected language. please check http://afana.me/post/aspnet-mvc-internationalization.aspx i want do same jobs but i don`t like to make view for every culture.. i`d like make one view ( page ) and translate words in it for users... – Mehdi Yeganeh May 10 '13 at 22:56
1

This article should get you started. I disagree with the author's premise that you should create a view for each language (talk about violating DRY), but the other aspects of globalization should point you in the right direction.

There's a lot to your question and it's not something that someone's easily going to be able to convey in the context of an SO answer. I'd search for blogs/tutorials on MVC and globalization.

Scott
  • 13,735
  • 20
  • 94
  • 152
  • +1 but please give a break down of the article you are linking to otherwise if the link goes dead, your answer isn't any good anymore. – Charlino May 11 '13 at 00:00
1

This question was asked and answered here: Globalization in ASP.Net MVC 3. Not sure why the right answer (that community voted for 27 times) wasn't selected as correct one, but this is exactly the way to globalize / localize your ASP.NET MVC project.

Copy paste:

You localize it in the same way as any other application like this:

  1. Create a folder, call it e.g. Resources
  2. Right click the folder and add class... choose resource file. Call it anything you like e.g. Strings.resx
  3. Under the properties of file, change Custom Tool to be PublicResXFileCodeGenerator
  4. Populate the Resource file with Translation key and value pairs (this will be the default translation)
  5. Create other resources with the name of the culture they're for in this format: {name}.de.resx e.g. Strings.de.resx
  6. (This is for Razor) crack open the web.config in the Views folder and add this to /configuration/system.web.webPages.razor/pages/namespaces: (assuming resources is the name of the folder you created the resources in and you haven't changed the default namespace on the resouce files themselves). This step means you don't have to fully qualify the resource classes in your views each time you want to reference a translation.
  7. Use the translations in place of text in your views like with the following code:

    @Strings.MyString
    

Strings will be automatically translated in the view depending on CultureInfo.CurrentCulture but this is not set automatically for you

You will need to change the CurrentCulture (potentially in Application_BeginRequest). How you do this is up to you, it could be a route value which sets it or you can read the user's browser language

You can find a list of the user's prefered languages (in order) in HttpContext.Current.Request.UserLanguages.

You need not to create a view per culture, you create view models with palceholders that will take their values from resources and you create resource (list of strings) per culture, rather than view per culture.

Hope this will help you

Community
  • 1
  • 1
Display Name
  • 4,672
  • 1
  • 33
  • 43