Skip to content

Commit c71783a

Browse files
authoredSep 6, 2020
feat: i18n for ESM and Deno (#1735)
1 parent d360577 commit c71783a

File tree

3 files changed

+14
-42
lines changed

3 files changed

+14
-42
lines changed
 

‎lib/platform-shims/deno.ts

+7-21
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33

44
import { assertNotEquals, assertStrictEquals } from 'https://deno.land/std/testing/asserts.ts'
55
import { basename, dirname, extname, posix } from 'https://deno.land/std/path/mod.ts'
6-
import { sprintf } from 'https://deno.land/std/fmt/printf.ts'
76

87
import cliui from 'https://deno.land/x/cliui@v7.0.0-deno/deno.ts'
98
import escalade from 'https://deno.land/x/escalade@v3.0.3/sync.ts'
109
import Parser from 'https://deno.land/x/yargs_parser@v19.0.1-deno/deno.ts'
10+
import y18n from 'https://deno.land/x/y18n@v5.0.0-deno/deno.ts'
1111
import { YError } from '../../build/lib/yerror.js'
1212

1313
const REQUIRE_ERROR = 'require is not supported by ESM'
1414
const REQUIRE_DIRECTORY_ERROR = 'loading a directory of commands is not supported yet for ESM'
1515

1616
// Deno removes argv[0] and argv[1 from Deno.args:
1717
const argv = ['deno run', ...Deno.args]
18+
const __dirname = new URL('.', import.meta.url).pathname
1819

1920
// Yargs supports environment variables with prefixes, e.g., MY_APP_FOO,
2021
// MY_APP_BAR. Environment variables are also used to detect locale.
2122
let cwd: string = ''
2223
let env: {[key: string]: string} = {}
2324
try {
24-
cwd = Deno.cwd()
2525
env = Deno.env.toObject()
26+
cwd = Deno.cwd()
2627
} catch (err) {
2728
if (err.name !== 'PermissionDenied') {
2829
throw err
@@ -94,23 +95,8 @@ export default {
9495
stringWidth: (str: string) => {
9596
return [...str].length
9697
},
97-
y18n: {
98-
__: (...str: string[]) => {
99-
if (str.length === 0) return ''
100-
const args = str.slice(1)
101-
return sprintf(str[0], ...args)
102-
},
103-
__n: (str1: string, str2: string, count: number, ...args: string[]) => {
104-
if (count === 1) {
105-
return sprintf(str1, ...args)
106-
} else {
107-
return sprintf(str2, ...args)
108-
}
109-
},
110-
getLocale: (): string => {
111-
return 'en_US'
112-
},
113-
setLocale: () => {},
114-
updateLocale: () => {}
115-
}
98+
y18n: y18n({
99+
directory: posix.resolve(__dirname, '../../locales'),
100+
updateFiles: false
101+
})
116102
}

‎lib/platform-shims/esm.mjs

+6-20
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import Parser from 'yargs-parser'
1010
import { basename, dirname, extname, relative, resolve } from 'path'
1111
import { getProcessArgvBin } from '../../build/lib/utils/process-argv.js'
1212
import { YError } from '../../build/lib/yerror.js'
13+
import y18n from 'y18n'
1314

1415
const REQUIRE_ERROR = 'require is not supported by ESM'
1516
const REQUIRE_DIRECTORY_ERROR = 'loading a directory of commands is not supported yet for ESM'
1617

1718
const mainFilename = fileURLToPath(import.meta.url).split('node_modules')[0]
19+
const __dirname = fileURLToPath(import.meta.url)
1820

1921
export default {
2022
assert: {
@@ -57,24 +59,8 @@ export default {
5759
stringWidth: (str) => {
5860
return [...str].length
5961
},
60-
// TODO: replace this with y18n once it's ported to ESM:
61-
y18n: {
62-
__: (...str) => {
63-
if (str.length === 0) return ''
64-
const args = str.slice(1)
65-
return format(str[0], ...args)
66-
},
67-
__n: (str1, str2, count, ...args) => {
68-
if (count === 1) {
69-
return format(str1, ...args)
70-
} else {
71-
return format(str2, ...args)
72-
}
73-
},
74-
getLocale: () => {
75-
return 'en_US'
76-
},
77-
setLocale: () => {},
78-
updateLocale: () => {}
79-
}
62+
y18n: y18n({
63+
directory: resolve(__dirname, '../../../locales'),
64+
updateFiles: false
65+
})
8066
}

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"get-caller-file": "^2.0.5",
4141
"require-directory": "^2.1.1",
4242
"string-width": "^4.2.0",
43-
"y18n": "^4.0.0",
43+
"y18n": "^5.0.1",
4444
"yargs-parser": "^19.0.4"
4545
},
4646
"devDependencies": {

0 commit comments

Comments
 (0)
Please sign in to comment.