mirror of
https://github.com/an-anime-team/an-anime-game-launcher.git
synced 2024-12-30 05:28:15 +03:00
Improved Locales.system()
method according to the locale docs
This commit is contained in:
parent
8c4c41fa3b
commit
ebfb8b4db2
1 changed files with 19 additions and 5 deletions
|
@ -44,17 +44,31 @@ export default class Locales
|
|||
}
|
||||
|
||||
/**
|
||||
* Get system-used ($LANG) locale name, or {fallback}
|
||||
* Get system-used ($LC_ALL / $LC_MESSAGES / $LANG) locale name, or {fallback}
|
||||
* if system-used locale is not supported
|
||||
*
|
||||
* From "man locale.7":
|
||||
*
|
||||
* 1. If there is a non-null environment variable LC_ALL, the value of LC_ALL is used.
|
||||
* 2. If an environment variable with the same name as one of the categories above exists and is non-null, its value is used for that category.
|
||||
* 3. If there is a non-null environment variable LANG, the value of LANG is used.
|
||||
*/
|
||||
public static system(fallback: AvailableLocales = 'en-us'): Promise<AvailableLocales>
|
||||
{
|
||||
return new Promise(async (resolve) => {
|
||||
let locale = await Neutralino.os.getEnv('LANG');
|
||||
let locale = await Neutralino.os.getEnv('LC_ALL');
|
||||
|
||||
locale = locale.substring(0, locale.indexOf('.')).toLocaleLowerCase().replace('_', '-');
|
||||
// If $LC_ALL is empty - trying to get $LC_MESSAGES
|
||||
if (!locale)
|
||||
locale = await Neutralino.os.getEnv('LC_MESSAGES');
|
||||
|
||||
resolve(this.fallback(locale as AvailableLocales, fallback));
|
||||
// If $LC_MESSAGES is empty - getting $LANG
|
||||
// If it's empty - than {fallback} will be used by the this.fallback() method
|
||||
if (!locale)
|
||||
locale = await Neutralino.os.getEnv('LANG');
|
||||
|
||||
// "en_US.UTF-8" -> "en-us"
|
||||
resolve(this.fallback(locale.substring(0, locale.indexOf('.')).toLowerCase().replace('_', '-') as AvailableLocales, fallback));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue