This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Localize your AngularJS Application with angular-localization

5:28 Angular 1.x lesson by

It is best to start your application's localization efforts early in development, even if you only support one language initially. Libraries like angular-localization help make the process of supporting additional languages much easier.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

It is best to start your application's localization efforts early in development, even if you only support one language initially. Libraries like angular-localization help make the process of supporting additional languages much easier.

You can jumpstart your application's foreign language support using angular-localization or any one of the other great localization libraries available. The latest version of this is 1.1.3. That's what we're going to use for the purpose of this demo.

A little bit about the application. We have three animals here, each with an English name. I want to support three different translations here, English, Spanish, and Swedish. A note, particularly to the Swedes. I did most of the translations on Google Translate. I apologize if they're a bit off for the purpose of this demo.

Right now, the translations are not changing at all. I'm going to walk through how we can take an existing app and something that performs like a pretty normal Angular app and make it into a localized app.

A little bit on configuration here. Up at the app.js file, we define our app, and then we also pull in dependencies. ngSanitize and ngLocalize are both required to use the Localize library, and then the .Config and .InstalledLanguages just make it a little easier to use some of the existing structure, like this localeConf, as well as this localeSupported.

localeSupported is simple enough. It's just the three locales we are supporting in this application. This is US English, Mexican Spanish, and Swedish Swedish. The localeConf itself tells our library where to go to find its languages.

In this case, the basePath is languages from the root directory, and then our defaultLocale is actually going to be Mexican Spanish. Then our sharedDictionary is common. That's what this guy is right here, common.lang.json. Then the file extension, as you can imagine, is lang.json.

These observable attributes are a regular expression for determining which attributes to watch on our elements that have localization applied. This is the default. We're not going to switch this up for this example, but it's something you can definitely check out in the documentation.

The animal controller is fairly simple. We have several supportedLanguages, along with their codes in the value field. We're choosing English as the default, and then we have all of our animals. Each has an English name, and then they also have a code.

This code refers to a file name, and then the actual key that they are in the key-value pair. If you take a look at these language files, they are pretty normal JSON files with a flat structure, that just have keys and then values.

This is Spanish. This is Swedish. This is English. In this case, they all replicate each other, which means they all should have proper translations for everything that's sitting out there on the DOM.

With that, the first thing we want to do is make sure we have the right directive in place to translate these animal names. Right now, it's just an ng-bind to the animal's name. You may recall there's an animal.code as well.

What we actually want to do is instead of ng-bind, we want to use the directive from the library, i18n. We need to set that to double-bracketed because we have a dynamic value here. This is going to be animal.code.

That small change alone, because our default language is Spanish, should actually change all these translations to Spanish. Already, angular-localization is pulling all those translations from our Spanish language file.

If we actually take a look back here, even if we switch it, nothing's changing yet. We're going to wire that up to actually switch the locale for us. This select box isn't really doing anything special.

Now that we've demonstrated the default language changing, I'm actually going to make our default en-US, which is just English US, because that's the first option chosen. If we take a look again, we're going to see that that comes right back for us.

How can we change that locale then? We can also use the internal service that angular-localization provides for us. What I'm going to do is I'm going to create a method for the selector right here. We're going to use that on ng-change.

Let's go up to the controller first. We'll call it "this.updateLocale." This is going to take a lang. The lang object will actually be one of these objects. We'll have a label and a value on it. We're going to use a service that is provided to us. It's called "locale."

We're going to pass that in there. We're just going to do locale.setLocale. All we're doing is saying, "Hey. The user just changed that." Here's our lang.value right here, which is going to be our locale. Let's set that locale accordingly.

If we go over here, we can just add a quick ng-change directive to this selector. It'll be ctrl.updateLocale, and then our language that is selected is ctrl.language. Cool. Now we've got a way to update that locale, as well a default, which is English in this case. The first is chosen. Now Spanish. Now Swedish.

That's pretty cool too. We got all these characters in there. What it's doing is it is loading up those files on the fly. Then it's saving them to memory after that point so that no matter what locale you're choosing, you've still got those strings available to you after that point.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?