Skip to content

Commit

Permalink
fix: locale resources not loaded on windows (#1607)
Browse files Browse the repository at this point in the history
* fix: locale resource not loaded on windows

* refactor

* fix: cannot locale relative base path

* fix: unit tests
  • Loading branch information
kazupon authored Nov 3, 2022
1 parent ad45be6 commit 494116d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
os: [ubuntu-latest]
timeout-minutes: 10

name: Use Node.js ${{ matrix.node-version }}
name: Lint on Node.js ${{ matrix.node-version }}
steps:
- name: Checkout codes
uses: actions/checkout@v3
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
os: [ubuntu-latest]
timeout-minutes: 10

name: Use Node.js ${{ matrix.node-version }}
name: Build on Node.js ${{ matrix.node-version }}
steps:
- name: Checkout codes
uses: actions/checkout@v3
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
os: [ubuntu-latest]
timeout-minutes: 15

name: Use Node.js ${{ matrix.node-version }}
name: Test on Node.js ${{ matrix.node-version }}
steps:
- name: Checkout codes
uses: actions/checkout@v3
Expand Down
25 changes: 20 additions & 5 deletions src/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isString, isRegExp, isFunction, isArray, isObject } from '@intlify/shar
import { generateJSON } from '@intlify/bundle-utils'
import { NUXT_I18N_MODULE_ID } from './constants'
import { genImport, genSafeVariableName, genDynamicImport } from 'knitwork'
import { parse as parsePath } from 'pathe'
import { parse as parsePath, normalize } from 'pathe'

import type { NuxtI18nOptions, NuxtI18nInternalOptions, LocaleInfo } from './types'
import type { NuxtI18nOptionsDefault } from './constants'
Expand All @@ -23,6 +23,7 @@ const debug = createDebug('@nuxtjs/i18n:gen')
export function generateLoaderOptions(
lazy: NonNullable<NuxtI18nOptions['lazy']>,
langDir: NuxtI18nOptions['langDir'],
localesRelativeBase: string,
options: LoaderOptions = {},
dev = true
) {
Expand All @@ -48,9 +49,14 @@ export function generateLoaderOptions(
}

const importMapper = new Map<string, string>()
for (const { code, path } of syncLocaleFiles) {
for (const { code, path, file } of syncLocaleFiles) {
importMapper.set(code, genSafeVariableName(`locale_${code}`))
genCode += `${genImport(path, genSafeVariableName(`locale_${code}`))}\n`
let loadPath = path
if (file && langDir) {
loadPath = resolveLocaleRelativePath(localesRelativeBase, langDir, file)
}
// TODO: import assertions (we need to support it on kitwork)
genCode += `${genImport(loadPath, genSafeVariableName(`locale_${code}`))}\n`
}

// prettier-ignore
Expand Down Expand Up @@ -95,8 +101,13 @@ export function generateLoaderOptions(
for (const { code } of syncLocaleFiles) {
codes += ` ${toCode(code)}: () => Promise.resolve(${importMapper.get(code)}),\n`
}
for (const { code, path } of asyncLocaleFiles) {
codes += ` ${toCode(code)}: ${genDynamicImport(path, { comment: `webpackChunkName: "lang-${code}"` })},\n`
for (const { code, path, file } of asyncLocaleFiles) {
let loadPath = path
if (file && langDir) {
loadPath = resolveLocaleRelativePath(localesRelativeBase, langDir, file)
}
// TODO: import assertions (we need to support it on kitwork)
codes += ` ${toCode(code)}: ${genDynamicImport(loadPath, { comment: `webpackChunkName: "lang-${code}"` })},\n`
}
}
codes += `}\n`
Expand All @@ -114,6 +125,10 @@ export function generateLoaderOptions(
return genCode
}

function resolveLocaleRelativePath(relativeBase: string, langDir: string, file: string) {
return normalize(`${relativeBase}/${langDir}/${file}`)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function generateVueI18nOptions(options: Record<string, any>, dev: boolean): string {
let genCode = 'Object({'
Expand Down
6 changes: 4 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import createDebug from 'debug'
import { isBoolean, isObject, isString } from '@intlify/shared'
import { defineNuxtModule, isNuxt2, isNuxt3, getNuxtVersion, addPlugin, addTemplate, addImports } from '@nuxt/kit'
import { resolve } from 'pathe'
import { resolve, relative } from 'pathe'
import { setupAlias } from './alias'
import { setupPages } from './pages'
import { extendMessages } from './messages'
Expand Down Expand Up @@ -140,13 +140,15 @@ export default defineNuxtModule<NuxtI18nOptions>({
})

// for loading options
const localesRelativeBasePath = relative(nuxt.options.buildDir, nuxt.options.rootDir)
addTemplate({
filename: 'i18n.options.mjs',
write: true,
getContents: () => {
return generateLoaderOptions(
options.lazy,
langPath,
options.langDir,
localesRelativeBasePath,
{
localeCodes,
localeInfo,
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/gen.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Vitest Snapshot v1

exports[`basic 1`] = `
"import locale_en from \\"./locales/en.json\\";
import locale_ja from \\"./locales/ja.json\\";
"import locale_en from \\"../locales/en.json\\";
import locale_ja from \\"../locales/ja.json\\";
export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
export const localeMessages = {
Expand Down Expand Up @@ -30,12 +30,12 @@ export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
`;

exports[`lazy 1`] = `
"import locale_en from \\"./locales/en.json\\";
"import locale_en from \\"../locales/en.json\\";
export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
export const localeMessages = {
\\"en\\": () => Promise.resolve(locale_en),
\\"ja\\": () => import(\\"./locales/ja.json\\" /* webpackChunkName: \\"lang-ja\\" */),
\\"ja\\": () => import(\\"../locales/ja.json\\" /* webpackChunkName: \\"lang-ja\\" */),
}
export const additionalMessages = Object({})
Expand All @@ -56,8 +56,8 @@ export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
`;

exports[`vueI18n: path 1`] = `
"import locale_en from \\"./locales/en.json\\";
import locale_ja from \\"./locales/ja.json\\";
"import locale_en from \\"../locales/en.json\\";
import locale_ja from \\"../locales/ja.json\\";
export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
export const localeMessages = {
Expand Down
12 changes: 7 additions & 5 deletions test/gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ const LOCALE_CODES = ['en', 'ja', 'fr']
const LOCALE_INFO = [
{
code: 'en',
path: './locales/en.json'
file: 'en.json',
path: '/path/to/en.json'
},
{
code: 'ja',
path: './locales/ja.json'
file: 'ja.json',
path: '/path/to/ja.json'
}
]
const ADDITIONAL_MESSAGES = {
Expand Down Expand Up @@ -61,7 +63,7 @@ function validateSyntax(code: string): boolean {
}

it('basic', () => {
const code = generateLoaderOptions(false, '/path/to/locales', {
const code = generateLoaderOptions(false, 'locales', '..', {
localeCodes: LOCALE_CODES,
localeInfo: LOCALE_INFO,
additionalMessages: {},
Expand All @@ -74,7 +76,7 @@ it('basic', () => {
})

it('lazy', () => {
const code = generateLoaderOptions(true, '/path/to/locales', {
const code = generateLoaderOptions(true, 'locales', '..', {
localeCodes: LOCALE_CODES,
localeInfo: LOCALE_INFO,
additionalMessages: {},
Expand All @@ -86,7 +88,7 @@ it('lazy', () => {
})

it('vueI18n: path', () => {
const code = generateLoaderOptions(false, '/path/to/locales', {
const code = generateLoaderOptions(false, 'locales', '..', {
localeCodes: LOCALE_CODES,
localeInfo: LOCALE_INFO,
additionalMessages: ADDITIONAL_MESSAGES,
Expand Down

0 comments on commit 494116d

Please sign in to comment.