This repository has been archived by the owner on Apr 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
108 additions
and
80 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,13 @@ | ||
"use strict"; | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const template = require("../src/html/base-template"); | ||
|
||
const LOCALE_DATA_PATH = path.join(__dirname, "../node_modules/react-intl/lib/locale-data"); | ||
const MESSAGES_PATH = path.join(__dirname, "../l10n"); | ||
const DEFAULT_LOCALE = "en"; | ||
|
||
function readFileIfExists(filepath) { | ||
try { | ||
return fs.readFileSync(filepath, "utf8"); | ||
} catch (e) { | ||
if (e.code !== "ENOENT") { | ||
throw e; | ||
} else { | ||
return; | ||
} | ||
const template = require("../src/html/templates").base; | ||
|
||
process.stdout.write(template({ | ||
// todo: read from args | ||
locale: "en-US", | ||
title: "New Tab", | ||
paths: { | ||
css: "./main.css", | ||
js: "./main.js", | ||
localeData: "./locale-data.js" | ||
} | ||
} | ||
|
||
function readLocaleDataFile(locale, sync) { | ||
const text = readFileIfExists(path.join(LOCALE_DATA_PATH, locale + ".js")); | ||
return text ? text.replace("module.exports", "window.reactIntlLocaleData") : text; | ||
} | ||
|
||
function getMessages(locale) { | ||
// TODO - figure out how we handle these cases | ||
if (locale === "en") locale = "en-US"; | ||
const text = readFileIfExists(path.join(MESSAGES_PATH, locale + "/strings.json")); | ||
return text ? JSON.parse(text) : text; | ||
} | ||
|
||
const defaultLocaleData = readLocaleDataFile(DEFAULT_LOCALE); | ||
const defaultMessages = getMessages(DEFAULT_LOCALE); | ||
|
||
function generateHTML(locale) { | ||
locale = locale || DEFAULT_LOCALE; | ||
const baseLocale = locale.split("-")[0]; | ||
const localeData = readLocaleDataFile(locale) || readLocaleDataFile(baseLocale) || defaultLocaleData; | ||
const messages = getMessages(locale) || getMessages(baseLocale) || defaultMessages; | ||
return template({locale, messages, localeData}); | ||
} | ||
|
||
process.stdout.write(generateHTML(DEFAULT_LOCALE)); | ||
})); |
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,45 @@ | ||
"use strict"; | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const template = require("../src/html/templates").localeData; | ||
|
||
const LOCALE_DATA_PATH = path.join(__dirname, "../node_modules/react-intl/lib/locale-data"); | ||
const MESSAGES_PATH = path.join(__dirname, "../l10n"); | ||
const DEFAULT_LOCALE = "en"; | ||
|
||
function readFileIfExists(filepath) { | ||
try { | ||
return fs.readFileSync(filepath, "utf8"); | ||
} catch (e) { | ||
if (e.code !== "ENOENT") { | ||
throw e; | ||
} else { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
function readLocaleDataFile(locale, sync) { | ||
const text = readFileIfExists(path.join(LOCALE_DATA_PATH, locale + ".js")); | ||
return text ? text.replace("module.exports", "window.reactIntlLocaleData") : text; | ||
} | ||
|
||
function getMessages(locale) { | ||
// TODO - figure out how we handle these cases | ||
if (locale === "en") locale = "en-US"; | ||
const text = readFileIfExists(path.join(MESSAGES_PATH, locale + "/strings.json")); | ||
return text ? JSON.parse(text) : text; | ||
} | ||
|
||
const defaultLocaleData = readLocaleDataFile(DEFAULT_LOCALE); | ||
const defaultMessages = getMessages(DEFAULT_LOCALE); | ||
|
||
function generateFile(locale) { | ||
locale = locale || DEFAULT_LOCALE; | ||
const baseLocale = locale.split("-")[0]; | ||
const localeData = readLocaleDataFile(locale) || readLocaleDataFile(baseLocale) || defaultLocaleData; | ||
const messages = getMessages(locale) || getMessages(baseLocale) || defaultMessages; | ||
return template({locale, messages, localeData}); | ||
} | ||
|
||
process.stdout.write(generateFile(DEFAULT_LOCALE)); |
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 was deleted.
Oops, something went wrong.
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,38 @@ | ||
"use strict"; | ||
|
||
module.exports.base = function (options) { | ||
const locale = options.locale; | ||
const paths = options.paths; | ||
const title = options.title; | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="${locale}"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width user-scalable=no" /> | ||
<link rel="stylesheet" href="${paths.css}"> | ||
<title>${title}</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="${paths.localeData}"></script> | ||
<script src="${paths.js}"></script> | ||
</body> | ||
</html> | ||
`; | ||
}; | ||
|
||
module.exports.localeData = function (options) { | ||
const locale = options.locale; | ||
const messages = options.messages; | ||
const localeData = options.localeData; | ||
return ` | ||
// GENERATED FILE | ||
// These are our strings | ||
window.newTabLocalInfo = {locale: "${locale}", messages: ${JSON.stringify(messages)}}; | ||
// This is locale-specific code for react-intl | ||
${localeData} | ||
`; | ||
}; |
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