Skip to content

Commit 278539f

Browse files
committed
feat(storybook): add localization tooling
1 parent 5eea58b commit 278539f

File tree

6 files changed

+71
-56
lines changed

6 files changed

+71
-56
lines changed

.storybook/decorators/language.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { makeDecorator, useEffect } from "@storybook/preview-api";
2-
import { fetchContainers } from "./helpers.js";
1+
import { allFakers } from "@faker-js/faker";
2+
import { makeDecorator, useEffect } from '@storybook/preview-api';
3+
import { capitalize } from "lodash-es";
4+
import { fetchContainers } from "./helpers";
35
/* global Typekit */
46

57
/**
@@ -17,9 +19,21 @@ export const withLanguageWrapper = makeDecorator({
1719
viewMode,
1820
} = context;
1921

20-
useEffect(() => {
21-
const isNotEnglish = lang && lang !== "en-US";
22+
const isNotEnglish = lang && lang !== "en-US";
23+
const isRTL = ["ar", "fa", "he"].includes(lang);
24+
25+
// @todo: this can't be used for VRT because the strings are random
26+
// Attach the generator tool to the parameters for use in the story
27+
context.generator = allFakers?.[lang];
28+
29+
// Add a custom generator for titles
30+
context.generator.lorem.title = (count) => capitalize(context.generator.lorem.words(count));
2231

32+
// Add a textDirection property to the globals for use in the story
33+
context.globals.textDirection = isRTL ? "rtl" : "ltr";
34+
35+
36+
useEffect(() => {
2337
// If it is US-language or unset use the rok6rmo Adobe font web project id (smaller size),
2438
// otherwise use the mge7bvf kit with all the language settings (larger size)
2539
const kitId = isNotEnglish ? "mge7bvf" : "rok6rmo";
@@ -55,10 +69,12 @@ export const withLanguageWrapper = makeDecorator({
5569
} catch (e) {/* empty */}
5670
}
5771

72+
// Set the language and direction on the relevant containers
5873
for (const container of fetchContainers(id, viewMode === "docs")) {
5974
container.lang = lang;
75+
container.dir = !isRTL ? "ltr" : "rtl";
6076
}
61-
}, [lang]);
77+
}, [lang, isNotEnglish, isRTL, viewMode, id]);
6278

6379
return StoryFn(context);
6480
},

.storybook/decorators/text-direction.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

.storybook/modes/index.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const modes = {
1515
"Context: Spectrum 1": {
1616
scale: "medium",
1717
color: "light",
18-
textDirection: "ltr",
18+
lang: "en_US",
1919
context: "legacy",
2020
},
2121
"Context: Express": {
@@ -27,7 +27,7 @@ const modes = {
2727
"Dark | RTL": {
2828
scale: "medium",
2929
color: "dark",
30-
textDirection: "rtl",
30+
lang: "ar",
3131
},
3232
};
3333

@@ -39,3 +39,34 @@ export const disableDefaultModes = {
3939
return acc;
4040
}, {}),
4141
};
42+
43+
export const mobile = {
44+
"Mobile": {
45+
scale: "large",
46+
},
47+
};
48+
49+
export const viewports = {
50+
small: {
51+
width: 480,
52+
},
53+
};
54+
55+
export const i18n = {
56+
// This is the default language, so we don't need to specify it here
57+
// "English": {
58+
// lang: "en_US",
59+
// },
60+
"Hebrew": {
61+
lang: "he",
62+
},
63+
"Japanese": {
64+
lang: "ja",
65+
},
66+
"Korean": {
67+
lang: "ko",
68+
},
69+
"Arabic": {
70+
lang: "ar",
71+
},
72+
};

.storybook/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"exports": {
1010
".": "./preview.js",
1111
"./blocks": "./blocks/index.js",
12+
"./blocks/*": "./blocks/*",
1213
"./decorators": "./decorators/index.js",
1314
"./decorators/*": "./decorators/*",
14-
"./deprecated/*": "./deprecated/*",
1515
"./loaders": "./loaders/index.js",
1616
"./loaders/*": "./loaders/*",
1717
"./main": "./main.js",
@@ -20,6 +20,7 @@
2020
"./modes/*": "./modes/*",
2121
"./package.json": "./package.json",
2222
"./preview": "./preview.js",
23+
"./templates/*": "./templates/*",
2324
"./types": "./types/index.js",
2425
"./types/*": "./types/*"
2526
},
@@ -38,6 +39,7 @@
3839
"@babel/core": "^7.26.0",
3940
"@chromaui/addon-visual-tests": "^1.0.0",
4041
"@etchteam/storybook-addon-status": "^5.0.0",
42+
"@faker-js/faker": "^8.4.1",
4143
"@storybook/addon-a11y": "^8.3.6",
4244
"@storybook/addon-actions": "^8.3.6",
4345
"@storybook/addon-console": "^3.0.0",

.storybook/types/global.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ export default {
4747
dynamicTitle: true,
4848
},
4949
},
50-
textDirection: {
51-
title: "Text direction",
52-
description: "Direction of the content flow",
53-
defaultValue: "ltr",
54-
type: "string",
55-
toolbar: {
56-
items: [
57-
{ value: "ltr", title: "Left to right" },
58-
{ value: "rtl", title: "Right to left" },
59-
],
60-
dynamicTitle: true,
61-
},
62-
},
6350
// @todo https://jira.corp.adobe.com/browse/CSS-314
6451
reducedMotion: {
6552
title: "Reduce motion",
@@ -77,14 +64,15 @@ export default {
7764
lang: {
7865
title: "Language",
7966
description: "Language of the content",
80-
defaultValue: "en-US",
67+
defaultValue: "en_US",
8168
type: "string",
8269
toolbar: {
8370
items: [
84-
{ value: "en-US", title: "🇺🇸", right: "English (US)" },
85-
{ value: "ja", title: "🇯🇵", right: "日本語" },
86-
{ value: "ko", title: "🇰🇷", right: "한국어" },
87-
{ value: "zh", title: "🇨🇳", right: "中文" },
71+
{ value: "en_US", title: "English", right: "English (US)" },
72+
{ value: "he", title: "Hebrew", right: "עִברִית" },
73+
{ value: "ja", title: "Japanese", right: "日本語" },
74+
{ value: "ko", title: "Korean", right: "한국어" },
75+
{ value: "ar", title: "Arabic", right: "عربي" },
8876
],
8977
dynamicTitle: true,
9078
},

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,13 @@ __metadata:
19351935
languageName: node
19361936
linkType: hard
19371937

1938+
"@faker-js/faker@npm:^8.4.1":
1939+
version: 8.4.1
1940+
resolution: "@faker-js/faker@npm:8.4.1"
1941+
checksum: 10c0/4f2aecddcfdc2cc8b50b2d15d1e37302a7c7a5bbd184ae910a9d271bc11248533ca74dcdd4a9ccbe20410553e7af0f6a4d334c5b955635e09a32ddf4a64942d4
1942+
languageName: node
1943+
linkType: hard
1944+
19381945
"@fastify/busboy@npm:^2.0.0":
19391946
version: 2.1.1
19401947
resolution: "@fastify/busboy@npm:2.1.1"
@@ -3828,6 +3835,7 @@ __metadata:
38283835
"@babel/core": "npm:^7.26.0"
38293836
"@chromaui/addon-visual-tests": "npm:^1.0.0"
38303837
"@etchteam/storybook-addon-status": "npm:^5.0.0"
3838+
"@faker-js/faker": "npm:^8.4.1"
38313839
"@spectrum-css/table": "workspace:^"
38323840
"@spectrum-css/tokens": "workspace:^"
38333841
"@spectrum-css/typography": "workspace:^"

0 commit comments

Comments
 (0)