Fix translation functionality to properly condense whitespace and parse escaped characters (#17)

This commit is contained in:
Frank Li 2017-07-16 20:34:26 -07:00 committed by Joel Marcey
parent 70ed75b046
commit 523d824651
3 changed files with 18 additions and 3 deletions

View file

@ -19,7 +19,7 @@ module.exports = function translatePlugin(babel) {
return;
}
/* assume translate element only has one child which is the text */
const text = path.node.children[0].value.trim();
const text = path.node.children[0].value.trim().replace(/\s+/g, " ");
let description = "no description given";
const attributes = path.node.openingElement.attributes;
for (let i = 0; i < attributes.length; i++) {

View file

@ -11,12 +11,25 @@ const translation = require("./translation.js");
let language = "en";
/* handle escaped characters that get converted into json strings */
function parseEscapeSequences(str) {
return str
.replace(new RegExp("\\\\n", "g"), "\n")
.replace(new RegExp("\\\\b", "g"), "\b")
.replace(new RegExp("\\\\f", "g"), "\f")
.replace(new RegExp("\\\\r", "g"), "\r")
.replace(new RegExp("\\\\t", "g"), "\t")
.replace(new RegExp("\\\\'", "g"), "'")
.replace(new RegExp('\\\\"', "g"), '"')
.replace(new RegExp("\\\\", "g"), "\\");
}
function setLanguage(lang) {
language = lang;
}
function translate(str) {
return translation[language]["pages-strings"][str];
return parseEscapeSequences(translation[language]["pages-strings"][str]);
}
module.exports = {

View file

@ -73,7 +73,9 @@ function execute() {
path.node.type === "JSXElement" &&
path.node.openingElement.name.name === "translate"
) {
const text = path.node.children[0].value.trim();
const text = path.node.children[0].value
.trim()
.replace(/\s+/g, " ");
let description = "no description given";
const attributes = path.node.openingElement.attributes;
for (let i = 0; i < attributes.length; i++) {