Localization in ASP.NET MVC - asp.net-mvc

Visual Studio 2008
I want to bring some localisation into my ASP.NET MVC site.
Someone suggested creating a resource file "Strings.resx" as a publically strongly typed resource, which works nicely and allows me to write
<title><%= Strings.MyView_Title %></title>
I then proceeded to add a file "Strings.da.resx". This file is created right next to the first one, and defaults to "Access Modifier : No Compilation", whereas the first (the one without language modifier) defaulted to "Interal".
I can see in the bin directory that a directory has been created ("da") with a resource.dll, however, I cannot see any of the translated texts on my site.
I have checked with the browser that the only preferred langauge is Danish (da-DK), but I only see the english texts.
Questions:
1) Do I need to enable something in web.config ?
2) Am i creating the right files, with the correct types (i.e. should #2 be "No compilation") ?

In your views, do you have a page directive?
If so, do you have UICulture="Auto" and Culture="Auto"?
For example...
<%# Page Language="C#" Inherits="..."
culture="auto" uiculture="auto" %>
This will ensure that the Accept-Language header, passed by the browser in the request, gets used to set the Thread cultures. It's the UICulture that influences which resource file to pick.
For more on ASP.NET i18n this book is very good...
http://www.amazon.co.uk/NET-Internationalization-Developers-Guide-Building/dp/0321341384/ref=sr_1_1?ie=UTF8&s=books&qid=1241010151&sr=8-1
It doesn't cover MVC, but covers ASP.NET and, as such, many things continue to be relevant.

First you have to create action filter that will switch culture of request thread.
OR
Set your globalization element to UICulture="Auto" and Culture="Auto"
Check this screencast, it is in Russian but code samples are understandable.

Related

Changing page direction (Right-to-Left/ Left-To-Left) based on Localization settings in MVC 5

How can I change the page direction based on the local setting of the user? from left to right to right-to-left?
Should this be done using JS only, or there is a better way? please note that I need to Change to value, not check the current culture settings.
Using ASP.NET MVC5
Thanks
To set the text direction of the whole page in HTML 5, you can set the dir attribute on the html or body element. (In HTML 5 the dir attribute can be used on any element).
For instance if you're setting the CurrentThread CurrentCulture in the request, you can use the following in your view:
<html
dir="#(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft? "rtl" : "ltr")">
Based on http://afana.me/post/aspnet-mvc-internationalization.aspx
Note that if you're using a CSS library such as bootstrap, you'll also need to get an rtl-modified version of the CSS, because the page direction won't affect css rules.

How to define an MVC site in Sitecore (multisite)

I'm currently using 7.1 update 2 running multiple sites using web forms. I'm starting a new site and want to use MVC but can't get it to work; however, I can get the default "website" to use MVC but not a custom site.
For example, let's say I have two sites, site1 and site2, site1 is web forms and works fine, how do I define "site2" which is MVC? Not sure what the value should be in the "physicalFolder" property. Or the site should be defined somewhere else.
Thanks
<site name="site1"
targetHostName="site1.com"
hostName="site1.com"
virtualFolder="/"
physicalFolder="/site1"
rootPath="/sitecore/content/site1"
startItem="/Home"
language="en"
database="master"
domain="extranet"
allowDebug="true"
enablePreview="true"
enableWebEdit="true"
enableDebugger="true"
enableWorkflow="true"
patch:before="site[#name='website']" />
The physicalFolder parameter will allow you to specify the physical path that Sitecore will look for any physical files it needs for the specified site. It is one of the options that can be used to organize the physical files for multisite instances.
You will see the following definition in the web.config comments:
physicalFolder: The physical location of files for the site.
If the site is based on physical files, this is the path to the folder holding the files.
For non-physical sites, this is the place where Sitecore looks for a default.aspx file (to start the pipelines).
You can find more information about the physicalFolder parameter here -> http://sdn.sitecore.net/Articles/Administration/Configuring%20Multiple%20Sites/Adding%20New%20Site/site%20Attribute%20Properties/physicalFolder.aspx
As for configuring your MVC site, there really isn't anything special you need to do in terms of your Site definition. You should just be able to replicate your Site1 definition and make the appropriate updates. Sitecore will utilize the correct rendering method based on the Layout and Sublayouts defined on your Pages.
<site name="site2"
targetHostName="site2.com"
hostName="site2.com"
virtualFolder="/"
physicalFolder="/site2"
rootPath="/sitecore/content/site2"
startItem="/Home"
language="en"
database="master"
domain="extranet"
allowDebug="true"
enablePreview="true"
enableWebEdit="true"
enableDebugger="true"
enableWorkflow="true"
patch:before="site[#name='website']" />

Same link in menu and in footer are in different languages

My ASP.NET MVC 4 website URLs looks like this "/language/controller/action".
HTML code of link in menu and in footer are the same.
<li>Website development</li>
When site is opened in "en-GB" culture link in menu pinting to
/en-GB/Service/WebsiteDevelopment
But link in footer is
/hy-AM/Service/WebsiteDevelopment
How it is possible? How can I fix this?
You don't specify "language" in Url.Action, so it probably takes the default language.
ASP.NET links are absolute ones, so without specifying the language, it's
/default-language/controller/action
If you want the language to be taken from the current URL, you need to write your own Url.Action function, or extend it.
Last menu was changing entire language to Armenian. Aa a route value it takes place after that function.

How to force a specific uiculture using web.config in MVC

I have added Resource.fr-FR.resx to my project and have done the globalization setting in
web.config as follows.
<system.web>
<globalization culture="fr-FR" uiCulture="fr"/>
</system.web>
that is all you need according to http://msdn.microsoft.com/en-us/library/bz9tc508%28v=vs.80%29.aspx
but when I run my app it is still in english
I have checked "Thread.CurrentThread.CurrentUICulture" in Application_Start() and it says FR.
what am I missing?
the same thing has been posted in
MVC 3 Setting uiCulture does not work
but no answers.
Culture is not the same thing as Language. A "culture" is a set of formatting rules for dates/times, currency, calendaring, and a few other things (like text Title Casing rules).
Setting a culture will not automatically localize your application (where localization is when all of the human-readable strings are translated into another language, such as French). Localization and UI translation is a long, painful and expensive process. ASP.NET does not do it for you.
If you want to localize your application, you need to ensure that all human-readable strings are stored in a resource file (.resx in .NET) and you have created "satellite assemblies" that contain translated strings for other languages. You then need to ensure that ever string displayed to the user in your application uses the Resources APIs (or IDE-generated helper classes). This is painful because it means going from this in your *.aspx files...
<p>Hello, welcome. This is in English.</p>
...to this:
<p><%= Resources.WelcomeMessage %></p>
...or this (if you have too many strings to be managed by the Resources helper class):
<p><%= ResourceManager.GetString("HomePage_WelcomeMessage") %></p>
...doing this, of course, breaks any visual-designers for your website, for example.

Module Localization in DNN

I don't know much about the localization process in DNN. The question is that how can you localize a new module?
Is it possible to include localization files with every module separately? What solutions can you come up with?
Localization of a module is pretty easy thanks to DotNetNuke.
Wherever your .ascx (View) file is, the App_LocalResources folder should always accompany it, on the same level. There should also be a corresponding .ascx.resx file in that folder.
view.ascx
App_LocalResources
- view.ascx.resx
Once you have that structure in your module. DNN will pick the file up immediately.
To use that resource strings in the resx. Simple tack on the ResourceKey property to the end of your asp controls. e.g.
<asp:Label ID="lblExample" runat="server" ResourceKey="lblExample" />
You should have a lblExample.Text in your resx file which matches up with that label. Note that it adds .Text to it automatically.
If it's not showing up, there are a few things to check
LocalResourceFile property in code. What location is it pointing to?
set ShowMissingKeys=true in web.config and you'll see what resource strings you're missing.
Please find this document. I am not sure if it covers your questions and how localizing DotNetNuke modules is different from other Asp.Net applications but please try it out.
If I may suggest something, I would add more tags in the future (like C# for example), it will be visible to broader audience which may result in better answers.
Simply create a folder called "App_LocalResources" on the same level as your .ascx view files in your project. For each file that you want localized, simply add a .resx file with the same name as the view (including the .ascx extension).
Resx Name Example:
"View.ascx.resx"
Using localistion is really easy after that. Simply set the Resource Key property of whichever controls you want to pull from your resx file to a meaningful name
Example:
<dnn:Label id="lblName" ResourceKey="lblName" runat="server" />
Resx File:
"lblName.Text" will assign to the Text property of the label
"lblName.Help" will assign to the DNN Tooltip property if you are using dnn:Labels like above
If you want to start using DNN Labels simply put this tag at the top of your page.
<%# Register TagPrefix="dnn" Assembly="DotNetNuke.Web" Namespace="DotNetNuke.Web.UI.WebControls" %>
<%# Register TagPrefix="dnn" TagName="Label" Src="~/controls/LabelControl.ascx" %>
Another handy method available is:
LocalizeString("key")
It will pull from your resource file and it quite handy when working with things like email templates.

Resources