canvas-lms/packages/babel-preset-pretranslated-.../index.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.6 KiB
JavaScript
Raw Normal View History

Use new translation package for packages This makes it so canvas-rce and canvas-planner are using the new translations build rather than relying on their own builds. It also removes the package specific translations from the individual directories to avoid confusion since they are no longer used. It also removes some language packs that are no longer used/supported for the packages. Another important note is that another new package is introduced in the form of babel-preset-pretranslated-translations-package-format-message. This package is important because it allows the use of pretranslated builds for canvas-rce without causing problems trying to read in non-existant translations from the canvas-rce directory. Also the new build.js script inside of canvas-rce provides means to continue doing pretranslated builds for canvas-rce while using @instructure/translations. While these aren't used anywhere currently, it's a positive pattern going forward for other packages and this can be used a template to move towards that pattern. Test Plan: - Canvas builds appropriately - Translations of the canvas-planner and canvas-rce work as expected closes USERS-45 flag = none Change-Id: I1653bc7440aefc7299341b8cc2486843db320aee Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/220506 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> QA-Review: Charley Kline <ckline@instructure.com> Product-Review: Clay Diffrient <cdiffrient@instructure.com> Reviewed-by: Charley Kline <ckline@instructure.com>
2019-12-12 10:41:51 +08:00
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
const fs = require('fs')
const path = require('path')
module.exports = function babelPresetPretranslatedFormatMessage(context, opts = {}) {
return {
plugins: getFormatMessageConfig(opts)
}
}
function getFormatMessageConfig({
translationsDir = 'translations',
extractDefaultTranslations = process.env.NODE_ENV !== 'test',
translationPackage = '@instructure/translations'
}) {
let formatMessageConfig = []
// if a BUILD_LOCALE environment variable is set, generate pre-translated source for that language,
const BUILD_LOCALE = process.env.BUILD_LOCALE
if (BUILD_LOCALE) {
formatMessageConfig = [
[
'transform-format-message',
{
generateId: 'underscored_crc32',
inline: true,
locale: BUILD_LOCALE,
translations: {
[BUILD_LOCALE]: require(path.join(translationPackage, translationsDir, BUILD_LOCALE))
}
}
]
]
// In test mode, sometimes we are only dealing with a subset of files so if we extracted strings we'd be missing some
} else if (extractDefaultTranslations) {
try {
fs.accessSync(path.join(process.cwd(), translationsDir, 'en.json'), fs.W_OK)
formatMessageConfig = [
[
'extract-format-message',
{
generateId: 'underscored_crc32',
outFile: 'translations/en.json'
}
]
]
} catch (e) {
// Skip string extraction since we don't have write access to translations/en.json
}
}
return formatMessageConfig
}