@cloudflare/intl-core
Advanced tools
Comparing version 1.2.6 to 1.4.0
@@ -6,3 +6,3 @@ # Change Log | ||
## [1.2.6](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/intl-core@1.2.4...@cloudflare/intl-core@1.2.6) (2019-10-15) | ||
# [1.4.0](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/intl-core@1.2.4...@cloudflare/intl-core@1.4.0) (2019-11-20) | ||
@@ -12,8 +12,31 @@ | ||
* **stratus:** UI-2859: bump all package versions ([4cb3c90](http://stash.cfops.it:7999/fe/stratus/commits/4cb3c90)) | ||
* **stratus:** UI-2859: bump all package versions ([6db21c2](http://stash.cfops.it:7999/fe/stratus/commits/6db21c2)) | ||
### Features | ||
* **intl-core:** REG-1471 Add strongly typed interpolation ([70a4ee8](http://stash.cfops.it:7999/fe/stratus/commits/70a4ee8)) | ||
# [1.3.0](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/intl-core@1.2.4...@cloudflare/intl-core@1.3.0) (2019-11-19) | ||
### Bug Fixes | ||
* **stratus:** UI-2859: bump all package versions ([4cb3c90](http://stash.cfops.it:7999/fe/stratus/commits/4cb3c90)) | ||
* **stratus:** UI-2859: bump all package versions ([6db21c2](http://stash.cfops.it:7999/fe/stratus/commits/6db21c2)) | ||
### Features | ||
* **intl-core:** REG-1471 Add strongly typed interpolation ([70a4ee8](http://stash.cfops.it:7999/fe/stratus/commits/70a4ee8)) | ||
<a name="1.2.4"></a> | ||
@@ -20,0 +43,0 @@ ## [1.2.4](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/intl-core@1.2.3...@cloudflare/intl-core@1.2.4) (2019-02-03) |
@@ -5,2 +5,3 @@ import Translator from './Translator'; | ||
export * from './prepare'; | ||
export * from './Interpolation'; | ||
export declare type TLoadPhrases = tempTLoadPhrases; | ||
@@ -7,0 +8,0 @@ export declare type IPhrases = tempIPhrases; |
@@ -1,38 +0,44 @@ | ||
export declare type TTransPrepResult<T extends { | ||
[key: string]: string; | ||
}> = { | ||
namespace: string; | ||
keys: { | ||
[P in keyof T]: P; | ||
}; | ||
import { Interpolation } from './Interpolation'; | ||
declare type Catalog = { | ||
[key: string]: string | Interpolation<any> | Catalog; | ||
}; | ||
declare type Keys<T extends Catalog> = { | ||
[K in keyof T]: T[K] extends Interpolation<any> ? T[K] : T[K] extends string ? K : T[K] extends Catalog ? Keys<T[K]> : never; | ||
}; | ||
export declare type TranslationPrepareResult<T extends Catalog> = { | ||
keys: Keys<T>; | ||
translations: { | ||
[key: string]: string; | ||
}; | ||
namespace: string; | ||
}; | ||
export declare function prepareTranslations<T extends { | ||
[key: string]: string; | ||
}>(namespace: string, trans: T): TTransPrepResult<T>; | ||
/** | ||
* Takes a non-namespaced object of translations and returns an object | ||
* whose keys are the keys of the translatiosn and the values are the | ||
* keys prefixed with namespace | ||
* @param namespace What namespace to prefix the the keys | ||
* @param translations Standard translations object { "foo_bar_baz": "Some cool string" } | ||
* Extracts the union of all the leaf keys in a catalog | ||
*/ | ||
export declare function toStrings<T extends { | ||
[key: string]: string; | ||
}>(namespace: string, translations: T): { | ||
[P in keyof T]: P; | ||
}; | ||
export declare type KeysType<T> = { | ||
[K in keyof T]: T[K] extends Interpolation<any> ? T[K] : T[K] extends object ? KeysType<T[K]> : T[K]; | ||
}[keyof T]; | ||
/** | ||
* Prepares some translations and their keys for use with an instance of Translator | ||
* @param translations Standard translations object {"foo": "some cool strign" } | ||
* @param strings a namespaced set of translationns keys | ||
* Prepares a catalog for use with our intl libraries and exports | ||
* a keys object, which can be used to get the final key of a string: | ||
* | ||
* @example | ||
* import { prepareTranslations } from '@cloudflare/intl-core' | ||
* import { Trans } from '@cloudflare/intl-react' | ||
* const { keys, translations } = prepareTranslations('my-product', { | ||
* someFeature: { | ||
* title: 'Some feature title', | ||
* description: 'Some feature description', | ||
* }, | ||
* help: 'The help text' | ||
* }) | ||
* | ||
* console.log(keys.someFeature.title) // "my-product.someFeature.title" | ||
* | ||
* <Trans id={keys.someFeature.description} /> | ||
* | ||
* @param namespace Namespace of the catalog (prefixes with "${namespace}.") | ||
* @param trans The unprefixed keys/vals of the english catalog | ||
*/ | ||
export declare function translationsForTranslator<T extends { | ||
[key: string]: string; | ||
}, S extends { | ||
[key: string]: string; | ||
}>(translations: T, strings: S): { | ||
[key: string]: string; | ||
}; | ||
export declare function prepareTranslations<T extends Catalog>(namespace: string, trans: T): TranslationPrepareResult<T>; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
import { TTransPrepResult } from './prepare'; | ||
import { TranslationPrepareResult } from './prepare'; | ||
export declare type TLoadPhrases = (language: ESupportedLocales) => Promise<IPhrases>; | ||
@@ -12,5 +12,5 @@ export declare enum ESupportedLocales { | ||
default: IPhrases; | ||
}) | Partial<TTransPrepResult<any>>; | ||
}) | Partial<TranslationPrepareResult<any>>; | ||
export declare type TLoadLocales = { | ||
[locale in ESupportedLocales]: () => Promise<TPhrasesModule>; | ||
}; |
@@ -5,2 +5,3 @@ import Translator from './Translator'; | ||
export * from './prepare'; | ||
export * from './Interpolation'; | ||
export { ESupportedLocales, Translator, makeLoadPhrases }; |
@@ -1,41 +0,78 @@ | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import set from 'lodash.set'; | ||
import { Interpolation } from './Interpolation'; | ||
/** | ||
* Prepares a catalog for use with our intl libraries and exports | ||
* a keys object, which can be used to get the final key of a string: | ||
* | ||
* @example | ||
* import { prepareTranslations } from '@cloudflare/intl-core' | ||
* import { Trans } from '@cloudflare/intl-react' | ||
* const { keys, translations } = prepareTranslations('my-product', { | ||
* someFeature: { | ||
* title: 'Some feature title', | ||
* description: 'Some feature description', | ||
* }, | ||
* help: 'The help text' | ||
* }) | ||
* | ||
* console.log(keys.someFeature.title) // "my-product.someFeature.title" | ||
* | ||
* <Trans id={keys.someFeature.description} /> | ||
* | ||
* @param namespace Namespace of the catalog (prefixes with "${namespace}.") | ||
* @param trans The unprefixed keys/vals of the english catalog | ||
*/ | ||
export function prepareTranslations(namespace, trans) { | ||
var keys = toStrings(namespace, trans); | ||
var translations = translationsForTranslator(trans, keys); | ||
var translations = flattenCatalog(namespace, trans); | ||
var keys = Object.keys(translations).reduce(function (result, key) { | ||
return set(result, key.substring("".concat(namespace, ".").length), key); | ||
}, {}); | ||
return { | ||
namespace: namespace, | ||
translations: translations, | ||
keys: keys, | ||
translations: translations | ||
namespace: namespace | ||
}; | ||
} | ||
/** | ||
* Takes a non-namespaced object of translations and returns an object | ||
* whose keys are the keys of the translatiosn and the values are the | ||
* keys prefixed with namespace | ||
* @param namespace What namespace to prefix the the keys | ||
* @param translations Standard translations object { "foo_bar_baz": "Some cool string" } | ||
* Takes a nested catalog of the form: | ||
* { | ||
* feature: { | ||
* modal: { | ||
* title: "Hello!" | ||
* } | ||
* } | ||
* } | ||
* | ||
* And flattens it, prefixing a namespace: | ||
* | ||
* { | ||
* "feature.modal.title": "Hello!" | ||
* } | ||
* @param namespace Namespace of the catalog | ||
* @param obj The translations catalog | ||
*/ | ||
export function toStrings(namespace, translations) { | ||
return Object.keys(translations).reduce(function (result, key) { | ||
var _objectSpread2; | ||
function flattenCatalog(namespace, obj) { | ||
var result = {}; | ||
var keys = [namespace]; | ||
return _objectSpread({}, result, (_objectSpread2 = {}, _defineProperty(_objectSpread2, key, "".concat(namespace, ".").concat(key)), _defineProperty(_objectSpread2, "".concat(namespace, ".").concat(key), "".concat(namespace, ".").concat(key)), _objectSpread2)); | ||
}, {}); | ||
} | ||
/** | ||
* Prepares some translations and their keys for use with an instance of Translator | ||
* @param translations Standard translations object {"foo": "some cool strign" } | ||
* @param strings a namespaced set of translationns keys | ||
*/ | ||
function iterate(obj) { | ||
Object.keys(obj).forEach(function (key) { | ||
keys.push(key); | ||
export function translationsForTranslator(translations, strings) { | ||
return Object.keys(translations).reduce(function (result, key) { | ||
var resultKey = strings[key]; | ||
var resultValue = translations[key]; | ||
return _objectSpread({}, result, _defineProperty({}, resultKey, resultValue)); | ||
}, {}); | ||
if (typeof obj[key] === 'string' || obj[key] instanceof Interpolation) { | ||
result[keys.join('.')] = obj[key].toString(); | ||
} else if (_typeof(obj[key]) === 'object' && obj[key] !== null) { | ||
iterate(obj[key]); | ||
} | ||
keys.pop(); | ||
}); | ||
} | ||
iterate(obj); | ||
return result; | ||
} |
@@ -49,2 +49,15 @@ "use strict"; | ||
var _Interpolation = require("./Interpolation"); | ||
Object.keys(_Interpolation).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _Interpolation[key]; | ||
} | ||
}); | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
@@ -7,47 +7,84 @@ "use strict"; | ||
exports.prepareTranslations = prepareTranslations; | ||
exports.toStrings = toStrings; | ||
exports.translationsForTranslator = translationsForTranslator; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
var _lodash = _interopRequireDefault(require("lodash.set")); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var _Interpolation = require("./Interpolation"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
/** | ||
* Prepares a catalog for use with our intl libraries and exports | ||
* a keys object, which can be used to get the final key of a string: | ||
* | ||
* @example | ||
* import { prepareTranslations } from '@cloudflare/intl-core' | ||
* import { Trans } from '@cloudflare/intl-react' | ||
* const { keys, translations } = prepareTranslations('my-product', { | ||
* someFeature: { | ||
* title: 'Some feature title', | ||
* description: 'Some feature description', | ||
* }, | ||
* help: 'The help text' | ||
* }) | ||
* | ||
* console.log(keys.someFeature.title) // "my-product.someFeature.title" | ||
* | ||
* <Trans id={keys.someFeature.description} /> | ||
* | ||
* @param namespace Namespace of the catalog (prefixes with "${namespace}.") | ||
* @param trans The unprefixed keys/vals of the english catalog | ||
*/ | ||
function prepareTranslations(namespace, trans) { | ||
var keys = toStrings(namespace, trans); | ||
var translations = translationsForTranslator(trans, keys); | ||
var translations = flattenCatalog(namespace, trans); | ||
var keys = Object.keys(translations).reduce(function (result, key) { | ||
return (0, _lodash.default)(result, key.substring("".concat(namespace, ".").length), key); | ||
}, {}); | ||
return { | ||
namespace: namespace, | ||
translations: translations, | ||
keys: keys, | ||
translations: translations | ||
namespace: namespace | ||
}; | ||
} | ||
/** | ||
* Takes a non-namespaced object of translations and returns an object | ||
* whose keys are the keys of the translatiosn and the values are the | ||
* keys prefixed with namespace | ||
* @param namespace What namespace to prefix the the keys | ||
* @param translations Standard translations object { "foo_bar_baz": "Some cool string" } | ||
* Takes a nested catalog of the form: | ||
* { | ||
* feature: { | ||
* modal: { | ||
* title: "Hello!" | ||
* } | ||
* } | ||
* } | ||
* | ||
* And flattens it, prefixing a namespace: | ||
* | ||
* { | ||
* "feature.modal.title": "Hello!" | ||
* } | ||
* @param namespace Namespace of the catalog | ||
* @param obj The translations catalog | ||
*/ | ||
function toStrings(namespace, translations) { | ||
return Object.keys(translations).reduce(function (result, key) { | ||
var _objectSpread2; | ||
function flattenCatalog(namespace, obj) { | ||
var result = {}; | ||
var keys = [namespace]; | ||
return _objectSpread({}, result, (_objectSpread2 = {}, _defineProperty(_objectSpread2, key, "".concat(namespace, ".").concat(key)), _defineProperty(_objectSpread2, "".concat(namespace, ".").concat(key), "".concat(namespace, ".").concat(key)), _objectSpread2)); | ||
}, {}); | ||
} | ||
/** | ||
* Prepares some translations and their keys for use with an instance of Translator | ||
* @param translations Standard translations object {"foo": "some cool strign" } | ||
* @param strings a namespaced set of translationns keys | ||
*/ | ||
function iterate(obj) { | ||
Object.keys(obj).forEach(function (key) { | ||
keys.push(key); | ||
if (typeof obj[key] === 'string' || obj[key] instanceof _Interpolation.Interpolation) { | ||
result[keys.join('.')] = obj[key].toString(); | ||
} else if (_typeof(obj[key]) === 'object' && obj[key] !== null) { | ||
iterate(obj[key]); | ||
} | ||
function translationsForTranslator(translations, strings) { | ||
return Object.keys(translations).reduce(function (result, key) { | ||
var resultKey = strings[key]; | ||
var resultValue = translations[key]; | ||
return _objectSpread({}, result, _defineProperty({}, resultKey, resultValue)); | ||
}, {}); | ||
keys.pop(); | ||
}); | ||
} | ||
iterate(obj); | ||
return result; | ||
} |
{ | ||
"name": "@cloudflare/intl-core", | ||
"description": "", | ||
"version": "1.2.6", | ||
"version": "1.4.0", | ||
"types": "./dist/index.d.ts", | ||
@@ -15,5 +15,6 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@cloudflare/util-markdown": "^1.2.3", | ||
"@cloudflare/util-markdown": "^1.2.1", | ||
"@types/lodash": "4.14.117", | ||
"lodash": "2.4.2", | ||
"lodash.set": "4.3.2", | ||
"node-polyglot": "^2.3.0", | ||
@@ -32,3 +33,3 @@ "pseudoloc": "^1.1.0" | ||
}, | ||
"gitHead": "c54d6a7d9b4b8f2512966f9bd6b11a6bf46e42f4" | ||
"gitHead": "2c7116804e9d7e6f2e005ee4d7aa37829d2624a3" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46104
21
899
6
+ Addedlodash.set@4.3.2
+ Addedlodash.set@4.3.2(transitive)