mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 23:57:22 +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: "" */
|
||||
};
|
||||
|
||||
/* 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;
|
||||
|
|
|
@ -12,12 +12,13 @@ const Container = require("./Container.js");
|
|||
const Doc = require("./Doc.js");
|
||||
const DocsSidebar = require("./DocsSidebar.js");
|
||||
const Site = require("./Site.js");
|
||||
const translation = require("../server/translation.js");
|
||||
|
||||
class DocsLayout extends React.Component {
|
||||
render() {
|
||||
const metadata = this.props.metadata;
|
||||
const content = this.props.children;
|
||||
const i18n = this.props.config[this.props.metadata.language];
|
||||
const i18n = translation[this.props.metadata.language];
|
||||
return (
|
||||
<Site
|
||||
config={this.props.config}
|
||||
|
@ -25,9 +26,9 @@ class DocsLayout extends React.Component {
|
|||
section="docs"
|
||||
title={
|
||||
i18n
|
||||
? this.props.config[this.props.metadata.language][
|
||||
"localized-strings"
|
||||
][this.props.metadata.localized_id] || this.props.metadata.title
|
||||
? translation[this.props.metadata.language]["localized-strings"][
|
||||
this.props.metadata.localized_id
|
||||
] || this.props.metadata.title
|
||||
: this.props.metadata.title
|
||||
}
|
||||
description={content.trim().split("\n")[0]}
|
||||
|
@ -42,7 +43,7 @@ class DocsLayout extends React.Component {
|
|||
source={metadata.source}
|
||||
title={
|
||||
i18n
|
||||
? this.props.config[this.props.metadata.language][
|
||||
? translation[this.props.metadata.language][
|
||||
"localized-strings"
|
||||
][this.props.metadata.localized_id] ||
|
||||
this.props.metadata.title
|
||||
|
@ -58,7 +59,7 @@ class DocsLayout extends React.Component {
|
|||
>
|
||||
←{" "}
|
||||
{i18n
|
||||
? this.props.config[this.props.metadata.language][
|
||||
? translation[this.props.metadata.language][
|
||||
"localized-strings"
|
||||
]["previous"] || "Previous"
|
||||
: "Previous"}
|
||||
|
@ -69,7 +70,7 @@ class DocsLayout extends React.Component {
|
|||
href={metadata.next_id + ".html#content"}
|
||||
>
|
||||
{i18n
|
||||
? this.props.config[this.props.metadata.language][
|
||||
? translation[this.props.metadata.language][
|
||||
"localized-strings"
|
||||
]["next"] || "Next"
|
||||
: "Next"}{" "}
|
||||
|
|
|
@ -11,6 +11,7 @@ const React = require("react");
|
|||
const HeaderNav = require("./nav/HeaderNav.js");
|
||||
const Head = require("./Head.js");
|
||||
const Footer = require(process.cwd() + "/core/Footer.js");
|
||||
const translation = require("../server/translation.js");
|
||||
|
||||
class Site extends React.Component {
|
||||
/*
|
||||
|
@ -48,8 +49,8 @@ class Site extends React.Component {
|
|||
*/
|
||||
|
||||
render() {
|
||||
const tagline = this.props.config[this.props.language]
|
||||
? this.props.config[this.props.language]["localized-strings"].tagline
|
||||
const tagline = translation[this.props.language]
|
||||
? translation[this.props.language]["localized-strings"].tagline
|
||||
: this.props.config.tagline;
|
||||
const title = this.props.title
|
||||
? this.props.title + " · " + this.props.config.title
|
||||
|
|
|
@ -10,13 +10,14 @@
|
|||
const React = require("react");
|
||||
|
||||
const siteConfig = require(process.cwd() + "/siteConfig.js");
|
||||
const translation = require("../../server/translation.js");
|
||||
|
||||
class LanguageDropDown extends React.Component {
|
||||
render() {
|
||||
const enabledLanguages = [];
|
||||
let currentLanguage = "English";
|
||||
|
||||
siteConfig["languages"].map(lang => {
|
||||
translation["languages"].map(lang => {
|
||||
if (lang.tag == this.props.language) {
|
||||
currentLanguage = lang.name;
|
||||
}
|
||||
|
@ -97,8 +98,8 @@ class HeaderNav extends React.Component {
|
|||
href={linkWithLang}
|
||||
className={link.section === this.props.section ? "active" : ""}
|
||||
>
|
||||
{siteConfig[this.props.language]
|
||||
? siteConfig[this.props.language]["localized-strings"][link.text]
|
||||
{translation[this.props.language]
|
||||
? translation[this.props.language]["localized-strings"][link.text]
|
||||
: link.text}
|
||||
</a>
|
||||
</li>
|
||||
|
|
|
@ -11,6 +11,7 @@ const React = require("react");
|
|||
const classNames = require("classnames");
|
||||
|
||||
const siteConfig = require(process.cwd() + "/siteConfig.js");
|
||||
const translation = require("../../server/translation.js");
|
||||
|
||||
class SideNav extends React.Component {
|
||||
render() {
|
||||
|
@ -61,15 +62,15 @@ class SideNav extends React.Component {
|
|||
);
|
||||
}
|
||||
getLocalizedCategoryString(category) {
|
||||
let categoryString = siteConfig[this.props.language]
|
||||
? siteConfig[this.props.language]["localized-strings"][category] ||
|
||||
let categoryString = translation[this.props.language]
|
||||
? translation[this.props.language]["localized-strings"][category] ||
|
||||
category
|
||||
: category;
|
||||
return categoryString;
|
||||
}
|
||||
getLocalizedString(metadata) {
|
||||
let localizedString = siteConfig[this.props.language]
|
||||
? siteConfig[this.props.language]["localized-strings"][
|
||||
let localizedString = translation[this.props.language]
|
||||
? translation[this.props.language]["localized-strings"][
|
||||
metadata.localized_id
|
||||
] || metadata.title
|
||||
: metadata.title;
|
||||
|
|
|
@ -8,9 +8,6 @@
|
|||
*/
|
||||
|
||||
function execute() {
|
||||
const translation = require("./translation.js");
|
||||
translation();
|
||||
|
||||
const CWD = process.cwd();
|
||||
const fs = require("fs-extra");
|
||||
const readMetadata = require("./readMetadata.js");
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
function execute(port) {
|
||||
const translation = require("./translation.js");
|
||||
translation();
|
||||
const CWD = process.cwd();
|
||||
const express = require("express");
|
||||
const React = require("react");
|
||||
|
@ -282,8 +281,8 @@ function execute(port) {
|
|||
const match = regexLang.exec(req.path);
|
||||
const parts = match[1].split("/");
|
||||
const enabledLangTags = [];
|
||||
for (let i = 0; i < siteConfig["languages"].length; i++) {
|
||||
enabledLangTags.push(siteConfig["languages"][i].tag);
|
||||
for (let i = 0; i < translation["languages"].length; i++) {
|
||||
enabledLangTags.push(translation["languages"][i].tag);
|
||||
}
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
if (enabledLangTags.indexOf(parts[i]) !== -1) {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
* 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";
|
||||
|
||||
|
@ -16,7 +16,7 @@ function setLanguage(lang) {
|
|||
}
|
||||
|
||||
function translate(str) {
|
||||
return siteConfig[language]["pages-strings"][str];
|
||||
return translation[language]["pages-strings"][str];
|
||||
}
|
||||
|
||||
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 fs = require("fs-extra");
|
||||
const fs = require("fs");
|
||||
const glob = require("glob");
|
||||
const path = require("path");
|
||||
const mkdirp = require("mkdirp");
|
||||
|
||||
console.log("translation.js triggered...");
|
||||
|
||||
function writeFileAndCreateFolder(file, content) {
|
||||
mkdirp.sync(file.replace(new RegExp("/[^/]*$"), ""));
|
||||
fs.writeFileSync(file, content);
|
||||
}
|
||||
|
||||
function execute() {
|
||||
if (fs.existsSync(CWD + "/languages.js")) {
|
||||
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 languages;
|
||||
if (fs.existsSync(CWD + "/languages.js")) {
|
||||
languages = require(CWD + "/languages.js");
|
||||
} else {
|
||||
languages = [
|
||||
{
|
||||
enabled: true,
|
||||
name: "English",
|
||||
tag: "en"
|
||||
}
|
||||
});
|
||||
|
||||
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