Skip to content

Commit 1cd901f

Browse files
docs(ui): use storybook-i18n addon for locale switching (#2125)
Co-authored-by: Willow (GHOST) <ghostdevbusiness@gmail.com> Co-authored-by: Willow (GHOST) <git@willow.sh>
1 parent 3f2b865 commit 1cd901f

File tree

6 files changed

+71
-31
lines changed

6 files changed

+71
-31
lines changed

.storybook/main.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import type { StorybookConfig } from '@storybook-vue/nuxt'
22

33
const config = {
44
stories: ['../.storybook/*.mdx', '../app/**/*.stories.@(js|ts)'],
5-
addons: ['@storybook/addon-a11y', '@storybook/addon-docs', '@storybook/addon-themes'],
5+
addons: [
6+
'@storybook/addon-a11y',
7+
'@storybook/addon-docs',
8+
'@storybook/addon-themes',
9+
'storybook-i18n',
10+
],
611
framework: '@storybook-vue/nuxt',
712
staticDirs: ['./.public'],
813
features: {
@@ -23,7 +28,7 @@ const config = {
2328
// Minimal shims for Storybook v10 module federation system
2429
// These will be replaced when Storybook runtime loads
2530
window.__STORYBOOK_MODULE_GLOBAL__ = { global: window };
26-
window.__STORYBOOK_MODULE_CLIENT_LOGGER__ = {
31+
window.__STORYBOOK_MODULE_CLIENT_LOGGER__ = {
2732
deprecate: console.warn.bind(console, '[deprecated]'),
2833
once: console.log.bind(console),
2934
logger: console

.storybook/preview.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Preview } from '@storybook-vue/nuxt'
22
import { withThemeByDataAttribute } from '@storybook/addon-themes'
3+
import { addons } from 'storybook/preview-api'
34
import { currentLocales } from '../config/i18n'
45
import { fn } from 'storybook/test'
56
import { ACCENT_COLORS } from '../shared/utils/constants'
@@ -19,6 +20,12 @@ globalThis['__NUXT_COLOR_MODE__'] ??= {
1920
// @ts-expect-error - dynamic global name
2021
globalThis.defineOgImageComponent = fn()
2122

23+
// Subscribe to locale changes from storybook-i18n addon (once, outside decorator)
24+
let currentI18nInstance: any = null
25+
addons.getChannel().on('LOCALE_CHANGED', (newLocale: string) => {
26+
currentI18nInstance?.setLocale(newLocale)
27+
})
28+
2229
const preview: Preview = {
2330
parameters: {
2431
controls: {
@@ -31,24 +38,18 @@ const preview: Preview = {
3138
theme: npmxDark,
3239
},
3340
},
41+
initialGlobals: {
42+
locale: 'en-US',
43+
locales: currentLocales.reduce(
44+
(acc, locale) => {
45+
acc[locale.code] = locale.name
46+
return acc
47+
},
48+
{} as Record<string, string>,
49+
),
50+
},
3451
// Provides toolbars to switch things like theming and language
3552
globalTypes: {
36-
locale: {
37-
name: 'Locale',
38-
description: 'UI language',
39-
defaultValue: 'en-US',
40-
toolbar: {
41-
icon: 'globe',
42-
dynamicTitle: true,
43-
items: [
44-
// English is at the top so it's easier to reset to it
45-
{ value: 'en-US', title: 'English (US)' },
46-
...currentLocales
47-
.filter(locale => locale.code !== 'en-US')
48-
.map(locale => ({ value: locale.code, title: locale.name })),
49-
],
50-
},
51-
},
5253
accentColor: {
5354
name: 'Accent Color',
5455
description: 'Accent color',
@@ -75,9 +76,9 @@ const preview: Preview = {
7576
attributeName: 'data-theme',
7677
}),
7778
(story, context) => {
78-
const { locale, accentColor } = context.globals as {
79-
locale: string
79+
const { accentColor, locale } = context.globals as {
8080
accentColor?: string
81+
locale?: string
8182
}
8283

8384
// Set accent color from globals
@@ -89,14 +90,12 @@ const preview: Preview = {
8990

9091
return {
9192
template: '<story />',
92-
// Set locale from globals
9393
created() {
94-
if (this.$i18n) {
95-
this.$i18n.setLocale(locale)
96-
}
97-
},
98-
updated() {
99-
if (this.$i18n) {
94+
// Store i18n instance for LOCALE_CHANGED events
95+
currentI18nInstance = this.$i18n
96+
97+
// Set initial locale when component is created
98+
if (locale && this.$i18n) {
10099
this.$i18n.setLocale(locale)
101100
}
102101
},

app/components/Link/Link.stories.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ const meta = {
55
component: LinkBase,
66
args: {
77
to: '/',
8-
default: 'Click me',
98
},
109
} satisfies Meta<typeof LinkBase>
1110

1211
export default meta
1312
type Story = StoryObj<typeof meta>
1413

15-
export const Default: Story = {}
14+
export const Default: Story = {
15+
args: {
16+
default: 'Click me',
17+
},
18+
}
1619

1720
export const ExternalLink: Story = {
1821
args: {
@@ -75,16 +78,28 @@ export const WithIconButton: Story = {
7578
args: {
7679
variant: 'button-primary',
7780
classicon: 'i-lucide:copy',
78-
default: 'Copy',
7981
},
82+
render: args => ({
83+
components: { LinkBase },
84+
setup() {
85+
return { args }
86+
},
87+
template: `<LinkBase v-bind="args">{{ $t("package.readme.copy_as_markdown") }}</LinkBase>`,
88+
}),
8089
}
8190

8291
export const WithKeyboardShortcut: Story = {
8392
args: {
8493
variant: 'button-secondary',
8594
ariaKeyshortcuts: 's',
86-
default: 'Search',
8795
},
96+
render: args => ({
97+
components: { LinkBase },
98+
setup() {
99+
return { args }
100+
},
101+
template: `<LinkBase v-bind="args">{{ $t("search.button") }}</LinkBase>`,
102+
}),
88103
}
89104

90105
export const BlockLink: Story = {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
"markdown-it-anchor": "9.2.0",
139139
"schema-dts": "1.1.5",
140140
"storybook": "catalog:storybook",
141+
"storybook-i18n": "catalog:storybook",
141142
"typescript": "5.9.3",
142143
"unplugin-vue-markdown": "30.0.0",
143144
"vitest": "npm:@voidzero-dev/vite-plus-test@0.1.12",

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ catalogs:
4747
'@storybook/addon-docs': '^10.3.1'
4848
'@storybook/addon-themes': '^10.3.1'
4949
'storybook': '^10.3.1'
50+
'storybook-i18n': '^10.1.1'

0 commit comments

Comments
 (0)