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
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 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
I've implemented multi language support following this blog post http://geekswithblogs.net/shaunxu/archive/2010/05/06/localization-in-asp.net-mvc-ndash-3-days-investigation-1-day.aspx
Only some of the pages are in different languages, so I'm trying to create a html helper to show a dropdown to select the language they want. I want to make this html helper bit intelligent and implement it so that it will show the different languages if it's available when I provide it with a resource type.
Say, I have sample.resx, sample.ko.resx, sample.zh.resx and say their namespace is Resources.Contacts . Is it possible to find out different language resource files of a given type, namespace or whatever at runtime?
possible duplicate because it has been answered here already:
Programmatic way to get all the available languages (in satellite assemblies)
There are certain workarounds to figure out if the languages are available. You'll either have to check if the resource supports the culture, or, if check the folders within your bin directory (for each language, there might be a folder with the name of the culture).
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.
I have many different projects within a solution. I am in the process of localizing our website, but i have a question about how to access globalization resources from a project outside of our main web project. for example, i'm using a project to process twitter oauth tokens, and each language will have a different twitter account with it's own set of consumer_key and consumer_secret, so i would like to be able to put each language specific twitter application information inside of a localized file inside of Global_AppResources folder within my website project. but, my twitter oauth project, which is a separate project outside of website project needs a way to access this information. any ideas?
inside my twitter oauth project, what libraries do i need to reference so i can do something like this:
GetGlobalResourceObject("WebResources", "TwitterConsumerKey");
Instead of using a localized resource, i could put all the information inside of my web.config file, but each language will have its own set of information, which sort of defeats the purpose of bloating the web.config file. don't you think?
Satellite assemblies. Give this and this a read.