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
Related
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;
I am creating a wpf application which contains several self contained plugin dlls which are accessed by my main application. Is there any way to provide localization support such that all the localized strings comes from inside the dll?
Example:
Main app accesses Plugin1 or Plugin2 using predefined interface according to the scenario.
Plugin1 and Plugin2 performs entirely different functions depending on the case and displays corresponding strings from inside the plugin dll. (These strings has to be localized to current culture)
Note:
1. Once setup, main app should not have any changes for a plugin change (obviously)
2. Plugins should not have resources outside
Thanks
I may be missing something, so correct me.
In my shop we simply include resource files in the referenced DLLs. We take those resource files and send them to a translation service. I think as long as you're storing all of your localizable strings in resource files, as opposed to hard coding them, you should be ok in terms of localization.
Another thing to consider, however, is screen real estate. Some character sets take up more space than others. This is usually a problem for us when we make the translation from U.S. English to Chinese.
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
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.
Ive been using C#s (Visual Studios) resourcing mechanism and its worked nicely. I have a need to be able to use resources but instead of using CultureInfo as the determinator use something else. This is my current setup which works fine however I have to workout which Resource Manager to use. Either Brussels or Paris. The Resource Manager then works out which resource to use within Brussels or Paris to invoke. What I want is one resource manager for both, so I dont need to decide which resource manager to use. Part of the problem is the PublicResXFileCodeGenerator Custom Tool generates the code for you (which is very nice if you use the standard approach)
This is what it currently looks like (This is working fine)
Brussels Resources
brussels.en-GB.resx
brussels.ja-JP.resx
brussels.fr-FR.resx
brussels.resx
Paris
paris.en-GB.resx
paris.ja-JP.resx
paris.fr-FR.resx
paris.resx
I dont want to use the CultureInfo but instead specify my own resx identifier. Is there a way to do this using resx file?
etc
Brussels and Paris Resources
MyResourceManager.brussels-en-GB.resx
MyResourceManager.brussels-ja-JP.resx
MyResourceManager.brussels-fr-FR.resx
MyResourceManager.paris-en-GB.resx
MyResourceManager.paris-ja-JP.resx
MyResourceManager.paris-fr-FR.resx
MyResourceManager.resx
EDIT:
An example of how I would like to use it (or something similar)
MyResourceManager.Header
instead of
brussels.Header
paris.Header
An example of a similar problem but solved through Custom Cultures can be found here.
How to load different RESX files based on some parameter
I dont want to do this however as installing cultures on different machines is not an option.
Use CultureInfo to determine the resource set to load, it will be easier since it integrates into the Thread.CurrentUICulture.
If you have cultures that are not standard out of the box supported cultures, use CultureAndRegionInfoBuilder to build any additional cultures that you need. See http://msdn.microsoft.com/en-us/library/system.globalization.cultureandregioninfobuilder.aspx. There should be ample StackOverflow examples of using this API, i.e. Create custom culture in ASP.NET.