There are a couple ways to achieve this. If you are not using ASP.NET folders (App_GlobalResources), open your resource file and at the top of the file is a menu. Open the drop-down list labeled "Access Modifier" and change it to public. Save the file and
then open the designer.cs file for your Resource file and verify the namespace, or open the properties window for your resx file and set the namespace to the desired value. Now, depending on what your namespace is you can do this:
<%=Namespace.PrivateDetails.label_FirstName%>
Or if you have your resx file in App_GlobalResources, try this:
1) Create an App_Resources folder (right click project --> Add --> ASP.NET Folder --> App_GlobalResources)
2) Add a new Resource file to App_GlobalResources
3) Add your resources
Your resource designer file will generate a class in the "Resources" namespace. Now you'll be able to reference your resource file from your view. For some reason I don't always get Intellisense for the generated class file, but once I type the class name
in corrrectly I'll get intellisense for the resource keys. Example:
Type "<%=Res" and you'll see "Resources" namespace in the intellisense list. Select "Resources" from the list, then start typing the name of your resource class (ex. if the file were named "MyAppResources") and you won't get any intellisense. But after I
have typed "<%=Resources.MyAppResources." I get a list of resource keys in my resource file.
This has something to do with the fact that the generated class uses the "internal" modifier (C#), but for some reason you cannot change the access modifier for a resource file in App_GlobalResources.
If you are using App_LocalResources, then the instructions are the same as the first option, just change the access modifier to Public and you'll be fine.
Check out my blog for ASP.NET MVC localization covering all aspects including DisplayName localization, Validation, using Routing (storing culture name in URL), issues with output cache and so on..
throw
new
ApplicationException("DefaultLanguage key not in web.config");else
{
cultureInfo = Session[
}
SetThreadInfo(cultureInfo);
}
{
"CultureInfo"].ToString();private
static
void SetThreadInfo(string cultureInfo)//Ensure
that the cultureInfo parameter isn't empty
{
}
{
}
}
if (cultureInfo ==
"")CultureInfo objCultureInfo =
new
CultureInfo(cultureInfo);if (!objCultureInfo.IsNeutralCulture)Thread.CurrentThread.CurrentCulture
= objCultureInfo;Thread.CurrentThread.CurrentUICulture =
new
CultureInfo(cultureInfo);
8. Add a class file in ur project to declare all sesssion. You can assign & use them any where in the project.
public class SessionConstants
{
private const string conSelectedCulture = "SelectedCulture";
private const string conDefaultCulture = "DefaultCulture";
#region SetUICultureThread
///<para> Description : Stores the User Selected Language for UI display </para>
///<para> Author : Navish Rampal</para>
///<para> Date : 24/12/2010 ddmmyyyy</para>
public static string SelectedCulture
{
get
{
if (HttpContext.Current.Session[conSelectedCulture] != null)
return HttpContext.Current.Session[conSelectedCulture].ToString();
else
return null;
}
set
{
HttpContext.Current.Session[conSelectedCulture] = value;
}
}
#endregion
#region DefaultCulture
///<para> Description : On Application Load this seesion gets populated with the Default Culture from Web Config if No culture is Selected by User to display the UI </para>
///<para> Author : Navish Rampal</para>
///<para> Date : 24/12/2010 ddmmyyyy</para>
public static string DefaultCulture
{
get
{
if (HttpContext.Current.Session[conDefaultCulture] == null)
{
HttpContext.Current.Session[conDefaultCulture] = ConfigurationManager.AppSettings[CommonConstants.conDefaultCulture].ToString();
}
return HttpContext.Current.Session[conDefaultCulture].ToString();
}
set
{
HttpContext.Current.Session[conSelectedCulture] = value;
}
}
#endregion
<script language="javascript" type="text/javascript">
// This Script call the Controller class to select the Thread for UI Culture selected by user
function SetCulture() {
Localization is a .Net feature, not an asp.net or Mvc feature, so it works also with Mvc.
The way you specify what culture to use for the web site or for a single page is exactly the same in asp.net and mvc...This is the only part that is pecific to web application, the remainder is standard .Net.
The only point is that some asp.net server controls (such as a label for instance) can be configured to take their text from a resource file from visual studio designer in classical asp.net(by using the property tab), in case of mvc yo need to put manually
the string exposed as property of the resource file....but this is easy as shhown in some other posts of this thread..
The issue is quite simple to implement...no need to do complex things.
this way the culture used in the request will be taken from the user browser
2) if you want you can override the values detected by the system by letting the user choose a culture from a dropdown and storing it in a cookie or Session Variable.
3) To override the culture just use Thread.Current...that contains ponters to the current Culture and UIculture. The Culture is used to decide how to format numbers, dates and so on, while the UICultue is used to choose among the various resources files associted
to the web site, and thus determines the languages of all Text and messages.
3) Just use NameofResource.StringName to retrieve a message or text you gave the name "StringName", then .Net will automatically select the resource associated to the current UICulture.
4) Use this string in your views by using <%:......%>
The above is a general description for asp.net...Mvc has nothing different...the only difference is that you cannot select the resource text from the property browser...but you have to insert it in the view as at point 4).
Putting everything in a resource file is pretty hard when you are thinking about formatting and scripting and stuff. This generally means that you either have to start putting html tags in the resource file, or have many many resource values. Also for a
page that is just a lot of content such as a "Terms and Conditions" page, that can become a very big hassle.
Our approach is to use the right tool for the right job. Bigger things which require more code content, we placed those in seperate files inside a folder for the right culture. So a page like "Terms" would go in "~/Views/en-US/About/Terms.aspx" as well as
"~/Views/pt-BR/About/Terms.aspx". Each culture would have its own file. Combining resource file values with culture specific pages and partial rendering of shared pages makes it pretty simple to include globalization.
The approach is pretty simply, but I already wrote a blog about it so, just see it here:
Obviously...Resoruce files are for label of fields, maybe titles, error messages, and similar small things. If you have a full page of text content...it is better to create a localized version of the whole page and then adding the culture name in the URL...as
most of websites do.
miguelfteixe...
Member
2 Points
2 Posts
Re: MVC & Globalization / Localization
May 20, 2009 02:43 PM|LINK
With some minor changes you can create a Html helper based on this:
http://blog.eworldui.net/post/2008/05/ASPNET-MVC---Localization.aspx
I think that is a cleaner solution.
developmenta...
Member
115 Points
65 Posts
Re: MVC & Globalization / Localization
May 20, 2009 04:15 PM|LINK
There are a couple ways to achieve this. If you are not using ASP.NET folders (App_GlobalResources), open your resource file and at the top of the file is a menu. Open the drop-down list labeled "Access Modifier" and change it to public. Save the file and then open the designer.cs file for your Resource file and verify the namespace, or open the properties window for your resx file and set the namespace to the desired value. Now, depending on what your namespace is you can do this:
<%=Namespace.PrivateDetails.label_FirstName%>
Or if you have your resx file in App_GlobalResources, try this:
1) Create an App_Resources folder (right click project --> Add --> ASP.NET Folder --> App_GlobalResources)
2) Add a new Resource file to App_GlobalResources
3) Add your resources
Your resource designer file will generate a class in the "Resources" namespace. Now you'll be able to reference your resource file from your view. For some reason I don't always get Intellisense for the generated class file, but once I type the class name in corrrectly I'll get intellisense for the resource keys. Example:
Type "<%=Res" and you'll see "Resources" namespace in the intellisense list. Select "Resources" from the list, then start typing the name of your resource class (ex. if the file were named "MyAppResources") and you won't get any intellisense. But after I have typed "<%=Resources.MyAppResources." I get a list of resource keys in my resource file.
This has something to do with the fact that the generated class uses the "internal" modifier (C#), but for some reason you cannot change the access modifier for a resource file in App_GlobalResources.
If you are using App_LocalResources, then the instructions are the same as the first option, just change the access modifier to Public and you'll be fine.
Alik.Adamyan
Member
12 Points
1 Post
Re: MVC & Globalization / Localization
Jul 29, 2010 02:52 PM|LINK
Check out my blog for ASP.NET MVC localization covering all aspects including DisplayName localization, Validation, using Routing (storing culture name in URL), issues with output cache and so on..
ASP.NET MVC localization
localization routing "ASP.NET MVC 2" "display name
findnavish
Member
17 Points
7 Posts
Re: MVC & Globalization / Localization
Sep 23, 2010 01:31 PM|LINK
That really works... Thanks you...
Resource1 is my Resource File Name and lblName is the Key. And it works fine.
Even works if you have multiple Resource files
<span><%= Resources.Resource1.lblName%></span>
Navish Rampal
http://findnavish.blogspot.com/
Findnavish@gmail.com
http://findnavish.blogspot.com
http://findnavish.livejournal.com
findnavish
Member
17 Points
7 Posts
Re: MVC & Globalization / Localization
Sep 23, 2010 01:34 PM|LINK
Setting the Culture ..
Add the code in Global.ascx
{
{
{
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)if (HttpContext.Current.Session != null)if (Session["CultureInfo"] != null)//Culture is populatedSetCultureInfo(
}
true);else{
//Culture isn't populatedSetCultureInfo(
}
}
}
{
{
{
cultureInfo = System.Configuration.
}
false);private void SetCultureInfo(bool blnFlag)string cultureInfo = "";if (!blnFlag)if (System.Configuration.ConfigurationManager.AppSettings["DefaultLanguage"] != null)ConfigurationManager.AppSettings["DefaultLanguage"].ToString();else{
}
}
throw new ApplicationException("DefaultLanguage key not in web.config");else{
cultureInfo = Session[
}
SetThreadInfo(cultureInfo);
}
{
"CultureInfo"].ToString();private static void SetThreadInfo(string cultureInfo)//Ensure that the cultureInfo parameter isn't empty{
}
{
}
}
if (cultureInfo == "")CultureInfo objCultureInfo = new CultureInfo(cultureInfo);if (!objCultureInfo.IsNeutralCulture)Thread.CurrentThread.CurrentCulture = objCultureInfo;Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureInfo);http://findnavish.blogspot.com
http://findnavish.livejournal.com
findnavish
Member
17 Points
7 Posts
Re: MVC & Globalization / Localization
Dec 27, 2010 04:32 AM|LINK
Globalizing your Project.
1. Add the App_App_GlobalResources Folder in the Project.
2. Add the resource File , For example Translation.resx and Translation.zh-CN.resx
3. Add few enteries in it.
4. In Global.ascx file add the following events
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
if (HttpContext.Current.Session != null)
{
if (SessionConstants.SelectedCulture != null)
{
// IF Language Selected by User
SetCulture(true);
}
else
{
SetCulture(false);
}
}
}
5. SessionConstants.SelectedCulture will be null if user has not selected any user culture. Then load the sire with default Culture.
6. Add this also in Global File
private void SetCulture(bool blnFlag)
{
string cultureInfo = string.Empty;
if (blnFlag)
{
cultureInfo = SessionConstants.SelectedCulture;
}
else
{
if (!string.IsNullOrEmpty(SessionConstants.DefaultCulture))
{
cultureInfo = SessionConstants.DefaultCulture;
}
else
{
SessionConstants.DefaultCulture = cultureInfo = ConfigurationManager.AppSettings[CommonConstants.conDefaultCulture].ToString();
}
}
SetThreadInfo(cultureInfo);
}
private void SetThreadInfo(string cultureInfo)
{
CultureInfo objCultureInfo = new CultureInfo(cultureInfo);
Thread.CurrentThread.CurrentCulture = objCultureInfo;
Thread.CurrentThread.CurrentUICulture = objCultureInfo;
}
7. Add the key in web.config
<appSettings>
<add key="defaultCulture" value="en-US" />
</appSettings>
8. Add a class file in ur project to declare all sesssion. You can assign & use them any where in the project.
public class SessionConstants
{
private const string conSelectedCulture = "SelectedCulture";
private const string conDefaultCulture = "DefaultCulture";
#region SetUICultureThread
///<para> Description : Stores the User Selected Language for UI display </para>
///<para> Author : Navish Rampal</para>
///<para> Date : 24/12/2010 ddmmyyyy</para>
public static string SelectedCulture
{
get
{
if (HttpContext.Current.Session[conSelectedCulture] != null)
return HttpContext.Current.Session[conSelectedCulture].ToString();
else
return null;
}
set
{
HttpContext.Current.Session[conSelectedCulture] = value;
}
}
#endregion
#region DefaultCulture
///<para> Description : On Application Load this seesion gets populated with the Default Culture from Web Config if No culture is Selected by User to display the UI </para>
///<para> Author : Navish Rampal</para>
///<para> Date : 24/12/2010 ddmmyyyy</para>
public static string DefaultCulture
{
get
{
if (HttpContext.Current.Session[conDefaultCulture] == null)
{
HttpContext.Current.Session[conDefaultCulture] = ConfigurationManager.AppSettings[CommonConstants.conDefaultCulture].ToString();
}
return HttpContext.Current.Session[conDefaultCulture].ToString();
}
set
{
HttpContext.Current.Session[conSelectedCulture] = value;
}
}
#endregion
}
9. Now u are ready to use the Resources.
<label id="lblHomePageHeader">
<%= Resources.Translation.lblHomePageHeader%>
</label>
10 can also have a page to select the Culture
<td align="right">
<% using (Html.BeginForm())
{ %>
<label id="lblLanguage">
<%= Resources.Translation.lblLanguage%>
</label>
<select id="ddlCulture">
<option value="Select">
<%= Resources.Translation.ddlCultureSelectLanguage%></option>
<option value="en-US">
<%= Resources.Translation.ddlCultureEnglish%></option>
<option value="zh-CN">
<%= Resources.Translation.ddlCultureChinese%></option>
<option value="jp-JP">
<%= Resources.Translation.ddlCultureJapanese%></option>
</select>
<input type="image" id="imgSet" alt="Set Culture" onclick="SetCulture()" style="vertical-align: middle;"
height="17px" src="../../Images/arrow_head.gif" title='<%= Resources.Translation.ddlCultureJapanese%>' />
<% } %>
</td>
Put this in the head
<script language="javascript" type="text/javascript">
// This Script call the Controller class to select the Thread for UI Culture selected by user
function SetCulture() {
var culture = document.getElementById('ddlCulture').value;
if (culture == "Select") {
alert('<%= Resources.Translation.lblLanguageAlert%>');
return;
}
$.ajax({
type: "POST",
url: "/HomePage/SetCulture",
data: "culture=" + culture
});
}
</script>
10. Controller will have the following logic
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SetCulture(string culture)
{
SessionConstants.SelectedCulture = culture; // Setting the Selected Culture in Session
return View();
}
http://findnavish.blogspot.com
http://findnavish.livejournal.com
francesco ab...
All-Star
20912 Points
3279 Posts
Re: MVC & Globalization / Localization
Dec 27, 2010 09:30 AM|LINK
Localization is a .Net feature, not an asp.net or Mvc feature, so it works also with Mvc.
The way you specify what culture to use for the web site or for a single page is exactly the same in asp.net and mvc...This is the only part that is pecific to web application, the remainder is standard .Net.
The only point is that some asp.net server controls (such as a label for instance) can be configured to take their text from a resource file from visual studio designer in classical asp.net(by using the property tab), in case of mvc yo need to put manually the string exposed as property of the resource file....but this is easy as shhown in some other posts of this thread..
The issue is quite simple to implement...no need to do complex things.
1) put :
this way the culture used in the request will be taken from the user browser
2) if you want you can override the values detected by the system by letting the user choose a culture from a dropdown and storing it in a cookie or Session Variable.
3) To override the culture just use Thread.Current...that contains ponters to the current Culture and UIculture. The Culture is used to decide how to format numbers, dates and so on, while the UICultue is used to choose among the various resources files associted to the web site, and thus determines the languages of all Text and messages.
3) Just use NameofResource.StringName to retrieve a message or text you gave the name "StringName", then .Net will automatically select the resource associated to the current UICulture.
4) Use this string in your views by using <%:......%>
for more details see there: http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx
The above is a general description for asp.net...Mvc has nothing different...the only difference is that you cannot select the resource text from the property browser...but you have to insert it in the view as at point 4).
That's all
Mvc Controls Toolkit | Data Moving Plug-in Videos
vshlos
Member
2 Points
1 Post
Re: MVC & Globalization / Localization
Feb 25, 2011 06:43 AM|LINK
<div style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; width: 100%; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; margin: 8px;">Putting everything in a resource file is pretty hard when you are thinking about formatting and scripting and stuff. This generally means that you either have to start putting html tags in the resource file, or have many many resource values. Also for a page that is just a lot of content such as a "Terms and Conditions" page, that can become a very big hassle.
Our approach is to use the right tool for the right job. Bigger things which require more code content, we placed those in seperate files inside a folder for the right culture. So a page like "Terms" would go in "~/Views/en-US/About/Terms.aspx" as well as "~/Views/pt-BR/About/Terms.aspx". Each culture would have its own file. Combining resource file values with culture specific pages and partial rendering of shared pages makes it pretty simple to include globalization.
The approach is pretty simply, but I already wrote a blog about it so, just see it here:
http://blog.oimae.com/2011/02/20/cultured-view-engine-for-mvc/
We structured the code to be more cultured for example:
~/Views/en-US/Home/Index.aspx - is only in english
~/Views/pt-BR/Home/Index.aspx - is only in portuguese.
~/Views/Shared/Site.Master - Has resource file lookup values
etc.
</div>francesco ab...
All-Star
20912 Points
3279 Posts
Re: MVC & Globalization / Localization
Feb 25, 2011 08:19 AM|LINK
Obviously...Resoruce files are for label of fields, maybe titles, error messages, and similar small things. If you have a full page of text content...it is better to create a localized version of the whole page and then adding the culture name in the URL...as most of websites do.
Mvc Controls Toolkit | Data Moving Plug-in Videos