20

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?

Uri
  • 25,622
  • 10
  • 45
  • 72
  • Hi Uri, I've been playing with the same issue: integrating the .po files used by my Django app(back-end) into an AngularJS app (Front-end). Can you provide me more info about how you reached this finally? Both approaches works for me, although I'd rather 1. ;-) Thanks in advance. – trinchet Oct 24 '13 at 15:06
  • Went with option #2. It was much more simple and did the job. – Uri May 29 '14 at 11:03
  • How I did it http://stackoverflow.com/questions/19881590/how-you-deal-with-translation-multilanguage-webservices-in-django-rest-framewo/28078356#28078356 – littlegreen Jan 21 '15 at 22:54

4 Answers4

33

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

JD Smith
  • 1,764
  • 15
  • 17
9

First of all, there is a way to change angular's delimiters to other symbols as answered here: Angular JS custom delimiter

The 2. option is easier. You include it once and you have all translations on page load. No async calls, no promises, nice and easy.

And yet i'd go with the first one. Services like $translate would really make your life easier following option 1. Plus it has many options for loading and storing loaded data in LocalStorage and cookies, so there's plenty of space for extension and customization. You can then translate your content with $translate service, directive or filter.

And don't forget that 2 option disables any options of cached requests. On each request to your start page the server has to read static file and include it in the html. With first option the user's browser can cache .json for as long as you like.

Community
  • 1
  • 1
package
  • 4,741
  • 1
  • 25
  • 35
4

AngularJS supports il8n/L10n for currency, date and numbers filters only. According to this book:

Book-shot! (sorry for the low quality! cell phone camera)

I would say follow the first approach and load the translation dynamically. It would involve a lot of work but there's no other way around

Ahmad Alfy
  • 13,107
  • 6
  • 65
  • 99
4

Have a look at angular-translate :)

It solves both scenarios!

ivoputzer
  • 6,427
  • 1
  • 25
  • 43
Pascal Precht
  • 8,803
  • 7
  • 41
  • 53
  • There is also jlg-i18n, which solves both problems while adding interpolation and pluralization. https://github.com/jlguenego/jlg-i18n – jlguenego May 22 '15 at 14:41