1
0
mirror of synced 2026-01-09 06:03:09 -05:00

Merge pull request #21884 from github/repo-sync

repo sync
This commit is contained in:
Octomerger Bot
2022-11-07 11:23:27 -08:00
committed by GitHub
4 changed files with 32 additions and 16 deletions

View File

@@ -1,16 +1,12 @@
import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs/promises'
import Page from './page.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// Module level cache
const _basePaths = new Map()
// Return a full directory based on __dirname from a specific language directory.
// This function is memoized with a simple global cache object.
export function getBasePath(directory) {
if (!_basePaths.has(directory)) {
_basePaths.set(directory, path.posix.join(__dirname, '..', directory, 'content'))
_basePaths.set(directory, path.posix.join(directory, 'content'))
}
return _basePaths.get(directory)
}

View File

@@ -1,14 +1,38 @@
// see also languages-schema.js
import dotenv from 'dotenv'
import { TRANSLATIONS_ROOT } from './constants.js'
import path from 'path'
dotenv.config()
const possibleEnvVars = {
'es-ES': process.env.TRANSLATIONS_ROOT_ES_ES,
'zh-CN': process.env.TRANSLATIONS_ROOT_ZH_CN,
'ja-JP': process.env.TRANSLATIONS_ROOT_JA_JP,
'pt-BR': process.env.TRANSLATIONS_ROOT_PT_BR,
}
function getRoot(languageCode) {
if (languageCode === 'en') return ''
if (languageCode in possibleEnvVars) {
const possibleEnvVar = possibleEnvVars[languageCode]
if (possibleEnvVar) {
return possibleEnvVar
}
} else {
console.warn(`Not recognized languageCode '${languageCode}'`)
}
// Default
return path.join(TRANSLATIONS_ROOT, languageCode)
}
const languages = {
en: {
name: 'English',
code: 'en',
hreflang: 'en',
dir: '',
dir: getRoot('en'),
},
cn: {
name: 'Simplified Chinese',
@@ -16,7 +40,7 @@ const languages = {
code: 'cn',
hreflang: 'zh-Hans',
redirectPatterns: [/^\/zh-\w{2}/, /^\/zh/],
dir: path.join(TRANSLATIONS_ROOT, 'zh-CN'),
dir: getRoot('zh-CN'),
},
ja: {
name: 'Japanese',
@@ -24,21 +48,21 @@ const languages = {
code: 'ja',
hreflang: 'ja',
redirectPatterns: [/^\/jp/],
dir: path.join(TRANSLATIONS_ROOT, 'ja-JP'),
dir: getRoot('ja-JP'),
},
es: {
name: 'Spanish',
nativeName: 'Español',
code: 'es',
hreflang: 'es',
dir: path.join(TRANSLATIONS_ROOT, 'es-ES'),
dir: getRoot('es-ES'),
},
pt: {
name: 'Portuguese',
nativeName: 'Português do Brasil',
code: 'pt',
hreflang: 'pt',
dir: path.join(TRANSLATIONS_ROOT, 'pt-BR'),
dir: getRoot('pt-BR'),
},
}

View File

@@ -1,4 +1,3 @@
import { fileURLToPath } from 'url'
import path from 'path'
import languages from './languages.js'
import { allVersions } from './all-versions.js'
@@ -7,7 +6,6 @@ import loadSiteData from './site-data.js'
import nonEnterpriseDefaultVersion from './non-enterprise-default-version.js'
import Page from './page.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const versions = Object.keys(allVersions)
// These are the exceptions to the rule.
@@ -42,7 +40,7 @@ export async function loadUnversionedTree(languagesOnly = null) {
})
await Promise.all(
languagesValues.map(async (langObj) => {
const localizedContentPath = path.posix.join(__dirname, '..', langObj.dir, 'content')
const localizedContentPath = path.posix.join(langObj.dir, 'content')
unversionedTree[langObj.code] = await createTree(localizedContentPath, langObj)
})
)

View File

@@ -1,4 +1,3 @@
import { fileURLToPath } from 'url'
import path from 'path'
import { existsSync } from 'fs'
@@ -7,8 +6,7 @@ import { languageKeys } from '../lib/languages.js'
const languagePrefixRegex = new RegExp(`^/(${languageKeys.join('|')})(/|$)`)
const englishPrefixRegex = /^\/en(\/|$)/
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const CONTENT_ROOT = path.posix.join(__dirname, '..', 'content')
const CONTENT_ROOT = 'content'
export default async function findPage(
req,