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
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've an existing MVC3 project that implements a certain functionality, this project has it's own views, and a separate Database.
now I'm required to use the same functionality inside one of my orchard project,so I thought that I can host this solution in somewhere and view it inside an iframe or something.
Am I thinking right?,
is this the correct step to take in order to achieve this requirement inside Orchard?
to make it more clear, all I need to do is to view this solution and interact with it's controls and views from a hosting page inside orchard, and the subsequent requests should be handled by my solution in order to hit it's own data store and get back with the requested data in order to be displayed to the user.
any help would be appreciated.
Update:
thanks for Bertrand Le Roy for his answer, I can now view my solution inside my
orchard website.
I came in to one more HUGE problem, which is that my application can no longer connect to my external database.
I've a DB that is hosted in some where else, and I'm using EntityFramework to deal with it.
the problem is that if I put the connection string inside my module web.config, or main orchard web.config, I run into several types of errors like:
"System.Reflection.TargetException: Object does not match target type."
or
"System.Data.MetadataException: Unable to load the specified metadata resource."
My question is: How could I pass my connectionstring correctly to my solution, assuming that I'm using Entity framework as my ORM.
Many thanks.
You will need to put it into a module.
You will have to move route definitions to a Routes.cs file (look at any existing such file for examples).
You will also need, in order to access your data store, to opt out of the ambient Orchard transaction around the data access code (using (var scope = new TransactionScope(TransactionScopeOption.Suppress))).
If you are using dependency injection, you may have some work to move that to the Autofac-based way of doing things in Orchard.
If you want your work to appear seamlessly in the Orchard admin, you may want to decorate your admin controllers with the Admin attribute. If you want your front-end to use the current theme, you'll have to add Themed attributes and maybe refactor your views so that they only emit HTML for the content zone instead of for the whole page.
Add a manifest (module.txt) to your module folder and you should be good to go.
On My project i'm using ASP.NET MVC3, and my backgound comes from traditional asp.net, my question is related "comparing with asp.net webforms" with the inheritance that is possible on codebehind.
What I intend (in mvc3) is to have a couple of extra properties available on all views of the application.
Is it possible to add my extra properties to my views or I need to use the "Helper way" as a work around?
Notes that my question is not at the model level, but for the view (UI).
Sorry forgot to mention that my viewengine is Razor
If your extra properties are meant to be UI specific, then you can define a master page and use that on all of your view pages. If they are meant to be value properties, then you can create a base model and inherit that in your model classes, which then will be available to all of your views using those models. If none of these are feasible for your purpose, then you might try extending System.Web.Mvc.ViewPage, but I am not sure if it would be a best practice.
Yes, by default MVC Views inherit System.Mvc.ViewPage. If you are using a strongly typed view, then it's System.Mvc.ViewPage<model> where your #model declaration is pass to the generic ViewPage<> class.
While it's possible to inherit from a derived ViewPage, it's not typically done. Depending on what you want to do, there is likely a better solution.
Perhaps if you clarified what you are doing, it might help us help you more.
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
Asp.net MVC 3 preview 1 was released at the end of last month. Are there any new features you are excited about or any features you would like to see before it is fully released?
Full support for Controllers with Generic Parameters
public GenericController<SomeType> : Controller
Generic controllers are quite possibly the greatest MVC timesaver if your doing a lot or business CRUD. There are so many similarities between the Add methods of almost every MVC project that it makes sense to abstract these operations out in a Controller that fits all scenarios.
Right now its a little hacky to create a generic controller. The MVC engine always gets the name wrong (GenericCo vs. Generic) and without full support plugin and libraries that interact with controllers just fall over when they encounter a generic one.
Make Dropdowns easier to work with
As a professional MVC tag watcher I've noticed that working with dropdowns is one of the most repeated questions on SO. The amount of Dropdown questions is a strong indication that something should be done to make it easier or less ... complex?
make checkbox list easy to work with
add T4MVC to the official release
add official helpers for OData
support one javascript library either MS Ajax or jQuery(preferably)
I wish they can add something to help developer to migrate their previous ASP.NET WebForms application.