Skip to content

Commit fc881b6

Browse files
committed
feat(i18n): add loadPageTranslations method to load translations
1 parent 41fa00b commit fc881b6

File tree

8 files changed

+81
-0
lines changed

8 files changed

+81
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<template>
2+
<div>
3+
<h1>Test $loadPageTranslations Method</h1>
4+
5+
<div>
6+
<h2>Current Locale: {{ $getLocale() }}</h2>
7+
<h2>Route Name: {{ $getRouteName() }}</h2>
8+
</div>
9+
10+
<div>
11+
<h3>Translations before loading:</h3>
12+
<p>test_key: {{ $t('test_key') || 'NOT FOUND' }}</p>
13+
<p>dynamic_key: {{ $t('dynamic_key') || 'NOT FOUND' }}</p>
14+
</div>
15+
16+
<div>
17+
<button @click="loadTestTranslations">
18+
Load Test Translations
19+
</button>
20+
</div>
21+
22+
<div v-if="translationsLoaded">
23+
<h3>Translations after loading:</h3>
24+
<p>test_key: {{ $t('test_key') }}</p>
25+
<p>dynamic_key: {{ $t('dynamic_key') }}</p>
26+
<p>nested.key: {{ $t('nested.key') }}</p>
27+
</div>
28+
29+
<div>
30+
<h3>Check translation existence:</h3>
31+
<p>has test_key: {{ $has('test_key') }}</p>
32+
<p>has dynamic_key: {{ $has('dynamic_key') }}</p>
33+
<p>has nested.key: {{ $has('nested.key') }}</p>
34+
</div>
35+
</div>
36+
</template>
37+
38+
<script setup lang="ts">
39+
import { useNuxtApp } from '#imports'
40+
41+
const { $getLocale, $getRouteName, $t, $has, $loadPageTranslations } = useNuxtApp()
42+
43+
const translationsLoaded = ref(false)
44+
45+
const loadTestTranslations = async () => {
46+
const currentLocale = $getLocale()
47+
const routeName = $getRouteName()
48+
49+
const testTranslations = {
50+
test_key: `Test key for ${currentLocale}`,
51+
dynamic_key: `Dynamic key loaded at ${new Date().toLocaleTimeString()}`,
52+
nested: {
53+
key: `Nested key for ${currentLocale}`,
54+
},
55+
}
56+
57+
try {
58+
await $loadPageTranslations(currentLocale, routeName, testTranslations)
59+
translationsLoaded.value = true
60+
console.log('Translations loaded successfully:', testTranslations)
61+
}
62+
catch (error) {
63+
console.error('Error loading translations:', error)
64+
}
65+
}
66+
67+
// Check if method is available
68+
onMounted(() => {
69+
console.log('$loadPageTranslations method is available:', typeof $loadPageTranslations === 'function')
70+
})
71+
</script>

src/runtime/composables/useI18n.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function useI18n(): PluginsInjectionsWithAliases {
3232
$switchRoute: nuxtApp.$switchRoute,
3333
$localeRoute: nuxtApp.$localeRoute,
3434
$localePath: nuxtApp.$localePath,
35+
$loadPageTranslations: nuxtApp.$loadPageTranslations,
3536
} as const
3637

3738
const noDollarInjections = Object.fromEntries(

src/runtime/plugins/01.plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ export default defineNuxtPlugin(async (nuxtApp) => {
249249
i18nRouteParams.value = value
250250
return i18nRouteParams.value
251251
},
252+
loadPageTranslations: async (locale: string, routeName: string, translations: Translations) => {
253+
await i18nHelper.loadPageTranslations(locale, routeName, translations)
254+
},
252255
}
253256

254257
const $provideData = Object.fromEntries(
@@ -288,4 +291,5 @@ export interface PluginsInjections {
288291
$localeRoute: (to: RouteLocationNamedRaw | RouteLocationResolvedGeneric | string, locale?: string) => RouteLocationResolved
289292
$localePath: (to: RouteLocationNamedRaw | RouteLocationResolvedGeneric | string, locale?: string) => string
290293
$setI18nRouteParams: (value: I18nRouteParams) => I18nRouteParams
294+
$loadPageTranslations: (locale: string, routeName: string, translations: Translations) => Promise<void>
291295
}

0 commit comments

Comments
 (0)