How to force a specific uiculture using web.config in MVC - asp.net-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.

Related

How to apply i18n on whole website by a single click in Struts 2

Is it possible to apply Struts 2 Internationalization (i18n) for changing language from English to Hindi on whole web pages of website by single click?
If it is possible then how can I resolve this problem?
As mentioned by #RomanC , it is possible to do that, you need to have the i18n interceptor in your package.
After that you need your jsp page submit a form with request_locale in it, or call an action with request_locale in its parameters . For example, if you want to do it with select you can use below:
<s:form id="langselect" action="locale-manager" namespace="/common">
<s:select name="request_locale" headerKey="-1"
headerValue="Language"
list="#{'en_US':'English', 'fa_IR':'فارسی','ar_SA':'العربية','zh_CN':'中国的'}"
onchange="forms['langselect'].submit()" />
</s:form>
You form action needs nothing to do about changing the locale at all, as the interceptor will do all the job for you.
Changing the locale will change all your 20 pages at once, of course as soon as you reload your pages. So if you want to do it with tab you need to reload your new pages (for example with ajax) or reload your total site to get localized jsp from struts.
The framework is internationalized.
You need to add the corresponding to each locale resource bundles for
the localized messages that you display via the struts tags. Use
text tag or getText() to retrieve a message in the UI.
The browser language is passed with the HTTP request and framework
create a locale corresponding to the browser settings. Switching
locale performed via passing a special parameter request_locale to
i18n interceptor that should be on your stack.
You can also configure this interceptor to accept user-defined
parameters.
Normally switching to a locale persist for the session of the user.
So, you don't have to pass a parameter each time on every request, but
this case is also supported if needed. See how could you achieve all
of the above using localization.

Struts2 : internationalize s:date?

<s:date name="mydate" format="dd/MM/yyyy HH:mm" /> works perfectly for fomatting my date to the french standard.
But It's hard-written in my jsp. So I have to build if clause to switch according to the locale.
Is there a way to put this format to a general property file?
I tried:
format.date = {0,date,dd/MM/yyyy}
format.time = {0,time,HH:mm}
put in my global .properties but it's not taken into account when, I try just a <s:date name="mydate"> or a <s:property value="mydate"/>.
Create localized date format inside your properties files, e.g. with struts.date.format key:
struts.date.format = dd.MM.yyyy
And use getText method to get this date format in <s:date> tag format attribute:
<s:date name="date" format="%{getText('struts.datetime.format')}"/>
Absolutely. Internationalization (i18n) is handled out-of-the-box in almost every framework out there, then it's better to spend time on understand how to make it works than to write some unneded, buggy custom solution.
With Struts2, you must ensure to have:
I18nInterceptor in your Interceptor Stack;
I18nInterceptor defined in struts.xml: <constant name="struts.custom.i18n.resources" value="global" />;
the localized, correctly named global.properties file for each locale you want to handle;
the request_locale` parameter (if you have not changed the language in your browser) when calling your Actions, giving the I18nInterceptor the ability to hijack the request to the correct resource;
an appropriate character encoding on your pages: <%# page contentType=”text/html;charset=UTF-8″ %>.
Take a look at the guides available on the web too:
http://www.mkyong.com/struts2/struts-2-i18n-or-localization-example/
http://www.2bloggers.com/2011/12/localization-i18n-in-struts2.html
http://www.roseindia.net/struts/struts/struts2.2.1/tags/i18ntag.html
http://www.roseindia.net/struts/strutsinternationalization.shtml
http://www.roseindia.net/tutorials/I18N/internationalison-code.shtml

Why do I need an underscore for partial views in asp.net mvc

Just to distinguish between a view used inside a dialog or used in a foreach loop (customer details) ?
You don't need an underscore. It's just a convention, and MVC is very keen on using conventions.
Mike Brind has put this nicely in the question Why does Razor _layout.cshtml have a leading underscore in file name?:
Since layout pages in Web Pages are not intended to be served directly, they are prefixed with the underscore. And the Web Pages framework has been configured not to allow files with leading underscores in their names from being requested directly.
Besides that, I find it very helpful to use this convention to differentiate between full views and partial ones.
#Marius Schulz gives a nice reference, but then misses the point. Yes, the underscore helps to differentiate between full views and partial ones, but more importantly, it prevents partial views from being loaded directly by their URL, which could provide some potentially ugly results! (Like no css, for starters.)
EDIT: Mystere Man is right...what was I thinking? URLs in MVC point to controller/action, not to view.
Also, it is possible to mess things up and display a partial in a seperate window, so the naming convention does not prevent that. #Marius Schulz and I had the same misinterpretation of his quote.
The leading underscore is a useful convention to differentiate full and partial views, and I will continue to use it, but is is just a convention, not a functional difference.

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.

Localization in 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.

Resources