mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
Move references to languages and translated strings out of siteConfig.js into new translation.js module
This commit is contained in:
parent
48916d9f39
commit
099a13a264
9 changed files with 52 additions and 112 deletions
|
@ -76,28 +76,4 @@ const siteConfig = {
|
||||||
/* gaTrackingId: "" */
|
/* gaTrackingId: "" */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* DO NOT EDIT BELOW THIS LINE */
|
|
||||||
|
|
||||||
const fs = require("fs");
|
|
||||||
|
|
||||||
let languages;
|
|
||||||
if (fs.existsSync("./languages.js")) {
|
|
||||||
languages = require("./languages.js");
|
|
||||||
} else {
|
|
||||||
languages = [
|
|
||||||
{
|
|
||||||
enabled: true,
|
|
||||||
name: "English",
|
|
||||||
tag: "en"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
const enabledLanguages = languages.filter(lang => lang.enabled);
|
|
||||||
|
|
||||||
siteConfig["languages"] = enabledLanguages;
|
|
||||||
|
|
||||||
/* INJECT LOCALIZED FILES BEGIN */
|
|
||||||
/* INJECT LOCALIZED FILES END */
|
|
||||||
|
|
||||||
module.exports = siteConfig;
|
module.exports = siteConfig;
|
||||||
|
|
|
@ -12,12 +12,13 @@ const Container = require("./Container.js");
|
||||||
const Doc = require("./Doc.js");
|
const Doc = require("./Doc.js");
|
||||||
const DocsSidebar = require("./DocsSidebar.js");
|
const DocsSidebar = require("./DocsSidebar.js");
|
||||||
const Site = require("./Site.js");
|
const Site = require("./Site.js");
|
||||||
|
const translation = require("../server/translation.js");
|
||||||
|
|
||||||
class DocsLayout extends React.Component {
|
class DocsLayout extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
const metadata = this.props.metadata;
|
const metadata = this.props.metadata;
|
||||||
const content = this.props.children;
|
const content = this.props.children;
|
||||||
const i18n = this.props.config[this.props.metadata.language];
|
const i18n = translation[this.props.metadata.language];
|
||||||
return (
|
return (
|
||||||
<Site
|
<Site
|
||||||
config={this.props.config}
|
config={this.props.config}
|
||||||
|
@ -25,9 +26,9 @@ class DocsLayout extends React.Component {
|
||||||
section="docs"
|
section="docs"
|
||||||
title={
|
title={
|
||||||
i18n
|
i18n
|
||||||
? this.props.config[this.props.metadata.language][
|
? translation[this.props.metadata.language]["localized-strings"][
|
||||||
"localized-strings"
|
this.props.metadata.localized_id
|
||||||
][this.props.metadata.localized_id] || this.props.metadata.title
|
] || this.props.metadata.title
|
||||||
: this.props.metadata.title
|
: this.props.metadata.title
|
||||||
}
|
}
|
||||||
description={content.trim().split("\n")[0]}
|
description={content.trim().split("\n")[0]}
|
||||||
|
@ -42,7 +43,7 @@ class DocsLayout extends React.Component {
|
||||||
source={metadata.source}
|
source={metadata.source}
|
||||||
title={
|
title={
|
||||||
i18n
|
i18n
|
||||||
? this.props.config[this.props.metadata.language][
|
? translation[this.props.metadata.language][
|
||||||
"localized-strings"
|
"localized-strings"
|
||||||
][this.props.metadata.localized_id] ||
|
][this.props.metadata.localized_id] ||
|
||||||
this.props.metadata.title
|
this.props.metadata.title
|
||||||
|
@ -58,7 +59,7 @@ class DocsLayout extends React.Component {
|
||||||
>
|
>
|
||||||
←{" "}
|
←{" "}
|
||||||
{i18n
|
{i18n
|
||||||
? this.props.config[this.props.metadata.language][
|
? translation[this.props.metadata.language][
|
||||||
"localized-strings"
|
"localized-strings"
|
||||||
]["previous"] || "Previous"
|
]["previous"] || "Previous"
|
||||||
: "Previous"}
|
: "Previous"}
|
||||||
|
@ -69,7 +70,7 @@ class DocsLayout extends React.Component {
|
||||||
href={metadata.next_id + ".html#content"}
|
href={metadata.next_id + ".html#content"}
|
||||||
>
|
>
|
||||||
{i18n
|
{i18n
|
||||||
? this.props.config[this.props.metadata.language][
|
? translation[this.props.metadata.language][
|
||||||
"localized-strings"
|
"localized-strings"
|
||||||
]["next"] || "Next"
|
]["next"] || "Next"
|
||||||
: "Next"}{" "}
|
: "Next"}{" "}
|
||||||
|
|
|
@ -11,6 +11,7 @@ const React = require("react");
|
||||||
const HeaderNav = require("./nav/HeaderNav.js");
|
const HeaderNav = require("./nav/HeaderNav.js");
|
||||||
const Head = require("./Head.js");
|
const Head = require("./Head.js");
|
||||||
const Footer = require(process.cwd() + "/core/Footer.js");
|
const Footer = require(process.cwd() + "/core/Footer.js");
|
||||||
|
const translation = require("../server/translation.js");
|
||||||
|
|
||||||
class Site extends React.Component {
|
class Site extends React.Component {
|
||||||
/*
|
/*
|
||||||
|
@ -48,8 +49,8 @@ class Site extends React.Component {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const tagline = this.props.config[this.props.language]
|
const tagline = translation[this.props.language]
|
||||||
? this.props.config[this.props.language]["localized-strings"].tagline
|
? translation[this.props.language]["localized-strings"].tagline
|
||||||
: this.props.config.tagline;
|
: this.props.config.tagline;
|
||||||
const title = this.props.title
|
const title = this.props.title
|
||||||
? this.props.title + " · " + this.props.config.title
|
? this.props.title + " · " + this.props.config.title
|
||||||
|
|
|
@ -10,13 +10,14 @@
|
||||||
const React = require("react");
|
const React = require("react");
|
||||||
|
|
||||||
const siteConfig = require(process.cwd() + "/siteConfig.js");
|
const siteConfig = require(process.cwd() + "/siteConfig.js");
|
||||||
|
const translation = require("../../server/translation.js");
|
||||||
|
|
||||||
class LanguageDropDown extends React.Component {
|
class LanguageDropDown extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
const enabledLanguages = [];
|
const enabledLanguages = [];
|
||||||
let currentLanguage = "English";
|
let currentLanguage = "English";
|
||||||
|
|
||||||
siteConfig["languages"].map(lang => {
|
translation["languages"].map(lang => {
|
||||||
if (lang.tag == this.props.language) {
|
if (lang.tag == this.props.language) {
|
||||||
currentLanguage = lang.name;
|
currentLanguage = lang.name;
|
||||||
}
|
}
|
||||||
|
@ -97,8 +98,8 @@ class HeaderNav extends React.Component {
|
||||||
href={linkWithLang}
|
href={linkWithLang}
|
||||||
className={link.section === this.props.section ? "active" : ""}
|
className={link.section === this.props.section ? "active" : ""}
|
||||||
>
|
>
|
||||||
{siteConfig[this.props.language]
|
{translation[this.props.language]
|
||||||
? siteConfig[this.props.language]["localized-strings"][link.text]
|
? translation[this.props.language]["localized-strings"][link.text]
|
||||||
: link.text}
|
: link.text}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -11,6 +11,7 @@ const React = require("react");
|
||||||
const classNames = require("classnames");
|
const classNames = require("classnames");
|
||||||
|
|
||||||
const siteConfig = require(process.cwd() + "/siteConfig.js");
|
const siteConfig = require(process.cwd() + "/siteConfig.js");
|
||||||
|
const translation = require("../../server/translation.js");
|
||||||
|
|
||||||
class SideNav extends React.Component {
|
class SideNav extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
|
@ -61,15 +62,15 @@ class SideNav extends React.Component {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
getLocalizedCategoryString(category) {
|
getLocalizedCategoryString(category) {
|
||||||
let categoryString = siteConfig[this.props.language]
|
let categoryString = translation[this.props.language]
|
||||||
? siteConfig[this.props.language]["localized-strings"][category] ||
|
? translation[this.props.language]["localized-strings"][category] ||
|
||||||
category
|
category
|
||||||
: category;
|
: category;
|
||||||
return categoryString;
|
return categoryString;
|
||||||
}
|
}
|
||||||
getLocalizedString(metadata) {
|
getLocalizedString(metadata) {
|
||||||
let localizedString = siteConfig[this.props.language]
|
let localizedString = translation[this.props.language]
|
||||||
? siteConfig[this.props.language]["localized-strings"][
|
? translation[this.props.language]["localized-strings"][
|
||||||
metadata.localized_id
|
metadata.localized_id
|
||||||
] || metadata.title
|
] || metadata.title
|
||||||
: metadata.title;
|
: metadata.title;
|
||||||
|
|
|
@ -8,9 +8,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function execute() {
|
function execute() {
|
||||||
const translation = require("./translation.js");
|
|
||||||
translation();
|
|
||||||
|
|
||||||
const CWD = process.cwd();
|
const CWD = process.cwd();
|
||||||
const fs = require("fs-extra");
|
const fs = require("fs-extra");
|
||||||
const readMetadata = require("./readMetadata.js");
|
const readMetadata = require("./readMetadata.js");
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
|
|
||||||
function execute(port) {
|
function execute(port) {
|
||||||
const translation = require("./translation.js");
|
const translation = require("./translation.js");
|
||||||
translation();
|
|
||||||
const CWD = process.cwd();
|
const CWD = process.cwd();
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const React = require("react");
|
const React = require("react");
|
||||||
|
@ -282,8 +281,8 @@ function execute(port) {
|
||||||
const match = regexLang.exec(req.path);
|
const match = regexLang.exec(req.path);
|
||||||
const parts = match[1].split("/");
|
const parts = match[1].split("/");
|
||||||
const enabledLangTags = [];
|
const enabledLangTags = [];
|
||||||
for (let i = 0; i < siteConfig["languages"].length; i++) {
|
for (let i = 0; i < translation["languages"].length; i++) {
|
||||||
enabledLangTags.push(siteConfig["languages"][i].tag);
|
enabledLangTags.push(translation["languages"][i].tag);
|
||||||
}
|
}
|
||||||
for (let i = 0; i < parts.length; i++) {
|
for (let i = 0; i < parts.length; i++) {
|
||||||
if (enabledLangTags.indexOf(parts[i]) !== -1) {
|
if (enabledLangTags.indexOf(parts[i]) !== -1) {
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const siteConfig = require(process.cwd() + "/siteConfig.js");
|
const translation = require("./translation.js");
|
||||||
|
|
||||||
let language = "en";
|
let language = "en";
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ function setLanguage(lang) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function translate(str) {
|
function translate(str) {
|
||||||
return siteConfig[language]["pages-strings"][str];
|
return translation[language]["pages-strings"][str];
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -1,71 +1,35 @@
|
||||||
/**
|
|
||||||
* Copyright (c) 2017-present, Facebook, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the BSD-style license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree. An additional grant
|
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const CWD = process.cwd();
|
const CWD = process.cwd();
|
||||||
const fs = require("fs-extra");
|
const fs = require("fs");
|
||||||
const glob = require("glob");
|
const glob = require("glob");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const mkdirp = require("mkdirp");
|
|
||||||
|
|
||||||
console.log("translation.js triggered...");
|
let languages;
|
||||||
|
if (fs.existsSync(CWD + "/languages.js")) {
|
||||||
function writeFileAndCreateFolder(file, content) {
|
languages = require(CWD + "/languages.js");
|
||||||
mkdirp.sync(file.replace(new RegExp("/[^/]*$"), ""));
|
} else {
|
||||||
fs.writeFileSync(file, content);
|
languages = [
|
||||||
}
|
{
|
||||||
|
enabled: true,
|
||||||
function execute() {
|
name: "English",
|
||||||
if (fs.existsSync(CWD + "/languages.js")) {
|
tag: "en"
|
||||||
injectContent();
|
|
||||||
} else {
|
|
||||||
console.log("No languages besides English enabled");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function injectContent() {
|
|
||||||
const I18N_JSON_DIR = CWD + "/i18n/";
|
|
||||||
|
|
||||||
const files = glob.sync(I18N_JSON_DIR + "**");
|
|
||||||
const languages = [];
|
|
||||||
const langRegex = /\/i18n\/(.*)\.json$/;
|
|
||||||
|
|
||||||
files.forEach(file => {
|
|
||||||
const extension = path.extname(file);
|
|
||||||
if (extension == ".json") {
|
|
||||||
const match = langRegex.exec(file);
|
|
||||||
const language = match[1];
|
|
||||||
languages.push(language);
|
|
||||||
}
|
}
|
||||||
});
|
];
|
||||||
|
|
||||||
let injectedContent = "";
|
|
||||||
languages.forEach(language => {
|
|
||||||
injectedContent +=
|
|
||||||
"\nsiteConfig['" +
|
|
||||||
language +
|
|
||||||
"'] = require('./i18n/" +
|
|
||||||
language +
|
|
||||||
".json');";
|
|
||||||
});
|
|
||||||
|
|
||||||
let siteConfigFile = fs.readFileSync(CWD + "/siteConfig.js", "utf8");
|
|
||||||
const injectStart = "/* INJECT LOCALIZED FILES BEGIN */";
|
|
||||||
const injectEnd = "/* INJECT LOCALIZED FILES END */";
|
|
||||||
siteConfigFile =
|
|
||||||
siteConfigFile.slice(
|
|
||||||
0,
|
|
||||||
siteConfigFile.indexOf(injectStart) + injectStart.length
|
|
||||||
) +
|
|
||||||
injectedContent +
|
|
||||||
"\n" +
|
|
||||||
siteConfigFile.slice(siteConfigFile.indexOf(injectEnd));
|
|
||||||
fs.writeFileSync(CWD + "/siteConfig.js", siteConfigFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = execute;
|
const enabledLanguages = languages.filter(lang => lang.enabled);
|
||||||
|
|
||||||
|
const translation = { languages: enabledLanguages };
|
||||||
|
|
||||||
|
const files = glob.sync(CWD + "/i18n/**");
|
||||||
|
const langRegex = /\/i18n\/(.*)\.json$/;
|
||||||
|
|
||||||
|
files.forEach(file => {
|
||||||
|
const extension = path.extname(file);
|
||||||
|
if (extension === ".json") {
|
||||||
|
const match = langRegex.exec(file);
|
||||||
|
const language = match[1];
|
||||||
|
translation[language] = require(file);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = translation;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue