-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translate): fixed regarding reviewers notes
- Loading branch information
1 parent
fccafe2
commit 0a1f43d
Showing
8 changed files
with
242 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
class ValidatorSchemaTranslations { | ||
constructor() { | ||
this.trackedKeys = ['name', 'content', 'label', 'settings', 'options', 'group']; | ||
this.validationSchema = require('./schemaTranslations.json'); | ||
this.translations = {}; | ||
this.schemaKeys = []; | ||
this.translationsKeys = []; | ||
} | ||
|
||
/** | ||
* Set schema.json | ||
* @param {array} schema | ||
*/ | ||
setSchema(schema) { | ||
this.getTranslatableStrings(schema); | ||
} | ||
|
||
/** | ||
* Set schemaTranslations.json | ||
* @param {object} translations | ||
*/ | ||
setTranslations(translations) { | ||
this.translations = translations; | ||
|
||
this.getTranslationsKeys(); | ||
} | ||
|
||
/** | ||
* Set i18n key from a schema into keys array | ||
* @param {string} value | ||
*/ | ||
setSchemaKeys(value) { | ||
if (value && !this.schemaKeys.includes(value) && /^i18n\./.test(value)) { | ||
this.schemaKeys.push(value); | ||
} | ||
} | ||
|
||
/** | ||
* Get validation schema | ||
* @return {object} | ||
*/ | ||
getValidationSchema() { | ||
return this.validationSchema; | ||
} | ||
|
||
/** | ||
* Get translations | ||
* @return {array} | ||
*/ | ||
getTranslations() { | ||
return this.translations; | ||
} | ||
|
||
/** | ||
* Get i18n keys from translations | ||
* @return {array} | ||
*/ | ||
getTranslationsKeys() { | ||
this.translationsKeys = Object.keys(this.translations); | ||
} | ||
|
||
/** | ||
* Get i18n keys from schema | ||
* @return {array} | ||
*/ | ||
getSchemaKeys() { | ||
return this.schemaKeys; | ||
} | ||
|
||
/** | ||
* Get translatable strings | ||
* @param {array} schema | ||
*/ | ||
getTranslatableStrings(schema) { | ||
const context = this; | ||
|
||
schema.forEach(element => { | ||
Object.entries(element).forEach(function (item) { | ||
const key = item[0]; | ||
const value = item[1]; | ||
|
||
if (!context.trackedKeys.includes(key)) { | ||
return; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return context.getTranslatableStrings(value); | ||
} | ||
|
||
context.setSchemaKeys(value); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Find unused i18n keys in schemaTranslations | ||
* @return {array} | ||
*/ | ||
findUnusedKeys() { | ||
return this.translationsKeys.filter(key => !this.schemaKeys.includes(key)); | ||
} | ||
|
||
/** | ||
* Find missed i18n keys in schemaTranslations | ||
* @return {array} | ||
*/ | ||
findMissedKeys() { | ||
return this.schemaKeys.filter(key => !this.translationsKeys.includes(key)); | ||
} | ||
} | ||
|
||
module.exports = ValidatorSchemaTranslations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const Paths = require('path'); | ||
const Code = require('code'); | ||
const Lab = require('lab'); | ||
const lab = exports.lab = Lab.script(); | ||
const describe = lab.describe; | ||
const it = lab.it; | ||
const expect = Code.expect; | ||
|
||
const ValidatorSchemaTranslations = require('./schema-translations'); | ||
const schema = require(Paths.join(process.cwd(), 'test/_mocks/themes/valid/schema.json')); | ||
const schemaTranslations = require(Paths.join(process.cwd(), 'test/_mocks/themes/valid/schemaTranslations.json')); | ||
const validationsTranslations = require('./schemaTranslations'); | ||
|
||
const validatorSchemaTranslations = () => { | ||
return new ValidatorSchemaTranslations(); | ||
}; | ||
|
||
describe('ValidatorSchemaTranslations', () => { | ||
it('should return translations', function (done) { | ||
const instance = validatorSchemaTranslations(); | ||
|
||
instance.setTranslations(schemaTranslations); | ||
|
||
expect(instance.getTranslations()).equals(schemaTranslations); | ||
|
||
done(); | ||
}); | ||
|
||
it('should return translations keys', function (done) { | ||
const instance = validatorSchemaTranslations(); | ||
|
||
instance.setSchema(schema); | ||
|
||
expect(instance.getSchemaKeys()).equals(['i18n.Test']); | ||
|
||
done(); | ||
}); | ||
|
||
it('should return validation schema', function (done) { | ||
const instance = validatorSchemaTranslations(); | ||
|
||
expect(instance.getValidationSchema()).equals(validationsTranslations); | ||
|
||
done(); | ||
}); | ||
|
||
it('should return i18n keys array without duplicates', function (done) { | ||
const instance = validatorSchemaTranslations(); | ||
|
||
instance.setSchemaKeys('i18n.Global'); | ||
instance.setSchemaKeys('i18n.Global'); | ||
|
||
expect(instance.getSchemaKeys()).equals(['i18n.Global']); | ||
|
||
done(); | ||
}); | ||
|
||
it('should return empty i18n keys array if specify empty string', function (done) { | ||
const instance = validatorSchemaTranslations(); | ||
|
||
instance.setSchemaKeys(''); | ||
|
||
expect(instance.getSchemaKeys()).equals([]); | ||
|
||
done(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"$id": "http://json-schema.org/draft-04/schema#", | ||
"title": "Theme translations", | ||
"description": "Translations of strings in schema.json file of a theme", | ||
"type": "object", | ||
"patternProperties": { | ||
"^i18n.": { | ||
"type": "object", | ||
"properties": { | ||
"default": { | ||
"type": "string", | ||
"minLength": 1 | ||
} | ||
}, | ||
"patternProperties": { | ||
"^[a-z]{2}$": { | ||
"type": "string", | ||
"minLength": 1 | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": [ | ||
"default" | ||
] | ||
} | ||
}, | ||
"additionalProperties": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[ | ||
{ | ||
"name": "Test", | ||
"name": "i18n.Test", | ||
"settings": [ | ||
{ | ||
"type": "heading", | ||
|
Oops, something went wrong.