Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Database localization

I'm developing an multilingual enterprise web site and I would like to store the localization in database.

I have read the following article which is very good but I personally think that is a an overhead and I can achieve the same much easy:

Extending the ASP.NET 2.0 Resource-Provider Model

I have already setup some ground but I'm not sure if my approach is fine. Basically I have created a service that is registers with DI.

public interface ILocalizedStringProvider
{
    string GetLocalizedString(string key);

    string GetLocalizedString(string key, string deafultValue);
}

Also i have created a Html helper like this

public static MvcHtmlString LocalizedString(this HtmlHelper helper, string key, string defaultValue)
    {            
        if (string.IsNullOrEmpty(defaultValue)) return new MvcHtmlString("");
        if (string.IsNullOrEmpty(key)) return new MvcHtmlString(defaultValue);

        ILocalizedStringProvider localizedStringProvider = DependencyResolver.Current.GetService<ILocalizedStringProvider>();

        if (localizedStringProvider == null)
        {
            return MvcHtmlString.Create(defaultValue);
        }

        string val = localizedStringProvider.GetLocalizedString(key, defaultValue);

        if (string.IsNullOrEmpty(val))
        {
            return MvcHtmlString.Create(defaultValue);
        }

        return MvcHtmlString.Create(val);
    }

Then the helper is simply invoked from the view.

First I want to know if this approach is good and if is not an anti-pattern.

Second my concern is this line:

ILocalizedStringProvider localizedStringProvider = DependencyResolver.Current.GetService<ILocalizedStringProvider>();

Is it maybe better to resolve the service ILocalizedStringProvider in the controller with constructor injection and let the controller populate the ViewBag with the localization's?

Thanks!

like image 968
The Tech Geek Avatar asked Nov 10 '12 19:11

The Tech Geek


People also ask

What is MVC localization?

Localization is the process of adapting software to meet the requirements of local markets and different languages. You can change the messages that are displayed in the Telerik UI for ASP.NET MVC helpers by including an additional script file in the document.

How do you localize a database?

THERE ARE FOUR METHODS THAT ARE USED TO LOCALIZE DATABASES: Row localization – copies the original row for each language. Field localization – updates the values of the localized fields. Table localization – adds new language tables for each table. Database cloning – creates a copy of the database for each language.

How ASP Net applications are localized?

App localization involves the following: Make the app's content localizable. Provide localized resources for the languages and cultures you support. Implement a strategy to select the language/culture for each request.

How will you localize URL in MVC application?

To offer a website in multiple languages using ASP.Net we simply need to add some resource. resx files to our project and voilà. Based on the language of the browser, IIS will match the localization resource.


1 Answers

You can use my Griffin.MvcContrib project. It contains a ready to use MS SqlServer implementation for storing localization in the database.

Introduction: http://www.codeproject.com/Articles/352583/Localization-in-ASP-NET-MVC-with-Griffin-MvcContri

Administration

There is also an adminstration area which you can use to manage the localizations:

enter image description here

SQL Server setup

https://github.com/jgauffin/griffin.mvccontrib/wiki/SqlServer

Source code

The project is available at github: https://github.com/jgauffin/griffin.mvccontrib

like image 51
jgauffin Avatar answered Oct 10 '22 20:10

jgauffin