gpt4 book ai didi

javascript - Angular 翻译使用 useStaticFilesLoader 加载嵌套的 json

转载 作者:行者123 更新时间:2023-11-30 00:19:54 25 4
gpt4 key购买 nike

我有一个像这样的嵌套 json 结构

    {
"module1": {
"component1": "text1",
"component2": "text2"
}
}

所以在我的代码中,我使用了 {"module1.component1"| translate 永远不会得到解决,而如果我这样做了

    {      
"component1": "text1",
"component2": "text2"
}

{"component1"|翻译 有效。有没有办法使用 useStaticFilesLoader

解析嵌套的 JSON 数据

编辑 1:这是我的 useStaticFilesLoader 配置:

$translateProvider.useStaticFilesLoader({
prefix: './langfiles/',
suffix: '.json'
});
$translate.use(lang);//lang is derived from header

langfiles 包含: en_US.json 等

最佳答案

查看这篇文章:Accessing nested JSON objects看看是否有帮助。他们的 JSFiddle 展示了如何完成:http://jsfiddle.net/z1uLjg89/

// Including ngTranslate
angular.module("ngTranslate", ["ng"]).config(["$provide", function (n) {
$TranslateProvider = function () {
var n, t = {};
this.translations = function (n, r) {
if (!n && !r) return t;
if (n && !r) {
if (angular.isString(n)) return t[n];
t = n
} else t[n] = r
}, this.uses = function (r) {
if (!r) return n;
if (!t[r]) throw Error("$translateProvider couldn't find translationTable for langKey: '" + r + "'");
n = r
}, this.$get = ["$interpolate", "$log", function (r, a) {
return $translate = function (e, i) {
var l = n ? t[n][e] : t[e];
return l ? r(l)(i) : (a.warn("Translation for " + e + " doesn't exist"), e)
}
}]
}, n.provider("$translate", $TranslateProvider)
}]), angular.module("ngTranslate").directive("translate", ["$filter", "$interpolate", function (n, t) {
var r = n("translate");
return {
restrict: "A",
scope: !0,
link: function (n, a, e) {
e.$observe("translate", function (r) {
n.translationId = angular.equals(r, "") ? t(a.text())(n.$parent) : r
}), e.$observe("values", function (t) {
n.interpolateParams = t
}), n.$watch("translationId + interpolateParams", function () {
a.html(r(n.translationId, n.interpolateParams))
})
}
}
}]), angular.module("ngTranslate").filter("translate", ["$parse", "$translate", function (n, t) {
return function (r, a) {
return angular.isObject(a) || (a = n(a)()), t(r, a)
}
}]);

// Configuring your module, asking for ngTranslate as dependency
var app = angular.module('myApp', ['ngTranslate']);

// Configuring $translateProvider
app.config(['$translateProvider', function ($translateProvider) {

// Simply register translation table as object hash
$translateProvider.translations('en', {
"SEARCH": {
"SEARCH": "Recherce",
"ABILITY": "Abilities",
"MANAGEMENT": "Management Competencies",
"PERSONAL": "Personal Suitability"
},

"ABILITIES": {
"TITLE": "Test Title here",
"ADVISORY": {
"TITLE": "Advisory Skills",
"QUESTIONS": [{
"TYPE": "A",
"LEVEL": "45",
"DESCRIPTION": "Can you tell me how awesome you are"
}, {
"TYPE": "B",
"LEVEL": "100",
"DESCRIPTION": "Tell me about your wicked project"
}]
}
},

"HEADLINE": "Oh No!",
"SUB_HEADLINE": "Looks like you are not amazing"
});

var list = $translateProvider.translations('en');
console.log(list);
var getTitle = list.HEADLINE;
var getSearch = list.SEARCH.ABILITY;
console.log(getSearch);
console.log(getTitle);
}]);
<div ng-app="myApp"></div>

一种可能的解决方法是展平您的 json 文件

Fastest way to flatten / un-flatten nested JSON objects或者 Convert complex JavaScript object to dot notation object

这个 fiddle 向您展示了它的外观:http://fiddle.jshell.net/blowsie/S2hsS/show/light/

关于javascript - Angular 翻译使用 useStaticFilesLoader 加载嵌套的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33537395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com