-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle translations in the webpack devel server
- Loading branch information
Showing
3 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
// Cockpit internally returns the "po.<LANG>.js" file content for the | ||
// "po.js" request, reimplement it with a simple redirection (the JS file | ||
// only exists in the webpack memory, we cannot read it from disk) | ||
// | ||
// This function processes the webpack HTTP request. | ||
// | ||
// @param req HTTP request | ||
// @param res HTTP response | ||
module.exports = function (req, res) { | ||
// the regexp was taken from the original Cockpit code :-) | ||
const language = req.headers.cookie.replace(/(?:(?:^|.*;\s*)CockpitLang\s*=\s*([^;]*).*$)|^.*$/, "$1") || ""; | ||
// the cookie uses "pt-br" format while the PO file is "pt_BR" :-/ | ||
let [lang, country] = language.split("-"); | ||
country = country.toUpperCase(); | ||
|
||
// first check the full locale ("pt_BR") PO file | ||
if (fs.existsSync(path.join(__dirname, "..", "..", "po", `${lang}_${country}.po`))) { | ||
res.redirect(`/po.${lang}_${country}.js`); | ||
} else { | ||
// then check the language part only ("pt") PO file | ||
if (fs.existsSync(path.join(__dirname, "..", "..", "po", `${lang}.po`))) { | ||
res.redirect(`/po.${lang}.js`); | ||
} else { | ||
if (lang !== "en") console.log(`translation "${language}" not found`); | ||
// Cockpit returns an empty script if the translation file is missing | ||
res.set("Content-Type", "application/javascript"); | ||
res.send(""); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters