mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-12 07:42:34 +02:00
Fix translation functionality to properly condense whitespace and parse escaped characters (#17)
This commit is contained in:
parent
70ed75b046
commit
523d824651
3 changed files with 18 additions and 3 deletions
|
@ -19,7 +19,7 @@ module.exports = function translatePlugin(babel) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* assume translate element only has one child which is the text */
|
/* 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";
|
let description = "no description given";
|
||||||
const attributes = path.node.openingElement.attributes;
|
const attributes = path.node.openingElement.attributes;
|
||||||
for (let i = 0; i < attributes.length; i++) {
|
for (let i = 0; i < attributes.length; i++) {
|
||||||
|
|
|
@ -11,12 +11,25 @@ const translation = require("./translation.js");
|
||||||
|
|
||||||
let language = "en";
|
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) {
|
function setLanguage(lang) {
|
||||||
language = lang;
|
language = lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
function translate(str) {
|
function translate(str) {
|
||||||
return translation[language]["pages-strings"][str];
|
return parseEscapeSequences(translation[language]["pages-strings"][str]);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -73,7 +73,9 @@ function execute() {
|
||||||
path.node.type === "JSXElement" &&
|
path.node.type === "JSXElement" &&
|
||||||
path.node.openingElement.name.name === "translate"
|
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";
|
let description = "no description given";
|
||||||
const attributes = path.node.openingElement.attributes;
|
const attributes = path.node.openingElement.attributes;
|
||||||
for (let i = 0; i < attributes.length; i++) {
|
for (let i = 0; i < attributes.length; i++) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue