From 7bcd8f81eb6e6a5af99d6e0b573f4d0a4eb02be3 Mon Sep 17 00:00:00 2001 From: Zachary Dovel Date: Mon, 17 Sep 2018 07:46:03 -0700 Subject: [PATCH] Initial commit --- src/isLocalizationFunctionStart.js | 4 +++ src/parseLocalizationFunction.js | 46 +++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/isLocalizationFunctionStart.js b/src/isLocalizationFunctionStart.js index 2e2140f..3ef2e73 100644 --- a/src/isLocalizationFunctionStart.js +++ b/src/isLocalizationFunctionStart.js @@ -3,6 +3,10 @@ const translationStartPatterns = [ '__`', `__n(`, '__n`', + '__p(', + '__p`', + `__np(`, + '__np`', ]; const length = translationStartPatterns diff --git a/src/parseLocalizationFunction.js b/src/parseLocalizationFunction.js index 9c6e5c8..0d503d2 100644 --- a/src/parseLocalizationFunction.js +++ b/src/parseLocalizationFunction.js @@ -37,9 +37,21 @@ function continueUntilStackLengthIs(text, state, length) { return state; } +function readStringArgument({index, stack, lineNumber}) { + const contextStart = continueToQuoteStart(text, {index, stack, lineNumber}); + const contextEnd = continueUntilStackLengthIs(text, {...keyStart}, keyStart.stack.length - 1); + const context = text.substring(contextStart.index, contextEnd.index - 1); + + if (contextStart.index === contextEnd.index - 1) { + throw new SyntaxError('empty context key'); + } + + return [{index, stack, lineNumber}, text]; +} + /** * 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} stack The current code stack @@ -50,31 +62,43 @@ 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}; + let key; + let context; - if (keyStart.index === keyEnd.index - 1) { - throw new SyntaxError('empty localization key'); + if (particular) { + [state, context] = readStringArgument(state); + metadata.context = context; } + [state, key] = readStringArgument(state); + metadata.key = key; + const functionEnd = (keyEnd.stack[0] === '(') ? continueUntilStackLengthIs(text, {...keyEnd}, keyEnd.stack.length - 1) : keyEnd; + 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); }