I am about to implement localization for my MVC3 web application. Googling my way through large amounts of ways to do it, I was left unsure which way to implement this. I found few ways how to do it:
First option I found was to use App_GlobalResources and ViewData and culture select with Map Routing. (Link)
Second was to make a separate Resources folder in my project and structure it like Model and View folders. Then create the resource files in those folders. To use those resource strings would be like using the Viewbag. Then edit the Global.asax to handle culture changing and create a partial view to allow changing cultures. The instructions to do this are very thorough so this might be the best bet.
(Link)
Third was to use DisplayAttribute and resx-files. This one was a bit hazy, I could not find the sort of information so that I could grasp how this actually is implemented and its restrictions. (Link)
Fourth was to create a custom class to handle resources. This seemed pretty fancy and easy to implement and use but no real information was found if it actually worked. (Link)
Then I found a post that warned about using the App_GlobalResouces and App_LocalResources. (Link)
After going through possible ways of doing localization. I could not find a way which was universally approved or accepted. This left me pondering, which of these, or some I didn't find, would be the best way to implement localization in ASP.NET MVC3?
Then I found a post that warned about
using the App_GlobalResouces and
App_LocalResources. (Link)
I found that post extremely useful. The method explained there is very clean. Here is a snippet of my newly created index view using that method:
#using Resources.Index
#{
ViewBag.Title = "Index";
}
<h1>#Index.Title</h1>
I don't think there is a definitive do-it-this-way-or-else method, so going for the cleanest method seems to me like a good deal.
I've come up with an easier way to handle localization where you don't have to use resource files or attributes. It involves custom metadata providers.
MVC3 uses metadata providers to get all text strings. My approach allows you to either use string tables, a database, flat files or any other source to provide the translations. All you need to do is to inherit two interfaces.
Read about it here: http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/
Edit
Everything is now documented at github and there are nuget packages: https://github.com/jgauffin/griffin.mvccontrib
Related
I am implementing various analytic tracking services in an MVC3 site (Google, Coremetrics) and researching if a custom HTML helper or partial view would work better given the following details:
-Code runs multiple sites and business logic is needed to change analytic service account Ids.
-The class must accept an object with order details to render the appropriate tags.
-The code must know which view is being rendered.
The solution I am working on includes am HTML helper base class that accepts the needed objects and is inherited by each provider's individual HTML helper. These helpers will live on the common layout. Is there a better way of implementing analytics on MVC and are partial views better suited since business logic is needed?
The GA analytics tracking code is pretty simple, we also use ASP.NET MVC3 but have just dropped the necessary code directly into our 'Order Complete' view.
There's no reason why you couldn't use an HTML helper instead though. I suggest not overly complicating your object model. Just use a single helper that has parameters matching the parameters used in the JavaScript.
You should be able to find examples of eCommerce tracking code, add parameters as appropriate, done. You might want to create another overload that pulls GA ID and domain name from the web.config as in the example as well, but still accepts the order details as parameters.
I'm trying to create a asp.net mvc3 project for a academic project, and one of the requirements is it has to be able to change between different languages. Currently what i have is the following:
I have a external project that works as a repository for languages and for each view i have an interface for each view that defines all the "placeholders" do define all the changeable text.
At the beginning of any action i obtain the language that is in the uri (something like /{lng}/{command}/{action}) and pass it to the view using the ViewBag, once inside the view i user the repository to obtain the current implementation of the interface for that view in the chosen language.
I can't find any good topic on this mater. I'm just curios if there is a better way to do this and more efficient. And how is it normally done in a professional level.
I'm not very experienced with asp.net just started learning it about a month ago.
Also if it's important i am using the razor engine for the views, and we can't use any JavaScript in this phase of the project.
You may go through the following guide.
I'm working with a project called Griffin.MvcContrib which has some localization features.
First of all, I use the query string and a cookie to switch language. (Just create a link with a flag in your layout English)
and tag your controller with my attribute:
[LocalizedAttribute]
public class YourController : Controller
{
}
The next thing is to get localization of views, models and validation messages.
The localization of models and validations are described here. As for views, you only need to use #T() to get translated texts:
#Model.Title
<div>#T("This text will get translated")</div>
(you need to change pageBaseType in Views\Web.config to Griffin.MvcContrib.GriffinWebViewPage)
I'm almost done with an adminstration area that any non-technical user can use to manage all translations. Check the Griffin.MvcContrib.Admin project here: https://github.com/jgauffin/griffin.mvccontrib/tree/localization/source/Griffin.MvcContrib.Admin
I would like to create for my Yii application, a site where I can describe each model, view and controller used. However, doing this for each and everyone of my classes would be an extremely long task which I believe might already be solved out there.
Is there a way to automatically create an site with each model/view/controller and its methods, so then I can add a description to it?
Have you tried phpDocumentor http://www.phpdoc.org/? It's crawls your PHP files' comments and generates a simple website for them in the spirit of JavaDocs.
Or, better still, there is a custom extension you can use to build docs like those on the Yii site call Yii Docs Generator http://www.yiiframework.com/extension/yiidocsgenerator. See the details at http://www.yiiframework.com/wiki/186/how-to-generate-yii-like-documentation
I would take a look at T4MVC - it uses a T4 template to generate code for each controller / method. Maybe you could utilize it.
Have you looked into the Gii module of yii. Once you create the tables it can produce models and then CRUD (create, update, delete) pages. Excluding the database there would be no programming required. And then you have the full source in a readable and documented form to manipulate and change to suite your needs.
If you wan't something a bit more advanced or custom you could extend Gii and produce some of your own templates
Larry Ullman has a really nice series on Learning the Yii Framework. In part 3 of the series, he walks you through configuring Yii and enabling Gii, a web-based automated code generation tool for Yii. In part 5, he shows you how to use it to generate the code for your models. The whole series is a really nice introduction to Yii, I recommend it.
I am delving into custom validation attributes and am curious to know how others structure the projects. Where do you typically store custom attributes?
My first thought was to simply create a new folder and be done with it.
Any suggestions?
My first thought was to simply create a new folder and be done with
it.
It would depend on the nature of those attributes and what thety are supposed to do. For example if they are validation attributes you could put them into a Validators folder. If they are action filters you could put them in the ActionFilters folder, etc... so your initial thought is correct. Personally I group those attributes based on their function and place them in a separate folder which indicates this function.
I use 2 different approaches.
Set up a common Class Library to store common validation that will be used on many MVC applications. Then reference this library from your MVC application. You can use http://dataannotationsextensions.org/ to view the source code on how to setup this project.
Place them in folders as suggested by Darin. This folder would be used to store custom validation. If you app was used to keep golf scores a custom validation only to the application could have something to do with a handicap calculation or something specific.
Thanks,
I'm trying to use Phil Sturgeon's CodeIgniter Template library, but I can't get it to change my pages. I read the documentation, followed the steps there,
Download
Copy to application folder
Create a views/layouts/default view
(Auto)load the library
however, it is not clear as to how the template is actually implemented. I thought that loading the view would apply the template everywhere, but it doesn't seem to work that way. I've also read about it in the CodeIgniter Wiki, but it looked too complicated to be the right answer.
How are you supposed to use Phil Sturgeon's Template with your controllers? (or views?) Am I missing something here?
It does not overload the load->view() methods, that would be bad. You need to update your controllers to use the template's syntax and methods in every instance you want to use it:
http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/
You will use $this->template->build() instead of $this->load->view() in most cases, after constructing your template by defining regions, setting titles, etc.
There should have been a user guide included in your download with examples.