11import type { Preview } from '@storybook-vue/nuxt'
22import { withThemeByDataAttribute } from '@storybook/addon-themes'
3+ import { addons } from 'storybook/preview-api'
34import { currentLocales } from '../config/i18n'
45import { fn } from 'storybook/test'
56import { ACCENT_COLORS } from '../shared/utils/constants'
@@ -19,6 +20,12 @@ globalThis['__NUXT_COLOR_MODE__'] ??= {
1920// @ts -expect-error - dynamic global name
2021globalThis . 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+
2229const 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 } ,
0 commit comments