AngularJS翻译对象attibute

时间:2016-10-18 16:45:15

标签: angularjs internationalization

在我的angular指令中,我有一个具有2个属性的对象:object.title_en(英文标题)和object.title_fr(法文标题)。如何根据$ translate.use()?

获得的当前语言显示和继续显示正确​​的标题

修改

这是我的HTML模板:

<div layout="column"> <div> {{object.title_fr | limitTo: 50}} </div> <div> {{object.description_fr | limitTo: 150}} </div> </div>

这是我的JSON对象:

{ "title_en" : "Title in english", "title_fr" : "Title in french", "description_en" : "Description in english", "description_fr" : "Description in french" }

我希望能够根据当前语言$ translate.use()将其翻译成英文。

非常感谢,

1 个答案:

答案 0 :(得分:0)

将JSON对象更改为两个单独的对象。然后指定每种语言使用哪一种。

以下是一个例子:

if (!require("pacman")) install.packages("pacman")

pacman::p_load(shiny, plotly)

nCodeNames <- c("a","b","c","d","e","f","g","h","i","j","k","l")


shinyUI(navbarPage(
  "E-N Matrics: Proportion of E-Code Resulting in each N-Code",

  tabPanel("Grid",

            lapply(c(1:4), function(i) fluidRow(
              lapply(c(1:3), function(j) column(4, plotlyOutput(paste0("grid",nCodeNames[(i-1)*3+j]))))
             ))

           #fluidRow(column(4,plotlyOutput(paste0("grid",nCodeNames[(1-1)*3+1]))),column(4,plotly))
  )
  ))

然后您可以在HTML中使用它,如下所示: myModule.config(['$translateProvider', function($translateProvider) { var language_fr={ "title" : "Title in french", "description" : "Description in french" }; var language_en= { "title" : "Title in english", "description" : "Description in english", }; $translateProvider .translations('en', language_en) .translations('fr', language_fr) .preferredLanguage('en') .fallbackLanguage('en') .useSanitizeValueStrategy('escape') ; }]);