Skip to content

Commit

Permalink
Merge pull request #10 from Drawbotics/feature/lokalise-format
Browse files Browse the repository at this point in the history
Feature/lokalise format
  • Loading branch information
nicmosc authored Feb 18, 2020
2 parents ad69ddc + 5a413f4 commit 12ad3ae
Show file tree
Hide file tree
Showing 26 changed files with 244 additions and 105 deletions.
34 changes: 31 additions & 3 deletions dist/utils/conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,42 @@ Object.defineProperty(exports, "__esModule", {
exports.jsonToYml = jsonToYml;
exports.ymlToJson = ymlToJson;

var _jsYaml = require('js-yaml');
var _jsYaml = require('@nicmosc/js-yaml');

var _jsYaml2 = _interopRequireDefault(_jsYaml);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function jsonToYml(json) {
return _jsYaml2.default.safeDump(json);
var pluralForms = ['zero', 'one', 'two', 'few', 'many', 'other'];

function lokaliseSort(a, b, object) {
var aValue = object[a];
var bValue = object[b];

if (pluralForms.includes(a) && pluralForms.includes(b)) {
// handle plural forms
return pluralForms.indexOf(a) - pluralForms.indexOf(b);
}
if (b.startsWith(a) && b.split(a)[0] === '' && b.split(a)[1].includes('_') && typeof aValue !== 'string' && !Object.keys(aValue).every(function (k) {
return pluralForms.includes(k);
})) {
// handle e.g. project vs project_item
return 1;
}
if (a.startsWith(b) && a.split(b)[0] === '' && a.split(b)[1].includes('_') && typeof bValue !== 'string' && typeof aValue !== 'string') {
// handle e.g. project vs project_item
return -1;
}
return a.localeCompare(b);
}

function jsonToYml(json, format) {
return _jsYaml2.default.dump(json, {
sortKeys: lokaliseSort,
lineWidth: -1,
// noCompatMode: true,
scalarQuoteStyle: format === undefined ? 'lokalise' : null
});
}

function ymlToJson(yml) {
Expand Down
4 changes: 2 additions & 2 deletions dist/utils/to-folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ function parseTranslation(json, rootFolder) {
var name = file.name.split('.')[0];
var content = json[name];
if (content) {
_fs2.default.writeFileSync(rootFolder + '/' + name + '.yml', (0, _conversion.jsonToYml)(content));
_fs2.default.writeFileSync(rootFolder + '/' + name + '.yml', (0, _conversion.jsonToYml)(content, null));
json = (0, _lodash.omit)(json, name);
}
});
}

// now replace the rest in the manifest
_fs2.default.writeFileSync(rootFolder + '/index.yml', (0, _conversion.jsonToYml)(json));
_fs2.default.writeFileSync(rootFolder + '/index.yml', (0, _conversion.jsonToYml)(json, null));

// add new contents of the yml (not parsed)
Object.assign(final, {
Expand Down
63 changes: 51 additions & 12 deletions examples/en.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
en:
meta:
meta_1: 'Our brand'
meta_2: 'Follow us'
'no': 'No'
option: Option
option_revenue:
detailed: 'Over an expected total of %{total}'
title: 'Revenue from optioned units'
password: 'Your password'
style_options_label: 'Great, let’s continue - Choose an option below'
style_options:
feelingLucky: 'Looking for ideas'
topics:
title: Some of the topics we cover
item_not_destroyed: Something
item_not_loaded: Something
item:
not_enough_credits: 'You don''t have enough credits to reopen the project.'
marketing:
feedback: 'Leave us some feedback!'
title: Marketing
feedback: Leave us some feedback!
user_action:
save: Save file
cancel: Cancel
tech:
code: This is the code we use
installation: How to install our software
meta:
meta_1: Our brand
meta_2: Follow us
username: Your username
password: Your password
welcome: Welcome aboard!
save: 'Save file'
plurals:
zero: Zero
one: One
many: Manies
project: Project
project_estate: Estate
project_item: 'Project item'
project_item_1: First
project_item_2: Second
technology:
code: 'This is the code we use'
installation: 'How to install our software'
production_tab:
description: 'Click on "Production" to see your items in more detail.'
production_table:
description: 'This list contains all the products you ordered for this project.'
extra_description: 'For each item you''ll be able to see its status, estimated delivery date, required actions etc.'
title: 'Production table'
units:
zero: ''
one: '%{count} unit'
many: ''
other: '%{count} units'
units_title: Units
title: 'Some of the topics we cover'
'true': 1
username: 'Your username'
welcome: 'Welcome aboard!'
with_apostrophe: 'Some text''s with stuff''s'
with_chars: '%{sdf} ${sdfsf} sdfsdf'
with_chars_2: '%{sdf}${sdfsf}'
with_other_chars: Some-thing
'yes': 'Yes'
16 changes: 15 additions & 1 deletion examples/src/index.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
meta:
meta_1: Our brand
meta_2: Follow us
username: Your username
password: Your password
username: Your username
welcome: Welcome aboard!
with_other_chars: Some-thing
with_apostrophe: Some text's with stuff's
with_chars: '%{sdf} ${sdfsf} sdfsdf'
with_chars_2: '%{sdf}${sdfsf}'
yes: Yes
no: No
option: Option
option_revenue:
detailed: 'Over an expected total of %{total}'
title: Revenue from optioned units
'true': 1
style_options_label: 'Great, let’s continue - Choose an option below'
style_options:
feelingLucky: 'Looking for ideas'
2 changes: 1 addition & 1 deletion examples/src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"topics": "{{topics}}"
}
}
13 changes: 13 additions & 0 deletions examples/src/topics/index.yml
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
title: Some of the topics we cover
plurals:
one: One
zero: Zero
many: Manies
project_item: Project item
project: Project
project_estate: Estate
project_item_1: First
project_item_2: Second
item_not_loaded: Something
item_not_destroyed: Something
item:
not_enough_credits: 'You don''t have enough credits to reopen the project.'
2 changes: 1 addition & 1 deletion examples/src/topics/manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"marketing": "{{marketing}}",
"technology": "{{tech}}"
}
}
4 changes: 2 additions & 2 deletions examples/src/topics/marketing/index.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
title: Marketing
feedback: Leave us some feedback!
title: Marketing
user_action:
save: Save file
cancel: Cancel
save: Save file
12 changes: 12 additions & 0 deletions examples/src/topics/tech/index.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
code: This is the code we use
installation: How to install our software
units:
zero: ''
one: '%{count} unit'
many: ''
other: '%{count} units'
units_title: Units
production_tab:
description: 'Click on "Production" to see your items in more detail.'
production_table:
description: 'This list contains all the products you ordered for this project.'
extra_description: 'For each item you''ll be able to see its status, estimated delivery date, required actions etc.'
title: 'Production table'
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "louki",
"version": "1.0.0",
"version": "2.0.0",
"description": "Locale Organization Utility to manage locales",
"main": "src/index.js",
"repository": "https://github.com/Drawbotics/louki.git",
Expand All @@ -25,12 +25,12 @@
]
},
"dependencies": {
"@nicmosc/js-yaml": "^0.1.11",
"babel-polyfill": "^6.23.0",
"commander": "^2.9.0",
"directory-tree": "^1.2.0",
"dotenv": "^4.0.0",
"fs": "^0.0.1-security",
"js-yaml": "^3.13.1",
"lodash": "^4.17.11",
"node-watch": "^0.5.5",
"rc": "^1.2.8",
Expand Down
39 changes: 36 additions & 3 deletions src/utils/conversion.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import yaml from 'js-yaml';
import yaml from '@nicmosc/js-yaml';


export function jsonToYml(json) {
return yaml.safeDump(json);
const pluralForms = ['zero', 'one', 'two', 'few', 'many', 'other'];


function lokaliseSort(a, b, object) {
const aValue = object[a];
const bValue = object[b];

if (pluralForms.includes(a) && pluralForms.includes(b)) { // handle plural forms
return pluralForms.indexOf(a) - pluralForms.indexOf(b);
}
if (b.startsWith(a)
&& b.split(a)[0] === ''
&& b.split(a)[1].includes('_')
&& typeof aValue !== 'string'
&& ! Object.keys(aValue).every((k) => pluralForms.includes(k))) { // handle e.g. project vs project_item
return 1;
}
if (a.startsWith(b)
&& a.split(b)[0] === ''
&& a.split(b)[1].includes('_')
&& (typeof bValue !== 'string'
&& typeof aValue !== 'string')) { // handle e.g. project vs project_item
return -1;
}
return a.localeCompare(b);
}


export function jsonToYml(json, format) {
return yaml.dump(json, {
sortKeys: lokaliseSort,
lineWidth: -1,
// noCompatMode: true,
scalarQuoteStyle: format === undefined ? 'lokalise' : null,
});
}


Expand Down
4 changes: 2 additions & 2 deletions src/utils/to-folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ function parseTranslation(json, rootFolder) {
const name = file.name.split('.')[0];
const content = json[name];
if (content) {
fs.writeFileSync(`${rootFolder}/${name}.yml`, jsonToYml(content));
fs.writeFileSync(`${rootFolder}/${name}.yml`, jsonToYml(content, null));
json = omit(json, name);
}
});
}

// now replace the rest in the manifest
fs.writeFileSync(`${rootFolder}/index.yml`, jsonToYml(json));
fs.writeFileSync(`${rootFolder}/index.yml`, jsonToYml(json, null));

// add new contents of the yml (not parsed)
Object.assign(final, {
Expand Down
12 changes: 6 additions & 6 deletions test/integration/louki.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ describe('commands', () => {
const rootFolder = path.resolve(__dirname, '../mock/config/full/sections');
const targetFolder = path.resolve(__dirname, '../mock/dist');
const targetFile = 'en-target';
const desiredFile = `${targetFolder}/en-desired.yml`;
let desiredTranslation;

describe('louki(build, root, target)', () => {
let desiredTranslation;
beforeEach(() => {
desiredTranslation = '';
});

before(() => {
desiredTranslation = fs.readFileSync(desiredFile, 'utf8');
});
describe('louki(build, root, target)', () => {
it('the build command should take the contents of the root folder and replace them in the dist', () => {
desiredTranslation = fs.readFileSync(`${targetFolder}/en-desired.yml`, 'utf8');
const targetTranslation = louki('build', rootFolder, targetFolder, targetFile);
expect(targetTranslation).to.equal(desiredTranslation);
});
Expand Down
2 changes: 1 addition & 1 deletion test/mock/config/full/sections/index.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
user: the user of life (shared between order and studio)
user: the user of life (shared between order and studio and this is a very long line that's being added blablablabla)
6 changes: 3 additions & 3 deletions test/mock/config/full/sections/order/estate/index.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
items: The items are great
buttons: Buttons
id: Identification
items: The items are great
products:
visit3d: 3D Visit
interior: Interior 3D
buttons: Buttons
visit3d: 3D Visit
2 changes: 1 addition & 1 deletion test/mock/config/full/sections/order/index.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
simple_key: Thefirst simple key
another_key: theo ther keeeey
simple_key: Thefirst simple key
2 changes: 1 addition & 1 deletion test/mock/config/full/sections/studio/index.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
zone: Zone
highlight_area: Highlighted area
user: null
zone: Zone
10 changes: 5 additions & 5 deletions test/mock/config/generated/sections/order/estate/index.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
items: The items are great
id: Identification
products:
visit3d: 3D Visit
interior: Interior 3D
buttons: Buttons
forever:
a_lot: null
id: Identification
items: The items are great
products:
interior: Interior 3D
visit3d: 3D Visit
2 changes: 1 addition & 1 deletion test/mock/config/generated/sections/order/index.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
another_key: theo ther e
an_extra_key: lalal the key
another_key: theo ther e
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
haha: fsdflkj
quick: 'Veloce %{cammina}'
service: 'Servizio %{ciao}'
haha: fsdflkj
Loading

0 comments on commit 12ad3ae

Please sign in to comment.