mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
feat: add app.getPreferredSystemLanguages() API (#36035)
* feat: add app.getSystemLanguage() API * Change the API to getPreferredSystemLanguages * Fix test * Clarify docs and add Linux impl * Remove USE_GLIB * Don't add C to list * Remove examples since there's a lot of edge cases * Fix lint * Add examples * Fix compile error * Apply PR feedback * Update the example
This commit is contained in:
@@ -124,6 +124,19 @@ describe('app module', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('app.getPreferredSystemLanguages()', () => {
|
||||
ifit(process.platform !== 'linux')('should not be empty', () => {
|
||||
expect(app.getPreferredSystemLanguages().length).to.not.equal(0);
|
||||
});
|
||||
|
||||
ifit(process.platform === 'linux')('should be empty or contain C entry', () => {
|
||||
const languages = app.getPreferredSystemLanguages();
|
||||
if (languages.length) {
|
||||
expect(languages).to.not.include('C');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('app.getLocaleCountryCode()', () => {
|
||||
it('should be empty or have length of two', () => {
|
||||
const localeCountryCode = app.getLocaleCountryCode();
|
||||
|
||||
@@ -375,6 +375,7 @@ describe('command line switches', () => {
|
||||
describe('--lang switch', () => {
|
||||
const currentLocale = app.getLocale();
|
||||
const currentSystemLocale = app.getSystemLocale();
|
||||
const currentPreferredLanguages = JSON.stringify(app.getPreferredSystemLanguages());
|
||||
const testLocale = async (locale: string, result: string, printEnv: boolean = false) => {
|
||||
const appPath = path.join(fixturesPath, 'api', 'locale-check');
|
||||
const args = [appPath, `--set-lang=${locale}`];
|
||||
@@ -397,9 +398,9 @@ describe('command line switches', () => {
|
||||
expect(output).to.equal(result);
|
||||
};
|
||||
|
||||
it('should set the locale', async () => testLocale('fr', `fr|${currentSystemLocale}`));
|
||||
it('should set the locale with country code', async () => testLocale('zh-CN', `zh-CN|${currentSystemLocale}`));
|
||||
it('should not set an invalid locale', async () => testLocale('asdfkl', `${currentLocale}|${currentSystemLocale}`));
|
||||
it('should set the locale', async () => testLocale('fr', `fr|${currentSystemLocale}|${currentPreferredLanguages}`));
|
||||
it('should set the locale with country code', async () => testLocale('zh-CN', `zh-CN|${currentSystemLocale}|${currentPreferredLanguages}`));
|
||||
it('should not set an invalid locale', async () => testLocale('asdfkl', `${currentLocale}|${currentSystemLocale}|${currentPreferredLanguages}`));
|
||||
|
||||
const lcAll = String(process.env.LC_ALL);
|
||||
ifit(process.platform === 'linux')('current process has a valid LC_ALL env', async () => {
|
||||
|
||||
2
spec/fixtures/api/locale-check/main.js
vendored
2
spec/fixtures/api/locale-check/main.js
vendored
@@ -9,7 +9,7 @@ app.whenReady().then(() => {
|
||||
if (process.argv[3] === '--print-env') {
|
||||
process.stdout.write(String(process.env.LC_ALL));
|
||||
} else {
|
||||
process.stdout.write(`${app.getLocale()}|${app.getSystemLocale()}`);
|
||||
process.stdout.write(`${app.getLocale()}|${app.getSystemLocale()}|${JSON.stringify(app.getPreferredSystemLanguages())}`);
|
||||
}
|
||||
process.stdout.end();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user