@boost/translate
Version:
Package and application level translations made easy.
40 lines (39 loc) • 1.1 kB
JavaScript
import { osLocaleSync } from 'os-locale';
import { debug } from './debug.mjs';
class LocaleDetector {
constructor() {
this.locale = 'en';
this.type = 'languageDetector';
}
init() {
// We don't need this but is required by the interface
}
cacheUserLanguage(locale) {
this.locale = locale;
}
detect() {
if (this.locale) {
debug('Locale "%s" manually provided', this.locale);
return this.locale;
}
return this.detectFromArgv() ?? this.detectFromOS();
}
detectFromArgv() {
const args = process.argv;
const index = args.indexOf('--locale');
const nextIndex = index + 1;
if (index >= 0 && args[nextIndex] && !args[nextIndex].startsWith('-')) {
const locale = args[nextIndex];
debug('Locale "%s" detected from --locale option', locale);
return locale;
}
return undefined;
}
detectFromOS() {
const locale = osLocaleSync().replace(/_/gu, '-');
debug('Locale "%s" detected from operating system', locale);
return locale;
}
}
export { LocaleDetector };
//# sourceMappingURL=LocaleDetector.mjs.map