Skip to content

Commit 143f720

Browse files
committed
simplify usage
1 parent 9adabda commit 143f720

File tree

10 files changed

+29
-99
lines changed

10 files changed

+29
-99
lines changed

src/app/src/composables/useStudio.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export const useStudio = createSharedComposable(() => {
4040
const mediaTree = useTree(StudioFeature.Media, host, draftMedias)
4141
const context = useContext(host, git, documentTree, mediaTree)
4242

43+
ui.setLocale(host.meta.defaultLocale)
44+
4345
host.on.mounted(async () => {
4446
if (devMode.value) {
4547
initDevelopmentMode()

src/app/src/composables/useUI.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSharedComposable } from '@vueuse/core'
2-
import { ref, watch } from 'vue'
2+
import { getCurrentInstance, ref, watch } from 'vue'
33
import type { StudioHost } from '../types'
44
import { useSidebar } from './useSidebar'
55

@@ -21,6 +21,17 @@ export const useUI = createSharedComposable((host: StudioHost) => {
2121
}
2222
})
2323

24+
function setLocale(locale: string) {
25+
const currentVueInstance = getCurrentInstance()
26+
if (currentVueInstance) {
27+
import(`../locales/${locale}.json`).then((locales) => {
28+
const i18n = currentVueInstance.appContext.provides.i18n.global
29+
i18n.locale.value = locale
30+
i18n.setLocaleMessage(locale, locales.default)
31+
})
32+
}
33+
}
34+
2435
return {
2536
colorMode,
2637
sidebar,
@@ -30,5 +41,6 @@ export const useUI = createSharedComposable((host: StudioHost) => {
3041
},
3142
toggle: () => isOpen.value = !isOpen.value,
3243
close: () => isOpen.value = false,
44+
setLocale,
3345
}
3446
})

src/app/src/i18n.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

src/app/src/main.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import styles from './assets/css/main.css?inline'
66
import { createHead } from '@unhead/vue/client'
77
import { generateColors, tailwindColors } from './utils/colors'
88
import { refineTailwindStyles } from './utils/styles.ts'
9-
import { createStudioI18n } from './i18n'
9+
import { createI18n } from 'vue-i18n'
1010
import App from './app.vue'
1111
import Content from './pages/content.vue'
1212
import Media from './pages/media.vue'
@@ -54,8 +54,14 @@ if (typeof window !== 'undefined' && 'customElements' in window) {
5454

5555
app.use(router)
5656

57-
// @ts-expect-error - Global variable defined in plugin
58-
const i18n = createStudioI18n(window.__NUXT_STUDIO_DEFAULT_LOCALE__, window.__NUXT_STUDIO_I18N_MESSAGES__)
57+
const i18n = createI18n({
58+
legacy: false,
59+
locale: 'en',
60+
fallbackLocale: 'en',
61+
globalInjection: true,
62+
})
63+
64+
app.provide('i18n', i18n)
5965

6066
app.use(i18n)
6167

src/app/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface StudioHost {
2121
meta: {
2222
dev: boolean
2323
components: () => ComponentMeta[]
24+
defaultLocale: string
2425
}
2526
on: {
2627
routeChange: (fn: (to: RouteLocationNormalized, from: RouteLocationNormalized) => void) => void

src/module/src/i18n.ts

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

src/module/src/module.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { createStorage } from 'unstorage'
77
import { getAssetsStorageDevTemplate, getAssetsStorageTemplate } from './templates'
88
import { version } from '../../../package.json'
99
import { setupDevMode } from './dev'
10-
import { setupI18n } from './i18n'
1110

1211
interface ModuleOptions {
1312
/**
@@ -91,11 +90,6 @@ interface ModuleOptions {
9190
* @default 'en'
9291
*/
9392
defaultLocale?: string
94-
/**
95-
* Directly override or add translations.
96-
* @default {}
97-
*/
98-
translations?: Record<string, unknown>
9993
}
10094
}
10195

@@ -126,7 +120,6 @@ export default defineNuxtModule<ModuleOptions>({
126120
},
127121
i18n: {
128122
defaultLocale: 'en',
129-
translations: {},
130123
},
131124
},
132125
async setup(options, nuxt) {
@@ -158,6 +151,8 @@ export default defineNuxtModule<ModuleOptions>({
158151
},
159152
// @ts-expect-error Autogenerated type does not match with options
160153
repository: options.repository,
154+
// @ts-expect-error Autogenerated type does not match with options
155+
i18n: options.i18n,
161156
}
162157

163158
nuxt.options.runtimeConfig.studio = {
@@ -212,8 +207,6 @@ export default defineNuxtModule<ModuleOptions>({
212207
: getAssetsStorageTemplate(assetsStorage, nuxt),
213208
})
214209

215-
await setupI18n(nuxt, options.i18n!)
216-
217210
if (options.dev) {
218211
setupDevMode(nuxt, runtime, assetsStorage)
219212
}

src/module/src/runtime/host.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { kebabCase } from 'scule'
88
import type { StudioHost, StudioUser, DatabaseItem, MediaItem, Repository } from 'nuxt-studio/app'
99
import type { RouteLocationNormalized, Router } from 'vue-router'
1010
// @ts-expect-error queryCollection is not defined in .nuxt/imports.d.ts
11-
import { clearError, getAppManifest, queryCollection, queryCollectionItemSurroundings, queryCollectionNavigation, queryCollectionSearchSections } from '#imports'
11+
import { clearError, getAppManifest, queryCollection, queryCollectionItemSurroundings, queryCollectionNavigation, queryCollectionSearchSections, useRuntimeConfig } from '#imports'
1212
import { collections } from '#content/preview'
1313
import { publicAssetsStorage } from '#build/studio-public-assets'
1414
import { useHostMeta } from './composables/useMeta'
@@ -107,6 +107,7 @@ export function useStudioHost(user: StudioUser, repository: Repository): StudioH
107107
meta: {
108108
dev: false,
109109
components: () => meta.componentsMeta.value,
110+
defaultLocale: useRuntimeConfig().public.studio.i18n?.defaultLocale || 'en',
110111
},
111112
on: {
112113
routeChange: (fn: (to: RouteLocationNormalized, from: RouteLocationNormalized) => void) => {

0 commit comments

Comments
 (0)