Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkudo committed Sep 18, 2018
1 parent acd8955 commit 20a2241
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/isLocalizationFunctionStart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const translationStartPatterns = [
'__`',
`__n(`,
'__n`',
'__p(',
'__p`',
`__np(`,
'__np`',
];

const length = translationStartPatterns
Expand Down
49 changes: 36 additions & 13 deletions src/parseLocalizationFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,21 @@ function continueUntilStackLengthIs(text, state, length) {
return state;
}

function readStringArgument(text, {index, stack, lineNumber}, name) {
const start = continueToQuoteStart(text, {index, stack, lineNumber});
const end = continueUntilStackLengthIs(text, {...start}, start.stack.length - 1);
const stringArgument = text.substring(start.index, end.index - 1);

if (start.index === end.index - 1) {
throw new SyntaxError(`${name} string argument is empty`);
}

return [end, stringArgument];
}

/**
* Parses the information from a localization function, include the function string,
* the key, the line number.
* the key, the line number. Parses __, __n, __p, __np.
* @param {String} text - The text blob
* @param {Number} index - The offset on the text
* @param {Array<String>} stack The current code stack
Expand All @@ -50,31 +62,42 @@ function continueUntilStackLengthIs(text, state, length) {
*/
module.exports = function parseLocalizationFunction(text, {index, stack, lineNumber}) {
const functionStart = {index, stack, lineNumber};
let plural = false;
let particular = false;

index += 1;

if (text.charAt(index + 1) === 'n') {
plural = true;
index += 1;
}

if (text.charAt(index + 1) === '(') {
if (text.charAt(index + 1) === 'p') {
particular = true;
index += 1;
}

if (text.charAt(index + 1) === '(') {
index += 1;
}

const keyStart = continueToQuoteStart(text, {index, stack, lineNumber});
const keyEnd = continueUntilStackLengthIs(text, {...keyStart}, keyStart.stack.length - 1);
const metadata = {plural, particular};
let state = {index, stack, lineNumber};

if (keyStart.index === keyEnd.index - 1) {
throw new SyntaxError('empty localization key');
if (particular) {
let context;
[state, context] = readStringArgument(text, state, 'context');
metadata.context = context;
}

const functionEnd = (keyEnd.stack[0] === '(') ?
continueUntilStackLengthIs(text, {...keyEnd}, keyEnd.stack.length - 1) : keyEnd;
let key;
[state, key] = readStringArgument(text, state, 'key');
metadata.key = key;

const functionEnd = (state.stack[0] === '(') ?
continueUntilStackLengthIs(text, {...state}, state.stack.length - 1) : state;
const fn = text.substring(functionStart.index, functionEnd.index);
metadata.fn = fn;

return {
...functionEnd,
key: text.substring(keyStart.index, keyEnd.index - 1),
fn: text.substring(functionStart.index, functionEnd.index),
};
return Object.assign({}, functionEnd, metadata);
}
78 changes: 73 additions & 5 deletions src/readCharacter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ describe('plugins/readCharacter', () => {
localization: {
key: 'a',
fn: '__("a")',
plural: false,
particular: false,
}
}, {
index: 8,
Expand All @@ -229,6 +231,33 @@ describe('plugins/readCharacter', () => {
}]);
});

it('parses basic translation function with context', () => {
let state = {index: 0, stack: [], lineNumber: 0}
const text = '__p("a", "b")c';
const actual = [];

while ((state = readCharacter(text, state)) !== null) {
actual.push(state);
}

expect(actual).toEqual([{
index: 13,
stack: [],
lineNumber: 0,
localization: {
context: 'a',
key: 'b',
fn: '__p("a", "b")',
plural: false,
particular: true,
}
}, {
index: 14,
stack: [],
lineNumber: 0,
}]);
});

it('parses basic plural translation function', () => {
let state = {index: 0, stack: [], lineNumber: 0}
const text = '__n("%d cat", "%d cats", 1)b';
Expand All @@ -245,6 +274,8 @@ describe('plugins/readCharacter', () => {
localization: {
key: '%d cat',
fn: '__n("%d cat", "%d cats", 1)',
plural: true,
particular: false,
}
}, {
index: 28,
Expand All @@ -253,6 +284,33 @@ describe('plugins/readCharacter', () => {
}]);
});

it('parses basic plural translation function with context', () => {
let state = {index: 0, stack: [], lineNumber: 0}
const text = '__np("a", "%d cat", "%d cats", 1)b';
const actual = [];

while ((state = readCharacter(text, state)) !== null) {
actual.push(state);
}

expect(actual).toEqual([{
index: 33,
stack: [],
lineNumber: 0,
localization: {
key: '%d cat',
context: 'a',
fn: '__np("a", "%d cat", "%d cats", 1)',
plural: true,
particular: true,
}
}, {
index: 34,
stack: [],
lineNumber: 0,
}]);
});

describe('polymer-style template strings', () => {
it('parses basic translation function in [[]] interpolation string', () => {
let state = {index: 0, stack: [], lineNumber: 0}
Expand Down Expand Up @@ -285,7 +343,9 @@ describe('plugins/readCharacter', () => {
lineNumber: 0,
localization: {
"key": "a",
"fn": "__(\"a\")"
"fn": "__(\"a\")",
particular: false,
plural: false,
}
}, {
index: 12,
Expand Down Expand Up @@ -390,7 +450,9 @@ describe('plugins/readCharacter', () => {
lineNumber: 0,
localization: {
"key": "a",
"fn": "__(\"a\")"
"fn": "__(\"a\")",
particular: false,
plural: false,
}
}, {
index: 12,
Expand Down Expand Up @@ -495,7 +557,9 @@ describe('plugins/readCharacter', () => {
lineNumber: 0,
localization: {
"key": "a",
"fn": "__(\"a\")"
"fn": "__(\"a\")",
particular: false,
plural: false
}
}, {
index: 11,
Expand Down Expand Up @@ -579,7 +643,9 @@ describe('plugins/readCharacter', () => {
lineNumber: 0,
localization: {
"key": "a",
"fn": "__(\"a\")"
"fn": "__(\"a\")",
plural: false,
particular: false,
}
}, {
index: 13,
Expand Down Expand Up @@ -664,6 +730,8 @@ describe('plugins/readCharacter', () => {
localization: {
key: 'a',
fn: '__`a`',
particular: false,
plural: false,
}
}, {
index: 6,
Expand Down Expand Up @@ -764,7 +832,7 @@ describe('plugins/readCharacter', () => {
while ((state = readCharacter(text, state)) !== null) {
actual.push(state);
}
}).toThrow(new SyntaxError('empty localization key'));
}).toThrow(new SyntaxError('key string argument is empty'));

expect(actual).toEqual([]);

Expand Down
18 changes: 18 additions & 0 deletions src/readString.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,29 @@ describe('readString', () => {
});
});

describe('singular with context', () => {
it('create key and value when contains translation', () => {
expect(readString(`a __p('b', 'c') d`)).toEqual({c: {fn: `__p('b', 'c')`, lineNumber: 0, index: 15}});
});

it('adds nothing when no translation', () => {
expect(readString(`a c`)).toEqual({});
});
});

describe('plural', () => {
it('create key and value when contains plural translation', () => {
expect(readString("a __n('%d cat', '%d cats', 1) c")).toEqual({
'%d cat': {fn: "__n('%d cat', '%d cats', 1)", lineNumber: 0, index: 29}
});
});
});

describe('plural with context', () => {
it('create key and value when contains plural translation', () => {
expect(readString("a __np('a', '%d cat', '%d cats', 1) c")).toEqual({
'%d cat': {fn: "__np('a', '%d cat', '%d cats', 1)", lineNumber: 0, index: 35}
});
});
});
});

0 comments on commit 20a2241

Please sign in to comment.