-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { mkdirSync } from 'node:fs' | ||
import { dirname, join } from 'node:path' | ||
import { cwd } from './utils.mjs' | ||
import z from 'zod' | ||
|
||
const CWD = cwd(import.meta.url) | ||
const BASE = join(CWD, '..') | ||
const LOCALES_BASE = join(BASE, 'shared/locales') | ||
|
||
const env = z | ||
.object({ | ||
BUILD_OUTPUT: z.string(), | ||
NODE_ENV: z.union([z.literal('production'), z.literal('development')]).default('production'), | ||
}) | ||
.parse(process.env) | ||
|
||
// const IS_PROD = env.NODE_ENV === 'production' | ||
const OUTPUT_DIR = join(BASE, env.BUILD_OUTPUT, 'locales') | ||
|
||
// Function to read JSON file and return its contents as an object | ||
function readJSONFile(filePath) { | ||
try { | ||
const data = fs.readFileSync(filePath, 'utf8') | ||
return JSON.parse(data) | ||
} catch (err) { | ||
console.error(`Error reading file ${filePath}:`, err) | ||
return null | ||
} | ||
} | ||
|
||
// Function to create a combined JSON file for a locale directory | ||
function createLocaleFile(localePath, locale) { | ||
const result = {} | ||
const files = fs.readdirSync(localePath) | ||
|
||
files.forEach((file) => { | ||
const filePath = path.join(localePath, file) | ||
if (fs.statSync(filePath).isFile() && file.endsWith('.json')) { | ||
const fileName = path.basename(file, '.json') | ||
const json = readJSONFile(filePath) | ||
delete json.smartling | ||
Object.values(json).forEach((value) => { | ||
try { | ||
delete value.note | ||
} catch (e) { | ||
// | ||
} | ||
}) | ||
result[fileName] = json | ||
} | ||
}) | ||
|
||
let output = path.join(OUTPUT_DIR, `${locale}.json`) | ||
mkdirSync(dirname(output), { recursive: true }) | ||
fs.writeFileSync(output, JSON.stringify(result, null, 2), 'utf8') | ||
console.log(`✅ Created ${output}`) | ||
} | ||
|
||
// Function to traverse the locales directory and process each locale | ||
function processLocales() { | ||
const locales = fs.readdirSync(LOCALES_BASE) | ||
|
||
locales.forEach((locale) => { | ||
const localePath = path.join(LOCALES_BASE, locale) | ||
if (fs.statSync(localePath).isDirectory()) { | ||
createLocaleFile(localePath, locale) | ||
} | ||
}) | ||
} | ||
|
||
processLocales() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import { h } from 'preact' | ||
import { createContext } from 'preact' | ||
import { useData } from './data-provider' | ||
import { useEffect, useState } from 'preact/hooks' | ||
import { i18n } from '../shared/js/ui/base/localize' | ||
|
||
const TranslationContext = createContext({ | ||
/** @type {string} */ | ||
locale: 'en', | ||
}) | ||
|
||
export function TranslationProvider({ children }) { | ||
const data = useData() | ||
const locale = data.tab?.locale | ||
const [ready, setReady] = useState(false) | ||
useEffect(() => { | ||
let cont = new AbortController() | ||
async function fetchFile(locale, signal) { | ||
try { | ||
const s = await fetch(`../locales/${locale}.json`, { signal }).then((x) => x.json()) | ||
for (let [ns, translations] of Object.entries(s)) { | ||
i18n.addResourceBundle(locale, ns, translations) | ||
} | ||
} catch (e) { | ||
console.error('could not load locale') | ||
} | ||
|
||
if (Object.keys(/** @type {any} */ (i18n.options.resources)).includes(locale)) { | ||
i18n.changeLanguage(locale) | ||
} else { | ||
console.warn(`Unsupported locale ${locale}`) | ||
} | ||
} | ||
if (locale) { | ||
fetchFile(locale, cont.signal) | ||
.then(() => setReady(true)) | ||
.catch(console.error) | ||
} else { | ||
console.log('ignoring dup') | ||
} | ||
return () => { | ||
cont.abort() | ||
} | ||
}, [locale]) | ||
|
||
if (!ready) return null | ||
return <TranslationContext.Provider value={{ locale: 'en' }}>{children}</TranslationContext.Provider> | ||
} |