Skip to content

Commit 99fca5d

Browse files
authored
fix: use @intlify/utils in getLocale (#68)
1 parent 1b26283 commit 99fca5d

File tree

8 files changed

+46
-51
lines changed

8 files changed

+46
-51
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@intlify/bundle-utils": "^7.3.0",
2222
"@intlify/core": "^9.4.1",
2323
"@intlify/shared": "^9.4.1",
24+
"@intlify/utils": "0.4.2",
2425
"@vue/compiler-sfc": "^3.3.4",
2526
"colorette": "^2.0.20",
2627
"cosmiconfig": "^7.1.0",

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/env.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare namespace NodeJS {
2+
interface ProcessEnv {
3+
LC_ALL?: string
4+
LC_MESSAGES?: string
5+
LANG?: string
6+
LANGUAGE?: string
7+
}
8+
}

src/i18n.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'pathe'
22
import { promises as fs } from 'fs'
33
import { isString } from '@intlify/shared'
44
import { createCoreContext, translate } from '@intlify/core'
5+
import { getNavigatorLanguage } from '@intlify/utils/node'
56
import createDebug from 'debug'
67
import i18nResourceSchema from '../locales/en.json'
78

@@ -48,13 +49,8 @@ async function loadI18nResources(): Promise<
4849
)
4950
}
5051

51-
export function getLocale(env?: Record<string, unknown>): Locale {
52-
env = env || process.env
53-
const raw =
54-
((env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE) as string) ||
55-
DEFAULT_LOCALE
56-
debug('getLocale', raw)
57-
return raw.replace(/\.UTF\-8/g, '').replace(/_/g, '-')
52+
export function getLocale(): Locale {
53+
return getNavigatorLanguage() || DEFAULT_LOCALE
5854
}
5955

6056
export function t<Key extends keyof I18nReousrceSchema>(

test/commands/annotate.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { initI18n } from '../../src/i18n'
1010
const __dirname = dirname(fileURLToPath(import.meta.url))
1111
const CWD = resolve(__dirname, '../../')
1212

13-
let orgCwd
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
let orgCwd: any
1415
beforeEach(async function () {
1516
await initI18n()
1617
orgCwd = process.cwd

test/commands/compile.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { initI18n } from '../../src/i18n'
1010
const __dirname = dirname(fileURLToPath(import.meta.url))
1111
const CWD = resolve(__dirname, '../../')
1212

13-
let orgCwd
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
let orgCwd: any
1415
beforeEach(async () => {
1516
await initI18n()
1617
orgCwd = process.cwd

test/i18n.test.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,23 @@
1-
import { assert, beforeEach, afterEach, describe, it } from 'vitest'
1+
import { afterEach, describe, test, vi, expect } from 'vitest'
22
import { getLocale } from '../src/i18n'
33

4-
let ORG_ENV = {}
5-
6-
beforeEach(function () {
7-
ORG_ENV = process.env
8-
process.env = {}
9-
})
10-
11-
afterEach(function () {
12-
process.env = ORG_ENV
4+
afterEach(() => {
5+
vi.resetAllMocks()
136
})
147

15-
describe('getLocale', function () {
16-
it('default', function () {
17-
assert.equal(getLocale(), 'en-US')
18-
})
19-
20-
it('LC_ALL', function () {
21-
process.env['LC_ALL'] = 'ja_JP'
22-
assert.equal(getLocale(), 'ja-JP')
23-
})
24-
25-
it('LC_MESSAGES', function () {
26-
process.env['LC_MESSAGES'] = 'ja_JP'
27-
assert.equal(getLocale(), 'ja-JP')
28-
})
29-
30-
it('LANG', function () {
31-
process.env['LANG'] = 'ja_JP'
32-
assert.equal(getLocale(), 'ja-JP')
33-
})
34-
35-
it('LANGUAGE', function () {
36-
process.env['LANGUAGE'] = 'ja_JP'
37-
assert.equal(getLocale(), 'ja-JP')
8+
describe('getLocale', () => {
9+
test('default value', () => {
10+
vi.spyOn(process, 'env', 'get').mockReturnValue({})
11+
expect(getLocale()).toBe('en-US')
3812
})
3913

40-
it('BCP-47', function () {
41-
process.env['LC_ALL'] = 'ja-JP'
42-
assert.equal(getLocale(), 'ja-JP')
14+
test('basic', () => {
15+
vi.spyOn(process, 'env', 'get').mockReturnValue({
16+
LC_ALL: 'en-GB',
17+
LC_MESSAGES: 'en-US',
18+
LANG: 'ja-JP',
19+
LANGUAGE: 'en'
20+
})
21+
expect(getLocale()).toBe('en-GB')
4322
})
4423
})

tsconfig.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// "composite": true, /* Enable project compilation */
1818
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
1919
// "removeComments": true, /* Do not emit comments to output. */
20-
// "noEmit": true, /* Do not emit outputs. */
20+
"noEmit": true, /* Do not emit outputs. */
2121
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
2222
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
2323
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
@@ -38,8 +38,9 @@
3838
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
3939
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
4040

41+
"allowImportingTsExtensions": true,
4142
/* Module Resolution Options */
42-
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43+
"moduleResolution": "Bundler", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
4344
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
4445
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
4546
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
@@ -64,8 +65,5 @@
6465
/* Experimental Options */
6566
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
6667
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
67-
},
68-
"include": [
69-
"src/**/*"
70-
],
68+
}
7169
}

0 commit comments

Comments
 (0)