8000 Revert "[FEAT] templates support added" by ilsavin-fiverr · Pull Request #93 · fiverr/i18n.js · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Revert "[FEAT] templates support added" #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 56 additions & 39 deletions .circleci/config.yml
10000
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ defaults: &defaults
working_directory: ~/app
docker:
- image: circleci/node:12
auth:
username: $DOCKER_USER
password: $DOCKER_PASS

commands:
runtests:
description: "Checkout, install dependencies, and run tests"
steps:
- checkout
- run:
name: Install dependencies
command: npm i
- run:
name: Run tests
command: npm t

version: 2.1
jobs:
Expand All @@ -14,40 +23,53 @@ jobs:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run: npm i
- run:
name: Install dependencies
command: npm i
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- ./node_modules
- persist_to_workspace:
root: .
paths:
- ./node_modules
lint:
testnode6:
<<: *defaults
docker:
- image: circleci/node:6.11.5
steps:
- checkout
- attach_workspace:
at: .
- run: npm run lint -- --plugin log
test:
parameters:
image:
description: "node version"
default: "cimg/node:lts"
type: string
- runtests
testnode8:
<<: *defaults
docker:
- image: circleci/node:8
steps:
- runtests
testnode10:
<<: *defaults
docker:
- image: "<<parameters.image>>"
auth:
username: $DOCKER_USER
password: $DOCKER_PASS
- image: circleci/node:10
steps:
- runtests
test:
<<: *defaults
steps:
- checkout
- run: npm i
- run: npm t
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: Run tests
command: npm t
lint:
<<: *defaults
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: Check syntax
command: npm run lint -- --plugin log
publish:
<<: *defaults
docker:
- image: circleci/node:8
steps:
- checkout
- restore_cache:
Expand All @@ -74,29 +96,24 @@ workflows:
version: 2.1
ci-cd:
jobs:
- install:
context: org-global
- install
- test:
context: org-global
matrix:
parameters:
image:
- "circleci/node:6.11.5"
- "circleci/node:8"
- "circleci/node:10"
- "cimg/node:14.21"
- "cimg/node:16.20"
- "cimg/node:18.17"
- "cimg/node:20.5"
requires:
- install
- lint:
context: org-global
requires:
- install
- testnode6
- testnode8
- testnode10
- publish:
context: org-global
requires:
- test
- lint
- testnode6
- testnode8
- testnode10
- glossary:
context: org-global
requires:
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"parserOptions": {
"ecmaVersion": 9,
"ecmaVersion": 6,
"sourceType": "script",
"ecmaFeatures": {
"impliedStrict": true
Expand Down
91 changes: 15 additions & 76 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
* @since 1.0.0
*/

const { get, omit } = require('lodash');
const get = require('lodash.get');
const paraphrase = require('paraphrase');
const assign = require('@recursive/assign');
const freeze = require('deep-freeze');
const _global = require('./utils/glob');
const getOneOther = require('./utils/get-one-other');
const jsonclone = require('./utils/jsonclone');
const { isTemplateInjectionEligible, injectTemplates } = require('./utils/templates');

const TRANSLATIONS = typeof Symbol === 'function' ? Symbol() : '_translations';
const MISSING = typeof Symbol === 'function' ? Symbol() : '_missing';
const EMPTY = typeof Symbol === 'function' ? Symbol() : '_empty';
const EMPTY_VALUES = [null, ''];
const ACCEPTABLE_RETURN_TYPES = ['object', 'number', 'boolean', 'string'];
const TEMPLATE_INJECTION_ERROR = typeof Symbol === 'function' ? Symbol() : '_template_injection_error';

const interpolate = paraphrase(/\${([^{}]*)}/g, /%{([^{}]*)}/g, /{{([^{}]*)}}/g);

Expand All @@ -28,25 +26,14 @@ const interpolate = paraphrase(/\${([^{}]*)}/g, /%{([^{}]*)}/g, /{{([^{}]*)}}/g)
* @param {String} [options.$scope] Root string to be use for looking for translation keys
* @param {Function} [options.missing] Method to call when key is not found
* @param {Function} [options.empty] Method to call when value is empty
* @param {Function} [options.templateInjectionError] Method to call when error with template
*/
class I18n {
constructor({
translations = {},
$scope,
missing,
empty,
templateInjectionError
} = { translations: {} }) {
constructor({ translations = {}, $scope, missing, empty } = { translations: {} }) {
this[TRANSLATIONS] = freeze(jsonclone(translations));
this[MISSING] = () => undefined;
this[EMPTY] = () => undefined;
this[TEMPLATE_INJECTION_ERROR] = () => undefined;

this.onmiss(missing);
this.onempty(empty);
this.onTemplateInjectionError(templateInjectionError);

this.$scope = $scope;

this.translate = this.translate.bind(this);
Expand All @@ -57,8 +44,8 @@ class I18n {
* @param {String} key
* @return {String} A default string for a missing key
*/
static getDefault(key = '') {
return key.split('.').pop().replace(/_/g, ' ');
static getDefault(key) {
return (key || '').split('.').pop().replace(/_/g, ' ');
}

/**
Expand Down Expand Up @@ -95,24 +82,22 @@ class I18n {
* @param {Object} [data] Interpolation data
* @return {String} translated and interpolated
*/
translate(key, data = {}) {
const { templates, templatesTransformer } = data;
const params = omit(data, ['templates', 'templatesTransformer']);
translate(key, data) {

const keys = Array.isArray(key) ? key : [ key ];

// Create key alternatives with prefixes
const alternatives = [].concat(
...keys.map(
(key) => this.alternatives(key, params)
(key) => this.alternatives(key, data)
)
);

// Find the first match
let result = this.find(...alternatives);

// Handle one,other translation structure
result = getOneOther(result, params);
result = getOneOther(result, data);

if (EMPTY_VALUES.includes(result)) {
return this[EMPTY](
Expand All @@ -121,40 +106,13 @@ class I18n {
}

const type = typeof result;
result = type === 'string' ? interpolate(result, params) : result;

if (ACCEPTABLE_RETURN_TYPES.includes(type)) {
if (isTemplateInjectionEligible(result)) {
return this.handleTemplateInjection(key, result, templates, templatesTransformer);
}

return result;
}

return this[MISSING](`${key}`, this.$scope, this.translations) || I18n.getDefault(...keys);
}
result = type === 'string' ? interpolate(result, data) : result;

/**
* Inject all templates into the origin translation.
*
* @param {String} key String representing dot notation
* @param {String} originTranslation The translation into which the templates will be injected.
* @param {Record.<String, Function>} templates The templates that will be injected.
* @param {Record.<String, Function>} templatesTransformer The templates transformer function
* @return {String} template injected translation
*/
handleTemplateInjection(key, originTranslation, templates, templatesTransformer) {
try {
return injectTemplates({
originTranslation,
templates,
templatesTransformer
});
} catch (error) {
return this[TEMPLATE_INJECTION_ERROR](
`${key}`, this.$scope, error.message
) || I18n.getDefault(key);
}
return ACCEPTABLE_RETURN_TYPES.includes(type)
? result
: this[MISSING](
`${key}`, this.$scope, this.translations
) || I18n.getDefault(...keys);
}

/**
Expand All @@ -180,8 +138,8 @@ class I18n {
* @param {object} data Object optionally containing '$scope' parameter
* @return {string[]}
*/
alternatives(key, data= {}) {
return [data, this].map(
alternatives(key, data) {
return [data || {}, this].map(
({ $scope }) => $scope
).filter(
Boolean
Expand Down Expand Up @@ -241,25 +199,6 @@ class I18n {
return this;
}

/**
* Register callback to be called when a template injection fails.
*
* Function accepts arguments: {String} missing key
* {String} translation scope
* {Object} The entire translation dictionary
* @param {Function} callback
* @return {self}
*
* @example
* i18n.onTemplateInjectionError((key, value) => logTemplateInjectionError({ key, value })
*/
onTemplateInjectionError(callback) {
if (typeof callback === 'function') {
this[TEMPLATE_INJECTION_ERROR] = callback;
}
return this;
}

/**
* Spawns a scoped child
* @param {String} scope Namespace
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fiverr/i18n",
"version": "1.9.0",
"version": "1.9.1",
"description": "Translation helper",
"keywords": [
"i18n",
Expand All @@ -25,7 +25,7 @@
"dependencies": {
"@recursive/assign": "^2.0.2",
"deep-freeze": "0.0.1",
"lodash": "^4.17.21",
"lodash.get": "^4.4.2",
"paraphrase": "^1.7.3"
},
"devDependencies": {
Expand Down
Loading
0