Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good practice with Angular Translate

I'm attempting to use angular translate and I had a best practice question. Is it best practice to use a key for a string like so:

app.config(function($translateProvider) {
  $translateProvider.translations('en', {
    HEADLINE: 'Hello there, This is my awesome app!',
    INTRO_TEXT: 'And it has i18n support!'
  });
});

and

<h2>{{ 'HEADLINE' | translate }}</h2>
<p>{{ 'INTRO_TEXT' | translate }}</p>

as opposed to something like:

<h2 ng-bind="'Hello there, This is my awesome app!' | translate"></h2>

and if so why is one method best practice over the other? Is the using the key necessary for multi-language support?

like image 369
Jane Avatar asked Aug 31 '26 17:08

Jane


1 Answers

I am preferring short keys instead of the english text as key.. This will blow up your HTML and your config JS file.. when you use short keys you can also nest keys e.g.

{
  user: {
   name: 'Name', 
   email: 'E-Mail'
  }
} 

and use it like this in your HTML

<p translate="user.name"></p>

and you should use the directive instead of the filter.. here's why: https://github.com/angular-translate/angular-translate/wiki/Getting-Started#using-translate-directive

like image 94
Betty St Avatar answered Sep 04 '26 01:09

Betty St



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!