Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/localize-keystar-ui-strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystar/ui': patch
---

Localize the split view resize handle and the keyboard modifier labels in `Kbd`, and fall back to `en-US` for any key a locale doesn't have
5 changes: 5 additions & 0 deletions .changeset/localize-ui-strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Move the Admin UI's hard-coded English strings into the `en-US` locale so they can be translated, and fall back to English for any key a locale doesn't have
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ l10n.js
l10n.d.ts
packages/keystatic/src/app/l10n/index.js
packages/keystatic/src/app/l10n/index.d.ts
packages/keystatic/src/app/l10n/default-locale.js
packages/keystatic/src/app/l10n/default-locale.d.ts
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ l10n.js
l10n.d.ts
packages/keystatic/src/app/l10n/index.js
packages/keystatic/src/app/l10n/index.d.ts
packages/keystatic/src/app/l10n/default-locale.js
packages/keystatic/src/app/l10n/default-locale.d.ts
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ l10n.js
l10n.d.ts
packages/keystatic/src/app/l10n/index.js
packages/keystatic/src/app/l10n/index.d.ts
packages/keystatic/src/app/l10n/default-locale.js
packages/keystatic/src/app/l10n/default-locale.d.ts
24 changes: 19 additions & 5 deletions design-system/pkg/build-l10n.mts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,35 @@ for (const file of await fs.readdir(src)) {
const l10n: Record<string, Record<string, string>> = JSON.parse(
l10nFileContents
);
let out = 'const localizedMessages = {\n';
for (const [lang, translations] of Object.entries(l10n)) {
out += ` ${JSON.stringify(lang)}: {\n`;
const defaultStrings = l10n['en-US'];
const compile = (translations: Record<string, string>) => {
let out = '';
for (const [key, value] of Object.entries(translations)) {
out += ` ${JSON.stringify(key)}: ${compileString(value)},\n`;
}
out += ' },\n';
return out;
};
// every locale falls back to en-US for keys it doesn't have
let out = `const defaultStrings = {\n${compile(defaultStrings)}};\n\n`;
out += 'const localizedMessages = {\n';
for (const [lang, translations] of Object.entries(l10n)) {
out += ` ${JSON.stringify(lang)}: `;
out +=
lang === 'en-US'
? 'defaultStrings,\n'
: `{\n ...defaultStrings,\n${compile(translations)} },\n`;
}
out += '};\n';
out += 'export default localizedMessages;\n';
const withoutExtension = filePath.replace(/\.json$/, '');
await fs.writeFile(withoutExtension + '.js', out);
await fs.writeFile(
withoutExtension + '.d.ts',
`declare const localizedMessages: Record<string, Record<string, import('@internationalized/string').LocalizedString>>;
`declare const localizedMessages: Record<string, Record<${Object.keys(
defaultStrings
)
.map(key => JSON.stringify(key))
.join(' | ')}, import('@internationalized/string').LocalizedString>>;
export default localizedMessages;
`
);
Expand Down
5 changes: 4 additions & 1 deletion design-system/pkg/src/split-view/SplitView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@keystar/ui/style';
import { useId, useIsMounted } from '@keystar/ui/utils';
import { useLocale } from 'react-aria/I18nProvider';
import { useLocalizedStringFormatter } from 'react-aria/useLocalizedStringFormatter';
import { filterDOMProps } from 'react-aria/filterDOMProps';
import { useUpdateEffect } from 'react-aria/private/utils/useUpdateEffect';
import { clamp } from 'react-stately/private/utils/number';
Expand All @@ -23,6 +24,7 @@ import {

import { SplitViewProvider, useSplitView } from './context';
import { resetGlobalCursorStyle, setGlobalCursorStyle } from './cursor';
import localizedMessages from './l10n';
import { defaultStorage } from './storage';
import {
CursorState,
Expand Down Expand Up @@ -424,13 +426,14 @@ const SplitViewResizeHandle = forwardRef(function SplitViewResizeHandle(
forwardedRef: ForwardedRef<HTMLDivElement>
) {
let { activity, id, isCollapsed } = useSplitView();
let stringFormatter = useLocalizedStringFormatter(localizedMessages);

return (
<div
{...props}
ref={forwardedRef}
aria-controls={getPrimaryPaneId(id)}
aria-label="Resize" // FIXME: localize
aria-label={stringFormatter.format('resize')}
aria-orientation="vertical"
id={getResizeHandleId(id)}
role="separator"
Expand Down
3 changes: 3 additions & 0 deletions design-system/pkg/src/split-view/l10n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"en-US": { "resize": "Resize" }
}
11 changes: 7 additions & 4 deletions design-system/pkg/src/typography/Kbd.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { filterDOMProps } from 'react-aria/filterDOMProps';
import { useLocalizedStringFormatter } from 'react-aria/useLocalizedStringFormatter';
import { isMac as getIsMac } from 'react-aria/private/utils/platform';
import {
ForwardedRef,
Expand All @@ -15,6 +16,7 @@ import { DOMProps } from '@react-types/shared';
import { useSlotProps } from '@keystar/ui/slots';
import { BaseStyleProps, css } from '@keystar/ui/style';

import localizedMessages from './l10n';
import { useTextStyles } from './text';

function noopSubscribe() {
Expand Down Expand Up @@ -72,6 +74,7 @@ export const Kbd: ForwardRefExoticComponent<
});

const isMac = useIsMac();
const stringFormatter = useLocalizedStringFormatter(localizedMessages);

const modifiers = useMemo(() => {
const SYSTEM_KEYS = isMac
Expand All @@ -81,9 +84,9 @@ export const Kbd: ForwardRefExoticComponent<
shift: '⇧',
}
: {
alt: 'Alt',
meta: 'Ctrl',
shift: 'Shift',
alt: stringFormatter.format('alt'),
meta: stringFormatter.format('ctrl'),
shift: stringFormatter.format('shift'),
// shift: '⇧', // maybe?
};
let keys = [
Expand All @@ -93,7 +96,7 @@ export const Kbd: ForwardRefExoticComponent<
].filter(Boolean) as string[];

return joinModifierKeys(keys, isMac);
}, [alt, meta, shift, isMac]);
}, [alt, meta, shift, isMac, stringFormatter]);

return (
<kbd
Expand Down
3 changes: 3 additions & 0 deletions design-system/pkg/src/typography/l10n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"en-US": { "alt": "Alt", "ctrl": "Ctrl", "shift": "Shift" }
}
25 changes: 25 additions & 0 deletions design-system/pkg/test/l10n.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, test } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';

const src = path.resolve(
path.dirname(new URL(import.meta.url).pathname),
'../src'
);

test('every locale falls back to en-US for keys it does not have', async () => {
for (const component of fs.readdirSync(src)) {
const jsonPath = path.join(src, component, 'l10n.json');
if (!fs.existsSync(jsonPath)) continue;
const { default: messages } = await import(
path.join(src, component, 'l10n.js')
);
const defaultKeys = Object.keys(messages['en-US']).sort();
for (const locale of Object.keys(messages)) {
expect(
Object.keys(messages[locale]).sort(),
`${component} ${locale}`
).toEqual(defaultKeys);
}
}
});
54 changes: 45 additions & 9 deletions packages/keystatic/scripts/l10n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import path from 'path';
import { compileString } from '@internationalized/string-compiler';

const localesDir = 'src/app/l10n';
const defaultLocale = 'en-US';

function compileTranslations(translations: Record<string, string>) {
let out = '';
for (const [key, value] of Object.entries(translations).sort(([a], [b]) =>
a.localeCompare(b)
)) {
out += ` ${JSON.stringify(key)}: ${compileString(value)},\n`;
}
return out;
}

(async () => {
const locales: Record<string, Record<string, string>> = {};
for (const entry of (
Expand All @@ -24,23 +36,47 @@ const localesDir = 'src/app/l10n';
}
locales[locale] = translations;
}
let out = 'const strings = {\n';
const defaultTranslations = locales[defaultLocale];

// the default locale is in its own module so that code that also runs
// outside of the Admin UI (e.g. field validation in the reader) can use it
// without including every locale
await fs.writeFile(
path.join(localesDir, 'default-locale.js'),
`const defaultLocaleStrings = {\n${compileTranslations(
defaultTranslations
)}};\nexport default defaultLocaleStrings;\n`
);
const keyType = Object.keys(defaultTranslations)
.sort((a, b) => a.localeCompare(b))
.map(key => `\n | ${JSON.stringify(key)}`)
.join('');
await fs.writeFile(
path.join(localesDir, 'default-locale.d.ts'),
`export type LocalizedMessageKey =${keyType};
declare const defaultLocaleStrings: Record<LocalizedMessageKey, import('@internationalized/string').LocalizedString>;
export default defaultLocaleStrings;
`
);

// every locale falls back to the default locale for keys it doesn't have
let out = `import defaultLocaleStrings from './default-locale.js';\n\nconst strings = {\n`;
for (const [lang, translations] of Object.entries(locales)) {
out += ` ${JSON.stringify(lang)}: {\n`;
for (const [key, value] of Object.entries(translations).sort(([a], [b]) =>
a.localeCompare(b)
)) {
out += ` ${JSON.stringify(key)}: ${compileString(value)},\n`;
}
out += ' },\n';
out += ` ${JSON.stringify(lang)}: `;
out +=
lang === defaultLocale
? 'defaultLocaleStrings,\n'
: `{\n ...defaultLocaleStrings,\n${compileTranslations(
translations
)} },\n`;
}
out += '};\n';
out += 'export default strings;\n';

await fs.writeFile(path.join(localesDir, 'index.js'), out);
await fs.writeFile(
path.join(localesDir, 'index.d.ts'),
`declare const l10nMessages: Record<string, Record<string, import('@internationalized/string').LocalizedString>>;
`declare const l10nMessages: Record<string, Record<import('./default-locale').LocalizedMessageKey, import('@internationalized/string').LocalizedString>>;
export default l10nMessages;
`
);
Expand Down
Loading
Loading