-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (136 loc) · 3.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
if(typeof define !== 'function')
var define = require('amdefine')(module);
define(["require", "deepjs/deep", "deepjs/lib/stores/cache" ],function (require, deep)
{
//__________________________________________________
deep.protocols.translate = new deep.Store();
deep.protocols.localisation = new deep.Store();
// deep.protocols.translate.map = {};
deep.protocols.localisation.options = {};
deep.protocols.localisation.get = function (id)
{
var options = deep.protocols.localisation.options || {};
//console.log("Getting localisation file Language = ", options.language);
var lang = null;
if(options)
{
if(options.language)
lang = options.language;
}
if(!lang)
return deep.errors.Protocole("No language setted for localisation protocol (get)");
console.log("Getting localisation file for module : ", id, " Language = ", lang);
return deep("json::" + id + lang + ".json")
.flatten()
.fail(function (err) {
return deep.errors.Protocole("No file loaded for localisation protocol (get)");
});
};
var filter = function (root, language, wrap, originPath) {
// console.log("filter trans : ", root, language, wrap, originPath);
var res = {};
var current = null;
var stack = [{ value:root, cur:res, path:"/" }];
while(stack.length > 0)
{
current = stack.pop();
currentRes = current.cur;
var v = current.value;
var r = [];
if(v.forEach)
{
var len = v.length;
for(var i = 0; i < len; ++i)
{
var va = v[i];
if(typeof va === 'object')
{
if(va.push)
currentRes[i] = [];
else
currentRes[i] = {};
r.unshift({ value:va, cur:currentRes[i], path:current.path+i+"/" });
}
}
}
else
for(var i in v)
{
var va = v[i];
if(typeof va[language] === 'string')
{
//console.log("Do we have Wrap() ?????????? ", wrap);
currentRes[i] = wrap?wrap(va[language], current.path+i, originPath ):va[language];
}
else if(typeof va == "object")
{
if(va.push)
currentRes[i] = [];
else
currentRes[i] = {};
r.unshift({ value:va, cur:currentRes[i], path:current.path+i+"/" });
}
}
if(r.length > 0)
stack = stack.concat(r);
//console.log("stack : ", stack)
}
return res;
};
deep.protocols.translate.options = null;
deep.protocols.translate.stock = {};
deep.protocols.translate.get = function (id, opt)
{
var options = deep.protocols.translate.options || {};
if(options && options._deep_ocm_)
options = options();
var lang = null;
if(options)
{
if(options.language)
lang = options.language();
else if (options && options.defaultLanguage)
lang = options.defaultLanguage;
}
if(!lang)
return deep.errors.Protocole("No language available for translate protocol (get)");
var parsedPath = id.split(" ");
var key = null;
id = parsedPath.shift();
if(parsedPath.length == 1)
key = parsedPath.shift();
var cacheName = "translate-" + lang + "::" +id;
if(options.cache !== false && deep.mediaCache.cache[cacheName])
{
var cached = deep.mediaCache.cache[cacheName];
return cached.then(function(success){
if(key)
deep.protocols.translate.stock[key] = success;
});
}
var self = this;
var d = deep.when(deep.get("json::" + id))
.done(function (data) {
var ok = deep.Querier.firstObjectWithProperty(data, lang);
if(!ok)
console.error( "current language not found in translation file : "+id+". Please update it for : ", lang );
var resi = filter(data, lang, options.wrap, id);
if(key)
deep.protocols.translate.stock[key] = resi;
// console.log("translation resi : ", resi);
return resi;
});
if((options && options.cache !== false) || (options && options.cache !== false))
deep.mediaCache.manage(d, cacheName);
return d;
};
/*
deep.protocols.translate.patch = function (value, options) {
//TODO
};
*/
return function (opt) {
//console.log("Options in tranlsate protocol = ", opt);
deep.protocols.translate.options = opt || {};
};
});