angular translate using useStaticFilesLoader to load nested json

user429035

I have a nested json structure like this

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

So inside my code I am using {"module1.component1" | translate} which never get resolved whereas if I do

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

{"component1" | translate} works . Is there a way to resolve nested JSON data using useStaticFilesLoader

edit 1: here is my useStaticFilesLoader config looks like :

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

langfiles contains : en_US.json etc

Enkode

Check out this post: Accessing nested JSON objects and see if that helps. Their JSFiddle show how it can be done: 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>

A possible work around is to flatten your json file.

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

This fiddle shows you how what it will look like: http://fiddle.jshell.net/blowsie/S2hsS/show/light/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angular translate using useStaticFilesLoader to load nested json

From Dev

translate with angular does not work with useStaticFilesLoader

From Dev

angular-translate nested JSON and filter

From Dev

angular-translate: How to load static json files?

From Dev

ExtJS load nested JSON

From Dev

Angular translate, Using staticFilesLoader not working

From Dev

How to load Nested Arrays in Angular?

From Dev

Error: JSON Parse error: Property name must be a string literal when using angular translate

From Dev

How to translate a date object using angular-translate?

From Dev

translate using https://angular-translate.github.io/

From Dev

load json nested object with jquery

From Dev

JSON to HTML with breaklines, angular-translate

From Dev

How to access nested objects in JSON using angular JS

From Dev

Parsing the nested array of objects in JSON using Angular.js

From Dev

Angular-grid when using $http to load json data

From Dev

nested json in angular 2

From Dev

Load JSON into Angular

From Dev

How to show translations using Angular-Translate?

From Dev

Set title attribute with condition and using angular translate

From Dev

AngularJS translation with pluralization using angular-translate

From Dev

Using angular-translate on state parameters

From Dev

Angular Translate: Using .replace in an interpolation string in HTML

From Dev

angular-translate: unable to use dynamic data in nested directives

From Dev

How to fix angular-translate showing translation key on webpage on load?

From Dev

Nested options using angular

From Dev

Angular Translate

From Dev

RestKit using nested JSON

From Dev

Form nested nested Json Html and Angular

From Dev

How to load multidimensional / nested json into a store?

Related Related

  1. 1

    angular translate using useStaticFilesLoader to load nested json

  2. 2

    translate with angular does not work with useStaticFilesLoader

  3. 3

    angular-translate nested JSON and filter

  4. 4

    angular-translate: How to load static json files?

  5. 5

    ExtJS load nested JSON

  6. 6

    Angular translate, Using staticFilesLoader not working

  7. 7

    How to load Nested Arrays in Angular?

  8. 8

    Error: JSON Parse error: Property name must be a string literal when using angular translate

  9. 9

    How to translate a date object using angular-translate?

  10. 10

    translate using https://angular-translate.github.io/

  11. 11

    load json nested object with jquery

  12. 12

    JSON to HTML with breaklines, angular-translate

  13. 13

    How to access nested objects in JSON using angular JS

  14. 14

    Parsing the nested array of objects in JSON using Angular.js

  15. 15

    Angular-grid when using $http to load json data

  16. 16

    nested json in angular 2

  17. 17

    Load JSON into Angular

  18. 18

    How to show translations using Angular-Translate?

  19. 19

    Set title attribute with condition and using angular translate

  20. 20

    AngularJS translation with pluralization using angular-translate

  21. 21

    Using angular-translate on state parameters

  22. 22

    Angular Translate: Using .replace in an interpolation string in HTML

  23. 23

    angular-translate: unable to use dynamic data in nested directives

  24. 24

    How to fix angular-translate showing translation key on webpage on load?

  25. 25

    Nested options using angular

  26. 26

    Angular Translate

  27. 27

    RestKit using nested JSON

  28. 28

    Form nested nested Json Html and Angular

  29. 29

    How to load multidimensional / nested json into a store?

HotTag

Archive