An open source design system that's fully customizable and agent ready
TypeScript
// Copyright (c) Meta Platforms, Inc. and affiliates.
/**
* @file scripts/swift-ios-strings.mjs
* @input Core's locale catalogs (`@astryxdesign/core/locales/*.json`)
* @output apple/Sources/Astryx/Resources/<locale>.lproj/Localizable.strings
* @position Run by `generate:swift-ios`; the output is committed and a test
* fails when it drifts from core's catalogs
*/
import fs from 'node:fs';
import path from 'node:path';
import {createRequire} from 'node:module';
import {fileURLToPath} from 'node:url';
const PACKAGE_ROOT = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'..',
);
export const SWIFT_IOS_STRINGS_DIR = path.join(
PACKAGE_ROOT,
'apple',
'Sources',
'Astryx',
'Resources',
);
/** The catalog messages the Swift components speak. */
export const SWIFT_IOS_STRING_KEYS = [
'@astryx.avatar.nameWithStatus',
'@astryx.button.loading',
'@astryx.field.optional',
'@astryx.field.required',
'@astryx.input.readOnly',
'@astryx.input.statusButton.error',
'@astryx.input.statusButton.success',
'@astryx.input.statusButton.warning',
'@astryx.inputClearButton.tooltip',
'@astryx.spinner.loading',
'@astryx.textInput.clearLabel',
'@astryx.thumbnail.fallbackName',
'@astryx.thumbnail.open',
'@astryx.thumbnail.remove',
];
// Apple names a localization by its language alone unless one language has
// several catalogs (pt-BR, pt-PT), and writes Chinese by script.
const APPLE_LOCALES = {'zh-CN': 'zh-Hans', 'zh-TW': 'zh-Hant', 'no-NO': 'nb'};
function appleLocale(locale, languageCounts) {
if (APPLE_LOCALES[locale]) return APPLE_LOCALES[locale];
const language = locale.split('-')[0];
return languageCounts[language] > 1 ? locale : language;
}
function localesDir() {
const require = createRequire(import.meta.url);
// Read through core's public `./locales/*.json` export.
return path.dirname(require.resolve('@astryxdesign/core/locales/en.json'));
}
function quote(text) {
return `"${text.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n')}"`;
}
/**
* The generated `.strings` files, keyed by their path under
* `core/Resources/`. Core's pseudo-locale is left out.
*/
export function renderSwiftIOSStrings() {
const dir = localesDir();
const locales = fs
.readdirSync(dir)
.filter(file => file.endsWith('.json') && file !== 'pseudo.json')
.map(file => file.slice(0, -'.json'.length))
.sort();
const languageCounts = {};
for (const locale of locales) {
const language = locale.split('-')[0];
languageCounts[language] = (languageCounts[language] ?? 0) + 1;
}
const files = {};
for (const locale of locales) {
const file = `${locale}.json`;
const catalog = JSON.parse(fs.readFileSync(path.join(dir, file), 'utf8'));
const lines = [
"/* Generated by @astryxdesign/native from core's locale catalogs. Do not edit. */",
];
for (const key of SWIFT_IOS_STRING_KEYS) {
const entry = catalog[key];
// A catalog without a message falls back to English, as core does at
// run time; English itself must have every message.
if (!entry && locale.startsWith('en')) {
throw new Error(`${file} has no ${key}`);
}
if (!entry) continue;
lines.push('', `/* ${entry.description.replace(/\*\//g, '* /')} */`);
lines.push(`${quote(key)} = ${quote(entry.defaultMessage)};`);
}
files[`${appleLocale(locale, languageCounts)}.lproj/Localizable.strings`] =
`${lines.join('\n')}\n`;
}
return files;
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
fs.rmSync(SWIFT_IOS_STRINGS_DIR, {recursive: true, force: true});
const files = renderSwiftIOSStrings();
for (const [relative, text] of Object.entries(files)) {
const out = path.join(SWIFT_IOS_STRINGS_DIR, relative);
fs.mkdirSync(path.dirname(out), {recursive: true});
fs.writeFileSync(out, text);
}
console.log(
`wrote ${Object.keys(files).length} files to ${path.relative(PACKAGE_ROOT, SWIFT_IOS_STRINGS_DIR)}`,
);
}