简体   繁体   中英

Translating text within controller with AngularJs

Im using angular translate and need to translate some words in the controller. Im using json files for translation in the html.

// LABELS
var monthNames = [
    "Janvier", "Février", "Mars",
    "Avril", "Mai", "Juin", "Juillet",
    "Août", "Septembre", "Octobre",
    "Novembre", "Décembre"
];
//some code here
title: {
    display: true,
    text: 'Commandes'
}

Maybe you can define an plus level on monthNames Objects with the language you need

var monthNames = {
  "fr-fr": [
    "Janvier", "Février", "Mars",
    "Avril", "Mai", "Juin", "Juillet",
    "Août", "Septembre", "Octobre",
    "Novembre", "Décembre"
  ]
},

在您的控制器中注入$filter服务,然后您可以自由地将它与任何服务一起使用(在此示例中与翻译服务一起使用)

const translation = $filter('translate')('HELLO_WORLD'); // HELLO_WORLD is the definition from the translation json file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM