otsdaq_utilities  v2_05_02_indev
localize.js
1 var Localize = function(table){
2  this.table = table; //hold Tabulator object
3  this.locale = "default"; //current locale
4  this.lang = false; //current language
5  this.bindings = {}; //update events to call when locale is changed
6 };
7 
8 //set header placehoder
9 Localize.prototype.setHeaderFilterPlaceholder = function(placeholder){
10  this.langs.default.headerFilters.default = placeholder;
11 };
12 
13 //set header filter placeholder by column
14 Localize.prototype.setHeaderFilterColumnPlaceholder = function(column, placeholder){
15  this.langs.default.headerFilters.columns[column] = placeholder;
16 
17  if(this.lang && !this.lang.headerFilters.columns[column]){
18  this.lang.headerFilters.columns[column] = placeholder;
19  }
20 };
21 
22 //setup a lang description object
23 Localize.prototype.installLang = function(locale, lang){
24  if(this.langs[locale]){
25  this._setLangProp(this.langs[locale], lang);
26  }else{
27  this.langs[locale] = lang;
28  }
29 };
30 
31 Localize.prototype._setLangProp = function(lang, values){
32  for(let key in values){
33  if(lang[key] && typeof lang[key] == "object"){
34  this._setLangProp(lang[key], values[key])
35  }else{
36  lang[key] = values[key];
37  }
38  }
39 };
40 
41 
42 //set current locale
43 Localize.prototype.setLocale = function(desiredLocale){
44  var self = this;
45 
46  desiredLocale = desiredLocale || "default";
47 
48  //fill in any matching languge values
49  function traverseLang(trans, path){
50  for(var prop in trans){
51 
52  if(typeof trans[prop] == "object"){
53  if(!path[prop]){
54  path[prop] = {};
55  }
56  traverseLang(trans[prop], path[prop]);
57  }else{
58  path[prop] = trans[prop];
59  }
60  }
61  }
62 
63  //determing correct locale to load
64  if(desiredLocale === true && navigator.language){
65  //get local from system
66  desiredLocale = navigator.language.toLowerCase();
67  }
68 
69  if(desiredLocale){
70 
71  //if locale is not set, check for matching top level locale else use default
72  if(!self.langs[desiredLocale]){
73  let prefix = desiredLocale.split("-")[0];
74 
75  if(self.langs[prefix]){
76  console.warn("Localization Error - Exact matching locale not found, using closest match: ", desiredLocale, prefix);
77  desiredLocale = prefix;
78  }else{
79  console.warn("Localization Error - Matching locale not found, using default: ", desiredLocale);
80  desiredLocale = "default";
81  }
82  }
83  }
84 
85  self.locale = desiredLocale;
86 
87  //load default lang template
88  self.lang = Tabulator.prototype.helpers.deepClone(self.langs.default || {});
89 
90  if(desiredLocale != "default"){
91  traverseLang(self.langs[desiredLocale], self.lang);
92  }
93 
94  self.table.options.localized.call(self.table, self.locale, self.lang);
95 
96  self._executeBindings();
97 };
98 
99 //get current locale
100 Localize.prototype.getLocale = function(locale){
101  return self.locale;
102 };
103 
104 //get lang object for given local or current if none provided
105 Localize.prototype.getLang = function(locale){
106  return locale ? this.langs[locale] : this.lang;
107 };
108 
109 //get text for current locale
110 Localize.prototype.getText = function(path, value){
111  var path = value ? path + "|" + value : path,
112  pathArray = path.split("|"),
113  text = this._getLangElement(pathArray, this.locale);
114 
115  // if(text === false){
116  // console.warn("Localization Error - Matching localized text not found for given path: ", path);
117  // }
118 
119  return text || "";
120 };
121 
122 //traverse langs object and find localized copy
123 Localize.prototype._getLangElement = function(path, locale){
124  var self = this;
125  var root = self.lang;
126 
127  path.forEach(function(level){
128  var rootPath;
129 
130  if(root){
131  rootPath = root[level];
132 
133  if(typeof rootPath != "undefined"){
134  root = rootPath;
135  }else{
136  root = false;
137  }
138  }
139  });
140 
141  return root;
142 };
143 
144 //set update binding
145 Localize.prototype.bind = function(path, callback){
146  if(!this.bindings[path]){
147  this.bindings[path] = [];
148  }
149 
150  this.bindings[path].push(callback);
151 
152  callback(this.getText(path), this.lang);
153 };
154 
155 //itterate through bindings and trigger updates
156 Localize.prototype._executeBindings = function(){
157  var self = this;
158 
159  for(let path in self.bindings){
160  self.bindings[path].forEach(function(binding){
161  binding(self.getText(path), self.lang);
162  });
163  }
164 };
165 
166 //Localized text listings
167 Localize.prototype.langs = {
168  "default":{ //hold default locale text
169  "groups":{
170  "item":"item",
171  "items":"items",
172  },
173  "columns":{
174  },
175  "ajax":{
176  "loading":"Loading",
177  "error":"Error",
178  },
179  "pagination":{
180  "page_size":"Page Size",
181  "first":"First",
182  "first_title":"First Page",
183  "last":"Last",
184  "last_title":"Last Page",
185  "prev":"Prev",
186  "prev_title":"Prev Page",
187  "next":"Next",
188  "next_title":"Next Page",
189  },
190  "headerFilters":{
191  "default":"filter column...",
192  "columns":{}
193  }
194  },
195 };
196 
197 Tabulator.prototype.registerModule("localize", Localize);