what is a best way to create a multi language MVC3 application. For example in model Required field ErrorMessage texts in several languages?
and how in razor view cshtml pages to display labels, headings in several languages? I have googled and found some samples, but they required creating new cshtml page for each language. is there a way to just create one page and through resource files somehow
display label in different language?
If you are not using the MVC Html editor extension methods (and thus have full control) you could use a library like i18n. It's key advantage is you don't need to create keys for all your strings, you just write your strings in english (or your preferred
language) and then provide translations for it in the "po" language files it creates.
So in your cshtml and cs files you would have for example:
@_("Hello!")
Note the "_" function it's calling. That will pull the value from the translation files. Also when you build your application an additional process is run that scans your files for anything matching the _("some string") pattern and adds it to your translation
files.
Then in ~/locale/fr/messages.po (for French translations) you would have and entry like:
msgid "Hello!"
msgstr ""
You then edit it to:
msgid "Hello!"
msgstr "Bonjour!"
When the view is run the user will see the translated value if the site is running in French.
kvh
Member
131 Points
108 Posts
Multi Language MCV3 application
Feb 24, 2012 11:33 PM|LINK
what is a best way to create a multi language MVC3 application. For example in model Required field ErrorMessage texts in several languages?
and how in razor view cshtml pages to display labels, headings in several languages? I have googled and found some samples, but they required creating new cshtml page for each language. is there a way to just create one page and through resource files somehow display label in different language?
jerryjoseph
Contributor
6740 Points
1257 Posts
Re: Multi Language MCV3 application
Feb 25, 2012 12:14 AM|LINK
Add and use a resource file say ErrorMessage.resx in App_GlobalResources.
Add local resource files and access the resource like @Html.Resource("ResourceName").
It is unnecessary to create separate cshtml files for each language. It is an option if you want to do it that way.
linkedin | twitter | www.jerryjoseph.net
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: Multi Language MCV3 application
Feb 25, 2012 05:23 AM|LINK
If you are not using the MVC Html editor extension methods (and thus have full control) you could use a library like i18n. It's key advantage is you don't need to create keys for all your strings, you just write your strings in english (or your preferred language) and then provide translations for it in the "po" language files it creates.
So in your cshtml and cs files you would have for example:
@_("Hello!")Note the "_" function it's calling. That will pull the value from the translation files. Also when you build your application an additional process is run that scans your files for anything matching the _("some string") pattern and adds it to your translation files.
Then in ~/locale/fr/messages.po (for French translations) you would have and entry like:
msgid "Hello!"
msgstr ""
You then edit it to:
msgid "Hello!"
msgstr "Bonjour!"
When the view is run the user will see the translated value if the site is running in French.
https://github.com/danielcrenna/i18n - its also a nuget package.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: Multi Language MCV3 application
Feb 25, 2012 08:11 AM|LINK
have a look: http://stackoverflow.com/questions/5513993/asp-net-mvc3-multilingual-route-rewriting/5531191#5531191
tweets as @tourismgeek