Skip to content

Commit

Permalink
fix: unexpected en-US
Browse files Browse the repository at this point in the history
  • Loading branch information
Teages committed Jan 2, 2024
1 parent 130a7fc commit 12f9742
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineNuxtModule, addPlugin, createResolver, addTemplate, addTypeTemplate, addImports } from '@nuxt/kit'
import { defineNuxtModule, addPlugin, createResolver, addTemplate, addTypeTemplate, addImports, useLogger } from '@nuxt/kit'
import type { LocaleOptions } from './options'
import { genAvailableLocalesCode, genLazyImportLangCode } from './gen'

Expand All @@ -10,11 +10,7 @@ export default defineNuxtModule<LocaleOptions>({
// Default configuration options of the Nuxt module
defaults: {
langDir: 'locales',
lang: {
'en-US': {
name: 'English'
}
}
lang: {}
},
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
Expand All @@ -23,6 +19,15 @@ export default defineNuxtModule<LocaleOptions>({
options.langDir
))

const logger = useLogger('nuxt-i18n-lite')

const lang = options.lang

if (Object.keys(lang).length === 0) {
lang['en-US'] = { name: 'English' }
logger.warn('No language is configured, added English (en-US) as default.')
}

const available = Object.entries(options.lang).map(([code, lang]) => {
return {
code,
Expand Down
4 changes: 2 additions & 2 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Basic', async () => {
it('with no accept-language', async () => {
// Get response to a server-rendered page with `$fetch`.
const html = await $fetch('/')
check(html, 'en-US')
check(html, 'en')
})

it('with accept-language', async () => {
Expand All @@ -34,7 +34,7 @@ describe('Basic', async () => {
})

function check(html: string, locale: string) {
if (locale === 'en-US') {
if (locale === 'en') {
expect(html).toContain(`locale: ${locale}`)
expect(html).toContain(`general.hello: Hello`)
expect(html).toContain(`general.deep.hello: Deep: Hello`)
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default defineNuxtConfig({
modules: ['../../../src/module'],
locale: {
lang: {
'en-US': { name: 'English' },
'en': { name: 'English' },
'zh-CN': { name: '中文(简体)' },
'ja-JP': { name: '日本語' },
},
Expand Down

0 comments on commit 12f9742

Please sign in to comment.