Internationalization with angularjs

Uri picture Uri · Jul 10, 2013 · Viewed 18.5k times · Source

I'm thinking of moving my site to angularjs, and I want to start very small, by moving all my static server-side plain-text templating from django to angular (otherwise there will be syntax trouble with the '{{}}').

It seems that the best way to do that will be one of two options:

  1. To have an ajax call that returns a JSON with all the texts of my site. The texts will be stored in a variable which is binded to my HTML elements so angular will update everything.
  2. To store a static js file with the dictionary and include it in my HTML and bind the dictionary with angularjs.

Both options will allow me to switch between languages without reloading the page.

Which one is better? In general, is this a good approach or is there a more correct way?

Answer

JD Smith picture JD Smith · Dec 5, 2013

I tried out a few different options, including Angular Translate, but I liked Angular-gettext the best so far.

One thing that helped tremendously is that there's a working demo for it where they i18n TodoMVC, called angular-gettext-example.

The workflow is simple:

  1. Add the "translate" directive to your templates
  2. Run grunt to extract .pot template(s)
  3. Hand off the .pot to your translation vendor or DIY with POEdit or similar software
  4. Drop the .po translation files back into your project
  5. Run grunt to compile the .po files
  6. Set the default language in your scope
  7. Watch the magic!

I'm sure the other solutions posted here are good as well but I haven't seen an end-to-end example so nicely organized as angular-gettext-example.

Cheers, JD