Skip to content

Commit 0f558fc

Browse files
committed
feat(extendLocale): new composable
Resolves #3729
1 parent 1841e13 commit 0f558fc

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

docs/content/1.getting-started/7.i18n/1.nuxt.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { fr } from '@nuxt/ui-pro/locale'
6060

6161
### Custom locale
6262

63-
You also have the option to add your own locale using `defineLocale`:
63+
You can create your own locale using the `defineLocale` composable:
6464

6565
::module-only
6666

@@ -125,6 +125,65 @@ Look at the `code` parameter, there you need to pass the iso code of the languag
125125

126126
::
127127

128+
### Extend locale :badge{label="Soon" class="align-text-top"}
129+
130+
You can customize an existing locale by overriding its `messages` or `code` using the `extendLocale` composable:
131+
132+
::module-only
133+
134+
#ui
135+
:::div
136+
137+
```vue [app.vue]
138+
<script setup lang="ts">
139+
import { en } from '@nuxt/ui/locale'
140+
141+
const locale = extendLocale(en, {
142+
code: 'en-GB',
143+
messages: {
144+
commandPalette: {
145+
placeholder: 'Search a component...'
146+
}
147+
}
148+
})
149+
</script>
150+
151+
<template>
152+
<UApp :locale="locale">
153+
<NuxtPage />
154+
</UApp>
155+
</template>
156+
```
157+
158+
:::
159+
160+
#ui-pro
161+
:::div
162+
163+
```vue [app.vue]
164+
<script setup lang="ts">
165+
import { en } from '@nuxt/ui-pro/locale'
166+
167+
const locale = extendLocale(en, {
168+
code: 'en-GB',
169+
messages: {
170+
commandPalette: {
171+
placeholder: 'Search a component...'
172+
}
173+
}
174+
})
175+
</script>
176+
177+
<template>
178+
<UApp :locale="locale">
179+
<NuxtPage />
180+
</UApp>
181+
</template>
182+
```
183+
184+
:::
185+
::
186+
128187
### Dynamic locale
129188

130189
To dynamically switch between languages, you can use the [Nuxt I18n](https://i18n.nuxtjs.org/) module.

docs/content/1.getting-started/7.i18n/2.vue.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { fr } from '@nuxt/ui-pro/locale'
6060

6161
### Custom locale
6262

63-
You also have the option to add your locale using `defineLocale`:
63+
You can create your own locale using the `defineLocale` composable:
6464

6565
::module-only
6666

@@ -127,6 +127,67 @@ Look at the `code` parameter, there you need to pass the iso code of the languag
127127

128128
::
129129

130+
### Extend locale :badge{label="Soon" class="align-text-top"}
131+
132+
You can customize an existing locale by overriding its `messages` or `code` using the `extendLocale` composable:
133+
134+
::module-only
135+
136+
#ui
137+
:::div
138+
139+
```vue [App.vue]
140+
<script setup lang="ts">
141+
import { en } from '@nuxt/ui/locale'
142+
import { extendLocale } from '@nuxt/ui/composables/defineLocale.js'
143+
144+
const locale = extendLocale(en, {
145+
code: 'en-GB',
146+
messages: {
147+
commandPalette: {
148+
placeholder: 'Search a component...'
149+
}
150+
}
151+
})
152+
</script>
153+
154+
<template>
155+
<UApp :locale="locale">
156+
<RouterView />
157+
</UApp>
158+
</template>
159+
```
160+
161+
:::
162+
163+
#ui-pro
164+
:::div
165+
166+
```vue [App.vue]
167+
<script setup lang="ts">
168+
import { en } from '@nuxt/ui-pro/locale'
169+
import { extendLocale } from '@nuxt/ui/composables/defineLocale.js'
170+
171+
const locale = extendLocale(en, {
172+
code: 'en-GB',
173+
messages: {
174+
commandPalette: {
175+
placeholder: 'Search a component...'
176+
}
177+
}
178+
})
179+
</script>
180+
181+
<template>
182+
<UApp :locale="locale">
183+
<RouterView />
184+
</UApp>
185+
</template>
186+
```
187+
188+
:::
189+
::
190+
130191
### Dynamic locale
131192

132193
To dynamically switch between languages, you can use the [Vue I18n](https://vue-i18n.intlify.dev/) plugin.

src/runtime/composables/defineLocale.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defu } from 'defu'
22
import type { Locale, Direction } from '../types/locale'
3+
import type { DeepPartial } from '../types/utils'
34

45
interface DefineLocaleOptions<M> {
56
name: string
@@ -12,3 +13,8 @@ interface DefineLocaleOptions<M> {
1213
export function defineLocale<M>(options: DefineLocaleOptions<M>): Locale<M> {
1314
return defu<DefineLocaleOptions<M>, [{ dir: Direction }]>(options, { dir: 'ltr' })
1415
}
16+
17+
/* @__NO_SIDE_EFFECTS__ */
18+
export function extendLocale<M>(locale: Locale<M>, options: Partial<DefineLocaleOptions<DeepPartial<M>>>): Locale<M> {
19+
return defu<Locale<M>, [DefineLocaleOptions<M>]>(options, locale)
20+
}

src/runtime/types/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { VNode } from 'vue'
22
import type { AcceptableValue as _AcceptableValue } from 'reka-ui'
33

4+
export type DeepPartial<T> = {
5+
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P] | undefined
6+
}
7+
48
export type DynamicSlotsKeys<Name extends string | undefined, Suffix extends string | undefined = undefined> = (
59
Name extends string
610
? Suffix extends string

0 commit comments

Comments
 (0)