forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make the message compiler work on the server side (nuxt-modules#…
…2223) * fix: make the message compiler work on the server side * fix
- Loading branch information
Showing
15 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<template> | ||
<div id="app"> | ||
{{ env }} | ||
{{ data }} | ||
<div v-if="correct && env == 'DEV'">working in dev as intended</div> | ||
<div v-if="!correct && env == 'DEV'">wtf now it's not working even in dev?</div> | ||
|
||
<div v-if="!correct && env == 'PROD'">unfortunately still doesn't work in prod</div> | ||
<div v-if="correct && env == 'PROD'">yeah! it's finally working in prod too</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const data = ref([]) | ||
const env = process.dev ? 'DEV' : 'PROD' | ||
const correct = computed(() => ['Test', 'Тест'].includes(data.value[0])) | ||
data.value = await $fetch('/api/foo') | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default [ | ||
{ | ||
code: 'en', | ||
name: 'English' | ||
}, | ||
{ | ||
code: 'ru', | ||
name: 'Русский' | ||
} | ||
].map(lang => ({ file: lang.code + '.json', ...lang })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"test": "Test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"test": "Тест" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import locales from './locales' | ||
|
||
export default defineNuxtConfig({ | ||
modules: ['@nuxtjs/i18n'], | ||
i18n: { | ||
lazy: true, | ||
langDir: 'locales', | ||
locales, | ||
defaultLocale: 'en', | ||
detectBrowserLanguage: false, | ||
strategy: 'no_prefix' | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "nuxt3-test-issues-2220", | ||
"private": true, | ||
"scripts": { | ||
"build": "nuxt build", | ||
"dev": "nuxt dev", | ||
"generate": "nuxt generate", | ||
"preview": "nuxt preview" | ||
}, | ||
"devDependencies": { | ||
"@nuxtjs/i18n": "latest", | ||
"nuxt": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default defineEventHandler(e => { | ||
return [e.context.$t('test')] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { defineEventHandler, getCookie } from 'h3' | ||
import { createI18n } from 'vue-i18n' | ||
import locales from '../../locales' | ||
import en from '../../locales/en.json' | ||
import ru from '../../locales/ru.json' | ||
|
||
const resources = { | ||
en, | ||
ru | ||
} | ||
|
||
const i18n = createI18n({ | ||
fallbackLocale: 'en' | ||
}).global | ||
|
||
for (const { code } of locales) { | ||
i18n.setLocaleMessage(code, resources[code]) | ||
} | ||
|
||
export default defineEventHandler(e => { | ||
e.context.$t = (key: string) => i18n.t(key, getCookie(e, 'lang') || i18n.fallbackLocale.toString()) | ||
}) | ||
|
||
declare module 'h3' { | ||
interface H3EventContext { | ||
$t: typeof i18n.t | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { test, expect, describe } from 'vitest' | ||
import { fileURLToPath } from 'node:url' | ||
import { setup, createPage, url } from '../utils' | ||
import { getText } from '../helper' | ||
|
||
describe('#2220', async () => { | ||
await setup({ | ||
rootDir: fileURLToPath(new URL(`../fixtures/issues/2220`, import.meta.url)) | ||
}) | ||
|
||
test('message-compiler work on server-side', async () => { | ||
const home = url('/') | ||
const page = await createPage() | ||
await page.goto(home) | ||
|
||
expect(await getText(page, '#app')).include('PROD [ "Test" ]') | ||
expect(await getText(page, '#app')).include(`yeah! it's finally working in prod too`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { assign } from '@intlify/shared' | ||
import { getFeatureFlags } from './bundler' | ||
|
||
import type { Nuxt } from '@nuxt/schema' | ||
|
||
export async function setupNitro(nuxt: Nuxt) { | ||
if (nuxt.options.ssr) { | ||
if (!nuxt.options.nitro) { | ||
nuxt.options.nitro = {} | ||
} | ||
const nitroConfig = nuxt.options.nitro | ||
|
||
// vue-i18n feature flags configuration for server-side (server api, server middleware, etc...) | ||
nitroConfig.replace = assign(nitroConfig.replace || {}, getFeatureFlags()) | ||
} | ||
} |