Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
s00d committed Oct 21, 2024
1 parent eef4452 commit dc11c94
Show file tree
Hide file tree
Showing 32 changed files with 103 additions and 19 deletions.
6 changes: 6 additions & 0 deletions client/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,15 @@ watch([selectedLocale, selectedFile], () => {
onDevtoolsClientConnected(async (client) => {
const rpc = client.devtools.extendClientRpc(RPC_NAMESPACE, {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
localesUpdated(updatedLocales: LocaleData[]) {
locales.value = updatedLocales
},
})
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const initialLocales = await rpc.getLocalesAndTranslations()
locales.value = initialLocales
isLoading.value = false
Expand All @@ -144,6 +148,8 @@ onDevtoolsClientConnected(async (client) => {
return
}
if (selectedLocale.value && selectedFile.value && newContent) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
rpc.saveTranslationContent(selectedLocale.value, selectedFile.value, newContent)
}
}, { deep: true })
Expand Down
16 changes: 16 additions & 0 deletions nuxt-i18n.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { PluginsInjections } from './src/runtime/plugins/01.plugin'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
declare module 'vue/types/vue' {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface Vue extends PluginsInjections { }
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
declare module '@nuxt/types' {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface NuxtAppOptions extends PluginsInjections { }
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface Context extends PluginsInjections { }
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"client:build": "nuxi generate client",
"client:dev": "nuxi dev client --port 3300",
"typecheck": "tsc --noEmit",
"typecheck:nuxt": "nuxt typecheck --no-emit",
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:serve": "vitepress serve docs",
Expand Down
4 changes: 2 additions & 2 deletions src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import { useNuxt } from '@nuxt/kit'
import { extendServerRpc, onDevToolsInitialized } from '@nuxt/devtools-kit'
import type { Resolver } from '@nuxt/kit'
import type { ModuleOptions } from './types'
import type { ModuleOptions, ModulePrivateOptionsExtend } from './types'

export interface ServerFunctions {
getLocalesAndTranslations: () => Promise<{ locale: string, files: string[], content: Record<string, unknown> }[]>
Expand Down Expand Up @@ -56,7 +56,7 @@ export function setupDevToolsUI(options: ModuleOptions, resolve: Resolver['resol
}
},
async getLocalesAndTranslations() {
const rootDirs = nuxt.options.runtimeConfig.i18nConfig?.rootDirs || [nuxt.options.rootDir]
const rootDirs = (nuxt.options.runtimeConfig.i18nConfig as ModulePrivateOptionsExtend)?.rootDirs || [nuxt.options.rootDir]
const localesData: { locale: string, files: string[], content: Record<string, unknown> }[] = []

for (const rootDir of rootDirs) {
Expand Down
3 changes: 2 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { watch } from 'chokidar'
import { setupDevToolsUI } from './devtools'
import { PageManager } from './page-manager'
import type { ModuleOptions, ModuleOptionsExtend, ModulePrivateOptionsExtend, Locale, PluralFunc, GlobalLocaleRoutes, Getter, LocaleCode } from './types'
import type { PluginsInjections } from './runtime/plugins/01.plugin'
import { LocaleManager } from './locale-manager'

declare module '@nuxt/schema' {
Expand Down Expand Up @@ -331,4 +332,4 @@ declare module '@nuxt/schema' {
interface NuxtHooks extends ModuleHooks {}
}

export type { Locale, PluralFunc, ModuleOptions, GlobalLocaleRoutes, Getter, LocaleCode }
export type { Locale, PluralFunc, ModuleOptions, GlobalLocaleRoutes, Getter, LocaleCode, PluginsInjections }
3 changes: 2 additions & 1 deletion src/runtime/components/i18n-link.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

<script lang="ts" setup>
import type { RouteLocationRaw } from 'vue-router'
import type { PluginsInjections } from '../../runtime/plugins/01.plugin'
import { useNuxtApp, computed, useRoute, useRouter } from '#imports'
const { $localeRoute } = useNuxtApp()
const { $localeRoute } = useNuxtApp().$i18n as PluginsInjections
interface Props {
to: RouteLocationRaw | string
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/components/i18n-switcher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import { ref, computed } from 'vue'
import type { CSSProperties } from 'vue'
import type { Locale } from '../../types'
import type { PluginsInjections } from '../../runtime/plugins/01.plugin'
import { useNuxtApp } from '#app'
import { useRoute } from '#imports'
Expand Down Expand Up @@ -67,7 +68,7 @@ const props = withDefaults(defineProps<Props>(), {
customIconStyle: () => ({}),
})
const { $localeRoute, $getLocales, $getLocale, $getLocaleName } = useNuxtApp()
const { $localeRoute, $getLocales, $getLocale, $getLocaleName } = useNuxtApp().$i18n as PluginsInjections
const locales = ref($getLocales())
const currentLocale = computed(() => $getLocale())
const currentLocaleName = computed(() => $getLocaleName())
Expand Down
11 changes: 7 additions & 4 deletions src/runtime/components/i18n-t.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { h, defineComponent } from 'vue'
import type { VNode, PropType } from 'vue'
import type { PluralFunc } from '../../types'
import type { PluginsInjections } from '../../runtime/plugins/01.plugin'
import { useNuxtApp } from '#app'
export default defineComponent({
Expand Down Expand Up @@ -43,21 +44,23 @@ export default defineComponent({
return () => {
const options: Record<string, string | number | boolean> = {}
const { $getLocale, $t, $tc } = useNuxtApp().$i18n as PluginsInjections
if (props.plural !== undefined) {
if (props.customPluralRule) {
return h(props.tag, { ...attrs, innerHTML: props.customPluralRule(
props.keypath,
Number.parseInt((props.plural).toString()),
useNuxtApp().$getLocale(),
useNuxtApp().$t,
$getLocale(),
$t,
) })
}
else {
return h(props.tag, { ...attrs, innerHTML: useNuxtApp().$tc(props.keypath, Number.parseInt(props.plural.toString())) })
return h(props.tag, { ...attrs, innerHTML: $tc(props.keypath, Number.parseInt(props.plural.toString())) })
}
}
const translation = (useNuxtApp().$t(props.keypath, { ...props.params, ...options }) ?? '').toString()
const translation = ($t(props.keypath, { ...props.params, ...options }) ?? '').toString()
if (props.hideIfEmpty && !translation.trim()) {
return props.defaultValue ?? null
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/composables/useLocaleHead.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { joinURL } from 'ufo'
import type { ModuleOptionsExtend } from '../../types'
import type { PluginsInjections } from '../plugins/01.plugin'
import { unref, useRoute, useRuntimeConfig, watch, onUnmounted, ref, useNuxtApp } from '#imports'

interface MetaLink {
Expand Down Expand Up @@ -33,7 +34,7 @@ export const useLocaleHead = ({ addDirAttribute = true, identifierAttribute = 'i

function updateMeta() {
const { defaultLocale, includeDefaultLocaleRoute } = useRuntimeConfig().public.i18nConfig as ModuleOptionsExtend
const { $getLocales, $getLocale } = useNuxtApp()
const { $getLocales, $getLocale } = useNuxtApp().$i18n as PluginsInjections

const route = useRoute()
const locale = unref($getLocale())
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/plugins/01.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,13 @@ export default defineNuxtPlugin(async (nuxtApp) => {
// @ts-expect-error
provideData.i18n = provideData

const $provideData = Object.fromEntries(
Object.entries(provideData).map(([key, value]) => [`$${key}`, value]),
)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
provideData.i18n = { ...provideData, ...$provideData }

return {
provide: provideData,
}
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/plugins/03.define.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import type { ModuleOptionsExtend } from '../../types'
import type { Translations } from '../plugins/01.plugin'
import type { PluginsInjections, Translations } from '../plugins/01.plugin'
import { defineNuxtPlugin, navigateTo, useNuxtApp, useRuntimeConfig } from '#app'
import { useRoute, useRouter } from '#imports'

Expand Down Expand Up @@ -85,8 +85,8 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
// Если текущая локаль есть в объекте locales
if (normalizedLocales[currentLocale]) {
const translation = normalizedLocales[currentLocale]
const nuxtApp = useNuxtApp()
nuxtApp.$mergeTranslations(translation)
const { $mergeTranslations } = useNuxtApp().$i18n as PluginsInjections
$mergeTranslations(translation)
}

// Если текущей локали нет в объекте locales
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/server/middleware/i18n-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { readFile } from 'node:fs/promises'
import { defineEventHandler } from 'h3'
import type { ModuleOptionsExtend, ModulePrivateOptionsExtend } from '../../../types'
import type { Translations } from '../../plugins/01.plugin'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { useRuntimeConfig, createError, useStorage } from '#imports'

// Рекурсивная функция для глубокого слияния объектов
Expand Down
2 changes: 2 additions & 0 deletions test/cookie-replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ test.use({
nuxt: {
rootDir: fileURLToPath(new URL('./fixtures/cookie', import.meta.url)),
nuxtConfig: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
localeCookie: 'user-change-cookie',
},
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineNuxtConfig({
MyModule,
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [
{ code: 'en', iso: 'en_EN', displayName: 'English' },
Expand Down
9 changes: 6 additions & 3 deletions test/fixtures/basic/pages/news/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@
</template>

<script lang="ts" setup>
import type { PluginsInjections } from '../../../../../src/runtime/plugins/01.plugin'
import { useNuxtApp } from '#app'
const { params } = useRoute()
const router = useRouter()
const { $switchLocaleRoute, $setI18nRouteParams, $localeRoute } = useI18n()
const { $switchLocaleRoute, $setI18nRouteParams, $localeRoute } = useNuxtApp().$i18n as PluginsInjections
const newsLink = computed(() => $localeRoute({
name: 'news-id',
Expand All @@ -82,11 +85,11 @@ const newsLink = computed(() => $localeRoute({
}))
const { data: news } = await useAsyncData(`articles-${params.id}`, async () => {
const response = await $fetch<{ metadata: { [key: string]: { id: string } } }>('/api/getNews', {
const response = await $fetch('/api/getNews', {
query: {
id: params.id,
},
})
}) as { metadata: { [key: string]: { id: string } } }
if (response?.metadata) {
$setI18nRouteParams(response?.metadata)
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/content/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default defineNuxtConfig({
],
compatibilityDate: '2024-04-03',
devtools: { enabled: true },
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [
{ code: 'en', iso: 'en-US', dir: 'ltr' },
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/cookie/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineNuxtConfig({
MyModule,
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [{ code: 'de' }, { code: 'en' }],
defaultLocale: 'en',
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/custom-regex-fail/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineNuxtConfig({
MyModule,
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [{ code: 'de' }, { code: 'en' }],
defaultLocale: 'en',
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/custom-regex/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineNuxtConfig({
MyModule,
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [
{ code: 'en-us', iso: 'en_EN' },
Expand Down
9 changes: 6 additions & 3 deletions test/fixtures/custom-regex/pages/news/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@
</template>

<script lang="ts" setup>
import type { PluginsInjections } from '../../../../../src/runtime/plugins/01.plugin'
import { useNuxtApp } from '#app'
const { params } = useRoute()
const router = useRouter()
const { $switchLocaleRoute, $setI18nRouteParams, $localeRoute } = useI18n()
const { $switchLocaleRoute, $setI18nRouteParams, $localeRoute } = useNuxtApp().$i18n as PluginsInjections
const newsLink = computed(() => $localeRoute({
name: 'news-id',
Expand All @@ -82,11 +85,11 @@ const newsLink = computed(() => $localeRoute({
}))
const { data: news } = await useAsyncData(`articles-${params.id}`, async () => {
const response = await $fetch<{ metadata: { [key: string]: { id: string } } }>('/api/getNews', {
const response = await $fetch('/api/getNews', {
query: {
id: params.id,
},
})
}) as { metadata: { [key: string]: { id: string } } }
if (response?.metadata) {
$setI18nRouteParams(response?.metadata)
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/fallback-locale/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineNuxtConfig({
MyModule,
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [{ code: 'de' }, { code: 'en' }],
defaultLocale: 'en',
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/hashmode/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default defineNuxtConfig({
hashMode: true,
},
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [
{ code: 'en', iso: 'en_EN' },
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/hook/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default defineNuxtConfig({
MyModule,
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [{ code: 'de' }, { code: 'en' }],
defaultLocale: 'en',
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/i18n-micro/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { defineNuxtConfig } from 'nuxt/config'

export default defineNuxtConfig({
modules: ['nuxt-i18n-micro'],
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
i18n: {
locales: [
{ code: 'en', iso: 'en_EN' },
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/i18n/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { defineNuxtConfig } from 'nuxt/config'

export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
i18n: {
defaultLocale: 'en',
lazy: true,
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/layer/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
export default defineNuxtConfig({
devtools: { enabled: true },
extends: '../basic',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [
{ code: 'ru', iso: 'ru_RU' },
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/named/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineNuxtConfig({
MyModule,
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [{ code: 'de' }, { code: 'en' }],
defaultLocale: 'de',
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/redirect/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineNuxtConfig({
MyModule,
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [
{ code: 'en', iso: 'en_EN' },
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/sitemap/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default defineNuxtConfig({
'@nuxtjs/sitemap',
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18n: {
locales: [
{ code: 'en', iso: 'en_EN' },
Expand Down
Loading

0 comments on commit dc11c94

Please sign in to comment.