░▒▓████████████████████████████████▓▒░ ░▒▓█ ▓▒░ ░▒▓█ ~ S W A M P ~ ▓▒░ ░▒▓█ ▓▒░ ░▒▓████████████████████████████████▓▒░
pplmvsvm
LOCATION:
/var/www/canvas/packages/canvas-media/scripts
☗ ROOT
↻ REFRESH
✎ CARVE FLESH
EDITING: installTranslations.js
#!/usr/bin/env node /* * Copyright (C) 2021 - present Instructure, Inc. * * This file is part of Canvas. * * Canvas is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the Free * Software Foundation, version 3 of the License. * * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License along * with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * ./src/getTranslations.js is generated which defines a function that * imports the right src/translations/locales file based on the given locale. This * has the effect of webpack splitting each locales/*.js file into its own bundle. * * The output from this script gets checked in. */ /* eslint-disable import/no-extraneous-dependencies, no-console */ const fs = require('fs') const path = require('path') const getTranslationList = require('@instructure/translations/bin/get-translation-list') const readTranslationFile = require('@instructure/translations/bin/read-translation-file') const PACKAGE_NAME = 'canvas-media' // Here we go. installTranslations() .then(() => { console.log('Translations installed.') process.exit(0) }) .catch(err => { console.error('Failed installing translations', err) process.exit(1) }) // The driver that does all the work. async function installTranslations() { const canvasTranslations = await getTranslationList(PACKAGE_NAME) const canvasLocaleFileBasenames = canvasTranslations.map(t => t.replace('.json', '')) // We copy the translations over for users of our npm package. copyCanvasTranslations(canvasLocaleFileBasenames) generateGetTranslations(canvasLocaleFileBasenames.sort()) } // there's one file in src/translations/locales for each canvas locale function copyCanvasTranslations(canvasLocaleFileBasenames) { removeStaleTranslationFiles(canvasLocaleFileBasenames) for (const basename of canvasLocaleFileBasenames) { const filepath = path.resolve( __dirname, path.join('../src/translations/locales', `${basename}.js`) ) const content = localeFileContent(basename) fs.writeFileSync(filepath, content, {flag: 'w'}) } } // if there are any existing src/translations/locales files // for locales no longer in the new list, remove them function removeStaleTranslationFiles(locales) { const newLocalesFiles = locales.map(l => `${l}.js`) const curLocalesFiles = fs.readdirSync(path.resolve(__dirname, `../src/translations/locales`)) const staleFiles = curLocalesFiles.filter(f => !newLocalesFiles.includes(f)) for (const f of staleFiles) { fs.rmSync(path.resolve(__dirname, path.join('../src/translations/locales/', f))) } } const copyright = `/* * Copyright (C) 2021 - present Instructure, Inc. * * This file is part of Canvas. * * Canvas is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the Free * Software Foundation, version 3 of the License. * * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License along * with this program. If not, see <http://www.gnu.org/licenses/>. */ ` // the content of the file being generated in copyCanvasTranslations function localeFileContent(canvasLocaleFileBasename) { // some translation files have '_', but no canvas locale does, // they all use '-'. let canvasLocale = canvasLocaleFileBasename.replace(/_/g, '-') const localeData = readTranslationFile(PACKAGE_NAME, canvasLocaleFileBasename) const preface = `${copyright} import formatMessage from '../../format-message' ` const localeDef = ` const locale = ${localeData} ` if (/-/.test(canvasLocale)) { canvasLocale = `'${canvasLocale}'` } const trailer = ` formatMessage.addLocale({${canvasLocale}: locale}) ` return `${preface}${localeDef}${trailer}` } // generate the getTranslations() function // that serves to code-split the translations // into their own webpack bundle then provide // them to the RCE function generateGetTranslations(localeFileBasenames) { const preface = `${copyright} /* * ******************************************************** * This file is generated by scripts/installTranslations.js * as part of the build. DO NOT EDIT * ******************************************************** */ export default function getTranslations(locale) { let p switch (locale) { ` const cases = [] for (const locale of localeFileBasenames) { cases.push(` case '${locale.replace(/_/g, '-')}': p = import('./translations/locales/${locale}') break`) } const trailer = ` default: p = Promise.resolve(null) } return p } ` const getlocalelist = ` export function getLocaleList() { return [ '${localeFileBasenames.map(l => l.replace(/_/g, '-')).join("',\n '")}', ] } ` const content = `${preface}${cases.join('\n')}${trailer}${getlocalelist}` fs.writeFileSync('./src/getTranslations.js', content, {flag: 'w'}) }
CANCEL
Name
Type
Size
Modified
Actions
↩ ..
DIR
—
—
📄 commitTranslations.sh
SH
1.3 KB
2023-09-24 03:39
EDIT
📄 installTranslations.js
JS
5.4 KB
2023-09-24 03:36
EDIT