Skip to content

Commit d824b8a

Browse files
committed
Clean up structure
1 parent b9a2243 commit d824b8a

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

lib/get-user-locale.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
export * from './get-user-locale'
1+
type NativeModule = {
2+
getLocaleInfoEx: (
3+
lpLocaleName: string | null,
4+
LCType: number,
5+
lpLCData: Buffer
6+
) => number
7+
}
8+
9+
// The native binary will be loaded lazily to avoid any possible crash at start
10+
// time, which are harder to trace.
11+
let _nativeModule: NativeModule | undefined = undefined
12+
13+
function getNativeModule() {
14+
if (_nativeModule === undefined && process.platform === 'win32') {
15+
_nativeModule = require('bindings')('win32-user-locale.node')
16+
}
17+
18+
return _nativeModule
19+
}
20+
21+
const LOCALE_SNAME = 0x0000005c
22+
23+
export function getUserLocale(): string | undefined {
24+
const buf = Buffer.alloc(256)
25+
const result = getNativeModule()?.getLocaleInfoEx(null, LOCALE_SNAME, buf)
26+
27+
return result !== undefined && result > 0
28+
? buf.toString('utf16le', 0, (result - 1) * 2)
29+
: undefined
30+
}

test/smoke-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getUserLocale } from '../lib/get-user-locale'
1+
import { getUserLocale } from '../lib/'
22

33
describe('getUserLocale', () => {
44
it('works', () => {

0 commit comments

Comments
 (0)