@transifex/native 2.0.1 → 2.2.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transifex/native",
3
- "version": "2.0.1",
3
+ "version": "2.2.0-alpha.0",
4
4
  "description": "Transifex Native for Javascript",
5
5
  "keywords": [
6
6
  "transifex",
package/src/TxNative.js CHANGED
@@ -68,6 +68,8 @@ export default class TxNative {
68
68
  that[value] = params[value];
69
69
  }
70
70
  });
71
+
72
+ this.fetchedTags = {}; // {langCode: [tag1, tag2, ...], ...}
71
73
  }
72
74
 
73
75
  /**
@@ -159,23 +161,37 @@ export default class TxNative {
159
161
  * @returns {Promise}
160
162
  */
161
163
  async fetchTranslations(localeCode, params = {}) {
164
+ const filterTags = params.filterTags || this.filterTags;
165
+
162
166
  if (!params.refresh
163
- && this.cache.hasTranslations(localeCode)
164
167
  && !this.cache.isStale(localeCode)
165
- ) {
168
+ && (
169
+ (!filterTags && this.cache.hasTranslations(localeCode))
170
+ || (filterTags
171
+ && (this.fetchedTags[localeCode] || []).indexOf(filterTags) !== -1)
172
+ )) {
166
173
  return;
167
174
  }
168
175
 
176
+ if (filterTags) {
177
+ if (!(localeCode in this.fetchedTags)) {
178
+ this.fetchedTags[localeCode] = [];
179
+ }
180
+ if (this.fetchedTags[localeCode].indexOf(filterTags) === -1) {
181
+ this.fetchedTags[localeCode].push(filterTags);
182
+ }
183
+ }
184
+
169
185
  // contact CDS
170
186
  try {
171
- sendEvent(FETCHING_TRANSLATIONS, localeCode, this);
187
+ sendEvent(FETCHING_TRANSLATIONS, { localeCode, filterTags }, this);
172
188
  let response;
173
189
  let lastResponseStatus = 202;
174
190
  while (lastResponseStatus === 202) {
175
191
  /* eslint-disable no-await-in-loop */
176
192
  let url = `${this.cdsHost}/content/${localeCode}`;
177
- if (this.filterTags) {
178
- url = `${url}?filter[tags]=${this.filterTags}`;
193
+ if (filterTags) {
194
+ url = `${url}?filter[tags]=${filterTags}`;
179
195
  }
180
196
  response = await axios.get(url, {
181
197
  headers: {
@@ -197,13 +213,13 @@ export default class TxNative {
197
213
  }
198
214
  });
199
215
  this.cache.update(localeCode, hashmap);
200
- sendEvent(TRANSLATIONS_FETCHED, localeCode, this);
216
+ sendEvent(TRANSLATIONS_FETCHED, { localeCode, filterTags }, this);
201
217
  } else {
202
- sendEvent(TRANSLATIONS_FETCH_FAILED, localeCode, this);
218
+ sendEvent(TRANSLATIONS_FETCH_FAILED, { localeCode, filterTags }, this);
203
219
  throw new Error('Could not fetch translations');
204
220
  }
205
221
  } catch (err) {
206
- sendEvent(TRANSLATIONS_FETCH_FAILED, localeCode, this);
222
+ sendEvent(TRANSLATIONS_FETCH_FAILED, { localeCode, filterTags }, this);
207
223
  throw err;
208
224
  }
209
225
  }
@@ -11,7 +11,10 @@ export default class MemoryCache {
11
11
  * @param {String} translations[key] - Translation string
12
12
  */
13
13
  update(localeCode, translations) {
14
- this.translationsByLocale[localeCode] = translations;
14
+ if (!this.translationsByLocale[localeCode]) {
15
+ this.translationsByLocale[localeCode] = {};
16
+ }
17
+ Object.assign(this.translationsByLocale[localeCode], translations);
15
18
  }
16
19
 
17
20
  /**
@@ -1,7 +1,9 @@
1
1
  /* eslint-disable class-methods-use-this */
2
2
  import MessageFormat from '@messageformat/core';
3
3
 
4
- const MF = new MessageFormat();
4
+ // object to cache MessageFormat classes related to
5
+ // specific locales
6
+ const MF = {};
5
7
 
6
8
  /**
7
9
  * MessageFormat renderer
@@ -11,7 +13,17 @@ const MF = new MessageFormat();
11
13
  */
12
14
  export default class MessageFormatRenderer {
13
15
  render(sourceString, localeCode, params) {
14
- const msg = MF.compile(sourceString);
16
+ // construct a MessageFormat class based on locale
17
+ // to make dates and other content localizable
18
+ const locale = ((localeCode || '').split('_'))[0];
19
+ if (!MF[locale]) {
20
+ try {
21
+ MF[locale] = new MessageFormat(locale);
22
+ } catch (err) {
23
+ MF[locale] = new MessageFormat();
24
+ }
25
+ }
26
+ const msg = MF[locale].compile(sourceString);
15
27
  return msg(params);
16
28
  }
17
29
  }