ASP.NET MVC 5 Internationalization - c#

I'm starting a new solution in ASP.NET MVC5 and I'll have to implement internationalization for the whole views.
The best article I found so far that explains how to well implement ASP.NET MVC 5 Internationalization was this one. I also read this one.
Anyway I still have some questions that I'd like the ask here in order to make sure about the best practices of internationalization which ones I still haven't found clearly answers on web.
Where should I put my local (resources for a specific view) .resx files?
App_LocalResources folder? ItemView folder with all the other files? Where exactly and why?
How can I access my local resources values? Should I put them over the same App_GlobalResources namespace's .resx files (namespace Resources)?
Thank you in advance.

According to MSDN, local resources are a concept relative to .aspx views.
In MVC, it is recommended to avoid App_GlobalResources and App_LocalResources. A great answer/explanation regarding this can be found here.
Alternatives to App_*Resources include:
Properties > Resources
Resources Folder
Resources Project

You can simply put your resource files in a custom folder.
I created a "resources" folder and it works like a charm.
To access values, here an example for an action link:
#Html.ActionLink(Resources.Labels.Archivio, "Archivio", "Home")
Note you don't specify the Language, because MVC takes the locale loaded into the current thread
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

Related

MVC globalization with different DLLs

I have designed my MVC application in the following way:
-Portal.UI ( hold all the Views and controllers details)
- App_GlobalResources
- Global.en-GB.resx
- Global.ar-SA.resx
-Portal.Models ( hold all classes and Entity framework DbContext code first)
- Resources
- Global.en-GB.resx
- Global.ar-SA.resx
-Portal.Services ( used for accessing different internal web services)
client requested to make the application multilingual to support different languages
so I have added the resources files to the Portal.UI and converted the views titles and buttons to use these resources files
Also, I added the resources files to the Portal.Models project and used the [Display(Name="Name",typeof(Resources.Global)] for each used property
now how can I tell Project.Models which resource file to use?
I mean if i changed the culture to ar-SA for example in the Portal.UI , it will reflected also to the Portal.Models automatically or I have to write code to achieve this?
any advice would be highly appreciated
Resource files selected according to UICulture (and not Culture). Assuming that Portal.Models project is a part of your deployment and is added as reference to your Portal.UI the only thing that you should do is to set a correct UICulture in your Portal.UI.
Useful link that shows the difference between Culture and UICulture

language subsite in asp.net

So we have a site that was made quite some time ago. It uses asp 2.0. Currently the site is not multilingual. Since this site was not created with multi langue in mind it contains a lot of images with text on them.
Some time ago a college went ahead to create 3 versions of the site by basically copying the whole site and translating each one. I'm not sure if that's the best way to do it.
I would now like to go ahead and merge those sites. To create something like www.site.com/en/ for English for example.
What would be the best way to achieve this? Can I just take the main site and create for example 2 langue buttons and subfolders containing the translated versions? or is this more complex?
I should note that the site is using a masterpage.
Thanks in advance!
Globalization of an application is huge topic. You could start reading this guide:
https://msdn.microsoft.com/en-us/goglobal/bb688110.aspx
Basically, you have to:
refactor your code to put strings in resource files
refactor your images to use text from resource files and backgrounds (css will help)
Then you have to translate the resource files and put them in the application.
Then you can use the Routes to set the language in the URL using a route like this:
routes.MapPageRoute("PageId", "{language}/MyPage", "~/MyPage.aspx");

How to determine if a .resx file is used?

I've inherited a number of ASP.Net C# projects that mix MVC and forms. Several projects have .resx files in App_GlobalResources. My guess is that the files were related to .aspx forms that were converted to MVC.
I can think of two strategies for determining whether the files are in use:
1) Remove and see what happens. Do missing .resx files cause run-time or compile-time errors?
2) Work backwards from the files and see what uses them. Not exactly sure what to look for though. String searches have come up empty.
Any help/suggestions appreciated.
Thanks.
You should be able to search for the name of the resource files. E.g. if the Resourcefile is called MyGlobalResource.resx then you can search for MyGlobalresource. If it does not come up anywhere, I think you can be safe to remove it.

How to create a multi-lingual site

I am about to start on a project which must support a number of European languages. All static content on this site must be translatable between these languages. I have heard of satellite assemblies and have heard that they are used for multi-language sites in .NET but this was a long time ago. Is this still current practice in .NET? I'm using ASP.NET MVC.
If you're using ASP.NET MVC, one option would be to use a different resource for each view. I wrote an article about it, maybe it can be of help:
ASP.NET MVC Localization: Generate resource files and localized views using custom templates
Without the satellite part, you can add a App_GlobalResources folder to the project and add *.resx files for each language. You can have one resource file for the whole project, or one per ASP.NET MVC Area or however you see fit, but you don't need to have more then 1.
App_GlobalResources
MyResources.resx (Neutral / Default language translated texts)
MyResources.en-GB.resx
MyResources.de-DE.resx
In MyResources.resx
Name Value
TextID Some text which will be localized to the end user.
In Global.asax.cs (Application_PreRequestHandlerExecute)
// Set it according to cookies/Session etc.
System.Threading.Thread.CurrentThread.CurrentUICulture = "de-DE";
System.Threading.Thread.CurrentThread.CurrentCulture = "de-DE";
Then in the Views and PartialViews (e.g. MyView.cshtml).
<span>#Resources.MyResources.TextID</span>
Optional:
The Resources can be added as a link, and use custom namespaces.
BuildAction: Embedded Resource
CopyToOutputDirectory: Copy always
CustomTool: PublicResXFileCodeGenerator
CustomToolNamespace: MyNamespaceHere
mToolNamespace: MyNamespaceHere
Then they would be accessible via.
<span>#MyNamespaceHere.MyResources.TextID</span>
This is a general way to use (normal / linked) resources in ASP.NET MVC. If you use "Add as link" you can have one physical copy in a separate project.
Satellite:
Some info on satellite assemblies:
MSDN: Creating Satellite Assemblies
A MSDN Blog: Introduction to Satellite Assemblies

Where do I put my local resources in MVC project?

I am currently working on an mvc4 application. It needs to be multi lingual so therefore each of my views requires a resource file associated to it.
In the web forms world, I would be creating App_LocalResouces and have the resource files within this for my pages/user controls.
Being new to MVC I am not sure how this works - is it similar. For example for my razor views, where the resource file for the particular view reside. Same with my models and controllers, where does the resource file for such reside?
I've seen it done a lot of ways. It's really based on preference. My preference is to create a Resource folder and store the translated strings in one file with the name of the culture. I then set the language in the web.config file and leave it up to the page to pull the correct strings.

Categories