How To Import Languages Using Umbraco

In this tutorial, you will learn how to work with languages in your Umbraco website, all through code.  It is possible to work with languages manually within the Umbraco backend, however, any manual content import can burn a lot of your precious project time.  If you have multiple developers working on a project and if each person has to manually update languages you waste time.  The more people on the team, the more time wasted.  This is one of the reasons why automating it makes sense.

Writing the code to get and save languages is fairly quick and easy, so it makes more sense to write the code once and save some time.  If this sounds good to you, read on

LocalizationService

To get and add new languages you will be using the Umbraco LocalizationService. You can access the LocalizationService via the application context.  If you Google around the code around the location service, most of the code has been written in a way that breaks SOLID principles, as the application context is a static class.

When I build any C# website I like to follow a SOLID methodology, so I like to work against interfaces and inject my dependencies from a constructor. The ApplicationContext does not follow SOLID principles, as it is a static class.  The good news is that the LocalizationService is written to an interface, so I suggest that when you write a class and inject the ILocalizationService interface and build to that, like so:

    var LanguageImporter = new UmbracoLanguageImporter(ApplicationContext.Services.LocalizationService);
            
    public class UmbracoLanguageImporter : ILanguageImporter
    {
        private ILocalizationService _service;

        public UmbracoLanguageImporter(ILocalizationService service)
        {
            _service = service;
        }
} }

If you follow this pattern you can then apply a SOLID methodology.  Next, let's write some code to query all the languages set in your website

Getting All Existing Languages

To create a language picker, you will need to get all the languages defined within Umbraco. You can use this using the GetAllLanguages() method, like so:

var languages = ApplicationContext.Current.Services.LocalizationService.GetAllLanguages();

You can then render the available languages set-up within Umbraco using this code:

foreach (var language in languages)
{
       @language.CultureName
       @language.IsoCode
}   

Adding A New Languages into Umbraco through Code

As well as getting all the languages via code, you can also add new languages into Umbraco, like so:

        SaveLanguage("sv-SE", "sv-SE");
        public void SaveLanguage(string isoCode, string cultureName)
        {
            var language = new Language(isoCode) { CultureName = cultureName };
            _service.Save(language);
        }

Now you know how to read and write new languages into Umbraco, enjoy!

 

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top