Where do I put my local resources in MVC project? - c#

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.

Related

ASP.NET MVC 5 Internationalization

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;

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

What is the best way to use a script with applications variable in asp net mvc?

I am developing an Asp Net Mvc4 application to a real estate business. In the future this system will be sold to other companies.
The question is, how to create a file to store variables such as company name and other values so I don´t have to type those values in every view I need them?
Is it a good approach to store those values in the database or in a config file?
Generally you'd put things like that (mainly strings) inside a resource file .resx. Doing so you can change the string value in just one place (the .resx file) and have it reflected everywhere you use the string as for example the Company Name.
A .resx also makes it easy if in the future you decide to provide localized versions of your app. It's just a matter of adding a new .resx file with translated values for a given culture/language.

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

category localization resx or database?

I would like to request opinions in order to approach internationalization of a website in C#.
I´m working with resx files to define strings of text. I would like to know if its possible to use resx to define categories - eg: commerce website - or its preferred to use a table in the database with a foreign key to table category?.
brgds.
As a general rule, use resx files to localize strings that can only be added by developers and are only updated when new versions of the application are deployed. Resx files are specially tailored for this purpose and work great.
If you want to give the end user the ability to add their own values you will have to do the localization in the database (or use some other mechanism).
I thing that for a website it would be nicer to use a table in your database. Otherwise if you should write a desktop application. I would prefer a resx file.
So that you can load that file on the start of your program. So you don't need to go to a database. For a website you already need a connection to get some other data to fill the page.

Categories