Javascript intl8n flavour support

signup-info-prompt
kibigo! 2017-12-07 19:07:47 -08:00
parent 8394430081
commit b28cd6769c
42 changed files with 308 additions and 72 deletions

View File

@ -62,6 +62,7 @@ class ApplicationController < ActionController::Base
pack: pack_name, pack: pack_name,
preload: nil, preload: nil,
skin: nil, skin: nil,
supported_locales: data['locales'],
} }
if data['pack'][pack_name].is_a?(Hash) if data['pack'][pack_name].is_a?(Hash)
pack_data[:common] = nil if data['pack'][pack_name]['use_common'] == false pack_data[:common] = nil if data['pack'][pack_name]['use_common'] == false
@ -93,6 +94,7 @@ class ApplicationController < ActionController::Base
pack: nil, pack: nil,
preload: nil, preload: nil,
skin: nil, skin: nil,
supported_locales: data['locales'],
} }
end end

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/ar.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/bg.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/ca.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/de.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -1,4 +1,6 @@
{ import inherited from 'mastodon/locales/en.json';
const messages = {
"getting_started.open_source_notice": "Glitchsoc is free open source software forked from {Mastodon}. You can contribute or report issues on GitHub at {github}.", "getting_started.open_source_notice": "Glitchsoc is free open source software forked from {Mastodon}. You can contribute or report issues on GitHub at {github}.",
"layout.auto": "Auto", "layout.auto": "Auto",
"layout.current_is": "Your current layout is:", "layout.current_is": "Your current layout is:",
@ -42,5 +44,7 @@
"notification_purge.btn_all": "Select\nall", "notification_purge.btn_all": "Select\nall",
"notification_purge.btn_none": "Select\nnone", "notification_purge.btn_none": "Select\nnone",
"notification_purge.btn_invert": "Invert\nselection", "notification_purge.btn_invert": "Invert\nselection",
"notification_purge.btn_apply": "Clear\nselected" "notification_purge.btn_apply": "Clear\nselected",
} };
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/eo.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/es.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/fa.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/fi.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/fr.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/he.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/hr.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/hu.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/id.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/io.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/it.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/ja.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/ko.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/nl.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/no.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/oc.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -1,4 +1,6 @@
{ import inherited from 'mastodon/locales/pl.json';
const messages = {
"getting_started.open_source_notice": "Glitchsoc jest wolnym i otwartoźródłowym forkiem oprogramowania {Mastodon}. Możesz współtworzyć projekt lub zgłaszać błędy na GitHubie pod adresem {github}.", "getting_started.open_source_notice": "Glitchsoc jest wolnym i otwartoźródłowym forkiem oprogramowania {Mastodon}. Możesz współtworzyć projekt lub zgłaszać błędy na GitHubie pod adresem {github}.",
"layout.auto": "Automatyczny", "layout.auto": "Automatyczny",
"layout.current_is": "Twój obecny układ to:", "layout.current_is": "Twój obecny układ to:",
@ -41,4 +43,6 @@
"notification_purge.btn_none": "Odznacz\nwszystkie", "notification_purge.btn_none": "Odznacz\nwszystkie",
"notification_purge.btn_invert": "Odwróć\nzaznaczenie", "notification_purge.btn_invert": "Odwróć\nzaznaczenie",
"notification_purge.btn_apply": "Usuń\nzaznaczone" "notification_purge.btn_apply": "Usuń\nzaznaczone"
} };
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/pt-BR.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/pt.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/ru.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/sv.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/th.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/tr.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/uk.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/zh-CN.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/zh-HK.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -0,0 +1,7 @@
import inherited from 'mastodon/locales/zh-TW.json';
const messages = {
// No translations available.
};
export default Object.assign({}, inherited, messages);

View File

@ -20,6 +20,12 @@ pack:
settings: settings:
share: packs/share.js share: packs/share.js
# (OPTIONAL) The directory which contains localization files for
# the flavour, relative to this directory. The contents of this
# directory must be `.js` or `.json` files whose names correspond to
# language tags and whose default exports are a messages object.
locales: locales
# (OPTIONAL) The directory which contains the pack files. # (OPTIONAL) The directory which contains the pack files.
# Defaults to the theme directory (`app/javascript/themes/[theme]`), # Defaults to the theme directory (`app/javascript/themes/[theme]`),
# which should be sufficient for like 99% of use-cases lol. # which should be sufficient for like 99% of use-cases lol.

View File

@ -20,13 +20,17 @@ pack:
settings: settings:
share: share.js share: share.js
# (OPTIONAL) The directory which contains localization files for
# the flavour, relative to this directory.
locales: ../../mastodon/locales
# (OPTIONAL) The directory which contains the pack files. # (OPTIONAL) The directory which contains the pack files.
# Defaults to the theme directory (`app/javascript/themes/[theme]`), # Defaults to this directory (`app/javascript/flavour/[flavour]`),
# but in the case of the vanilla Mastodon theme the pack files are # but in the case of the vanilla Mastodon flavour the pack files are
# somewhere else. # somewhere else.
pack_directory: app/javascript/packs pack_directory: app/javascript/packs
# (OPTIONAL) By default the theme will fallback to the default theme # (OPTIONAL) By default the theme will fallback to the default flavour
# if a particular pack is not provided. You can specify different # if a particular pack is not provided. You can specify different
# fallbacks here, or disable fallback behaviours altogether by # fallbacks here, or disable fallback behaviours altogether by
# specifying a `null` value. # specifying a `null` value.

View File

@ -15,6 +15,14 @@ class Themes
Dir.glob(Rails.root.join('app', 'javascript', 'flavours', '*', 'theme.yml')) do |path| Dir.glob(Rails.root.join('app', 'javascript', 'flavours', '*', 'theme.yml')) do |path|
data = YAML.load_file(path) data = YAML.load_file(path)
name = File.basename(File.dirname(path)) name = File.basename(File.dirname(path))
if data['locales']
locales = []
Dir.glob(File.join(File.dirname(path), data['locales'], '*.{js,json}')) do |locale|
localeName = File.basename(locale, File.extname(locale))
locales.push(localeName) unless localeName.match(/defaultMessages|whitelist|index/)
end
data['locales'] = locales
end
if data['pack'] if data['pack']
data['name'] = name data['name'] = name
data['skin'] = { 'default' => [] } data['skin'] = { 'default' => [] }

View File

@ -19,7 +19,10 @@
= title = title
= javascript_pack_tag "locales", integrity: true, crossorigin: 'anonymous' = javascript_pack_tag "locales", integrity: true, crossorigin: 'anonymous'
= javascript_pack_tag "locale_#{I18n.locale}", integrity: true, crossorigin: 'anonymous' - if @theme[:supported_locales].include? I18n.locale.to_s
= javascript_pack_tag "locales/#{@theme[:flavour]}/#{I18n.locale}", integrity: true, crossorigin: 'anonymous'
- elsif @theme[:supported_locales].include? 'en'
= javascript_pack_tag "locales/#{@theme[:flavour]}/en", integrity: true, crossorigin: 'anonymous'
= csrf_meta_tags = csrf_meta_tags
= yield :header_tags = yield :header_tags

View File

@ -30,6 +30,9 @@ for (let i = 0; i < flavourFiles.length; i++) {
if (!data.pack_directory) { if (!data.pack_directory) {
data.pack_directory = dirname(flavourFile); data.pack_directory = dirname(flavourFile);
} }
if (data.locales) {
data.locales = join(dirname(flavourFile), data.locales);
}
if (data.pack && typeof data.pack === 'object') { if (data.pack && typeof data.pack === 'object') {
flavours[data.name] = data; flavours[data.name] = data;
} }
@ -45,7 +48,7 @@ for (let i = 0; i < skinFiles.length; i++) {
const data = flavours[name].skin; const data = flavours[name].skin;
if (lstatSync(skinFile).isDirectory()) { if (lstatSync(skinFile).isDirectory()) {
data[skin] = {}; data[skin] = {};
const skinPacks = glob.sync(resolve(skinFile, '*.{css,scss}')); const skinPacks = glob.sync(join(skinFile, '*.{css,scss}'));
for (let j = 0; j < skinPacks.length; j++) { for (let j = 0; j < skinPacks.length; j++) {
const pack = skinPacks[i]; const pack = skinPacks[i];
data[skin][basename(pack, extname(pack))] = pack; data[skin][basename(pack, extname(pack))] = pack;

View File

@ -1,70 +1,66 @@
// A message from upstream:
// ========================
// To avoid adding a lot of boilerplate, locale packs are // To avoid adding a lot of boilerplate, locale packs are
// automatically generated here. These are written into the tmp/ // automatically generated here. These are written into the tmp/
// directory and then used to generate locale_en.js, locale_fr.js, etc. // directory and then used to generate locale_en.js, locale_fr.js, etc.
const fs = require('fs'); // Glitch note:
const path = require('path'); // ============
// This code has been entirely rewritten to support glitch flavours.
// However, the underlying process is exactly the same.
const { existsSync, readdirSync, writeFileSync } = require('fs');
const { join, resolve } = require('path');
const rimraf = require('rimraf'); const rimraf = require('rimraf');
const mkdirp = require('mkdirp'); const mkdirp = require('mkdirp');
const { flavours } = require('./configuration.js');
const localesJsonPath = path.join(__dirname, '../../app/javascript/mastodon/locales'); module.exports = Object.keys(flavours).reduce(function (map, entry) {
const locales = fs.readdirSync(localesJsonPath).filter(filename => { const flavour = flavours[entry];
return /\.json$/.test(filename) && if (!flavour.locales) {
!/defaultMessages/.test(filename) && return map;
!/whitelist/.test(filename); }
}).map(filename => filename.replace(/\.json$/, '')); const locales = readdirSync(flavour.locales).filter(
filename => /\.js(?:on)?$/.test(filename) && !/defaultMessages|whitelist|index/.test(filename)
);
const outPath = resolve('tmp', 'locales', entry);
const outPath = path.join(__dirname, '../../tmp/packs'); rimraf.sync(outPath);
mkdirp.sync(outPath);
rimraf.sync(outPath); locales.forEach(function (locale) {
mkdirp.sync(outPath); const localeName = locale.replace(/\.js(?:on)?$/, '');
const localePath = join(outPath, `${localeName}.js`);
const outPaths = []; const baseLocale = localeName.split('-')[0]; // e.g. 'zh-TW' -> 'zh'
locales.forEach(locale => {
const localePath = path.join(outPath, `locale_${locale}.js`);
const baseLocale = locale.split('-')[0]; // e.g. 'zh-TW' -> 'zh'
const localeDataPath = [ const localeDataPath = [
// first try react-intl // first try react-intl
`../../node_modules/react-intl/locale-data/${baseLocale}.js`, `node_modules/react-intl/locale-data/${baseLocale}.js`,
// then check locales/locale-data // then check locales/locale-data
`../../app/javascript/mastodon/locales/locale-data/${baseLocale}.js`, `app/javascript/locales/locale-data/${baseLocale}.js`,
// fall back to English (this is what react-intl does anyway) // fall back to English (this is what react-intl does anyway)
'../../node_modules/react-intl/locale-data/en.js', 'node_modules/react-intl/locale-data/en.js',
].filter(filename => fs.existsSync(path.join(outPath, filename))) ].filter(
.map(filename => filename.replace(/..\/..\/node_modules\//, ''))[0]; filename => existsSync(filename)
).map(
let glitchInject = ` filename => filename.replace(/(?:node_modules|app\/javascript)\//, '')
const mergedMessages = messages; )[0];
`;
const glitchPath = `../../app/javascript/glitch/locales/${locale}.json`;
if (fs.existsSync(path.join(outPath, glitchPath))) {
glitchInject = `
import glitchMessages from ${JSON.stringify(glitchPath)};
let mergedMessages = messages;
Object.keys(glitchMessages).forEach(function (key) {
mergedMessages[key] = glitchMessages[key];
});
`;
}
const localeContent = `// const localeContent = `//
// locale_${locale}.js // locales/${entry}/${localeName}.js
// automatically generated by generateLocalePacks.js // automatically generated by generateLocalePacks.js
// //
import messages from '../../app/javascript/mastodon/locales/${locale}.json';
import localeData from ${JSON.stringify(localeDataPath)}; import messages from '../../../${flavour.locales}/${locale.replace(/\.js$/, '')}';
import localeData from '${localeDataPath}';
import { setLocale } from 'locales'; import { setLocale } from 'locales';
${glitchInject}
setLocale({messages: mergedMessages, localeData: localeData}); setLocale({
`; localeData,
fs.writeFileSync(localePath, localeContent, 'utf8'); messages,
outPaths.push(localePath);
}); });
`;
writeFileSync(localePath, localeContent, 'utf8');
map[`locales/${entry}/${localeName}`] = localePath;
});
module.exports = outPaths; return map;
}, {});

View File

@ -7,7 +7,7 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin'); const ManifestPlugin = require('webpack-manifest-plugin');
const extname = require('path-complete-extname'); const extname = require('path-complete-extname');
const { env, settings, core, flavours, output, loadersDir } = require('./configuration.js'); const { env, settings, core, flavours, output, loadersDir } = require('./configuration.js');
const localePackPaths = require('./generateLocalePacks'); const localePacks = require('./generateLocalePacks');
function reducePacks (data, into = {}) { function reducePacks (data, into = {}) {
if (!data.pack) { if (!data.pack) {
@ -48,11 +48,7 @@ function reducePacks (data, into = {}) {
module.exports = { module.exports = {
entry: Object.assign( entry: Object.assign(
{ locales: resolve('app', 'javascript', 'locales') }, { locales: resolve('app', 'javascript', 'locales') },
localePackPaths.reduce((map, entry) => { localePacks,
const localMap = map;
localMap[basename(entry, extname(entry, extname(entry)))] = resolve(entry);
return localMap;
}, {}),
reducePacks(core), reducePacks(core),
Object.keys(flavours).reduce((map, entry) => reducePacks(flavours[entry], map), {}) Object.keys(flavours).reduce((map, entry) => reducePacks(flavours[entry], map), {})
), ),