Skip to content

Commit

Permalink
Fixes #4170: Provide a setting to define the locale
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaeumer committed Mar 15, 2016
1 parent 90e5c71 commit 06ca87b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ var app = require('electron').app;
var fs = require('fs');
var path = require('path');

function stripComments(content) {
var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
var result = content.replace(regexp, function (match, m1, m2, m3, m4) {
// Only one of m1, m2, m3, m4 matches
if (m3) {
// A block comment. Replace with nothing
return '';
}
else if (m4) {
// A line comment. If it ends in \r?\n then keep it.
var length_1 = m4.length;
if (length_1 > 2 && m4[length_1 - 1] === '\n') {
return m4[length_1 - 2] === '\r' ? '\r\n' : '\n';
}
else {
return '';
}
}
else {
// We match a string
return match;
}
});
return result;
};


// Duplicated in ../index.html for the renderes.
function getNLSConfiguration() {
var locale = undefined;
Expand All @@ -23,6 +50,21 @@ function getNLSConfiguration() {
}
}

if (!locale) {
var userData = app.getPath('userData');
localeConfig = path.join(userData, 'User', 'locale.json');
if (fs.existsSync(localeConfig)) {
try {
var content = stripComments(fs.readFileSync(localeConfig, 'utf8'));
value = JSON.parse(content).locale;
if (value && typeof value === 'string') {
locale = value;
}
} catch (e) {
}
}
}

if (locale === 'pseudo') {
return { locale: locale, availableLanguages: {}, pseudo: true }
}
Expand Down Expand Up @@ -72,10 +114,9 @@ app.on('open-file', function(event, path) {
global.macOpenFiles.push(path);
});

var nlsConfig = getNLSConfiguration();
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);

// Load our code once ready
app.once('ready', function() {
var nlsConfig = getNLSConfiguration();
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);
require('./bootstrap-amd').bootstrap('vs/workbench/electron-main/main');
});
64 changes: 64 additions & 0 deletions src/vs/workbench/browser/actions/configureLocale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';

import { TPromise } from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import * as Path from 'vs/base/common/paths';
import URI from 'vs/base/common/uri';
import * as Labels from 'vs/base/common/labels';
import { Action } from 'vs/base/common/actions';

import { Registry } from 'vs/platform/platform';
import { IWorkbenchActionRegistry, Extensions } from 'vs/workbench/common/actionRegistry';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IWorkspaceContextService } from 'vs/workbench/services/workspace/common/contextService';
import { IEditor } from 'vs/platform/editor/common/editor';
import { IFileService } from 'vs/platform/files/common/files';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';


class ConfigureLocaleAction extends Action {
public static ID = 'workbench.action.configureLocale';
public static LABEL = nls.localize('configureLocale', "Configure Locale");

private static DEFAULT_CONTENT: string = [
'{',
`\t// ${nls.localize('displayLanguage', 'Defines VSCode\'s display language.')}`,
`\t// ${nls.localize('restart', 'Changing the value requires to restart VSCode.')}`,
`\t"locale":"en-US"`,
'}'
].join('\n');

constructor(id, label,
@IFileService private fileService: IFileService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@IWorkbenchEditorService private editorService: IWorkbenchEditorService
) {
super(id, label);
}

public run(event?: any): TPromise<IEditor> {
let file = URI.file(Path.join(this.contextService.getConfiguration().env.appSettingsHome, 'locale.json'));
return this.fileService.resolveFile(file).then(null, (error) => {
return this.fileService.createFile(file, ConfigureLocaleAction.DEFAULT_CONTENT);
}).then((stat) => {
if (!stat) {
return;
}
return this.editorService.openEditor({
resource: stat.resource,
options: {
forceOpen: true
}
});
}, (error) => {
throw new Error(nls.localize('fail.createSettings', "Unable to create '{0}' ({1}).", Labels.getPathLabel(file, this.contextService), error));
});
}
}

let workbenchActionsRegistry = <IWorkbenchActionRegistry>Registry.as(Extensions.WorkbenchActions);
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(ConfigureLocaleAction, ConfigureLocaleAction.ID, ConfigureLocaleAction.LABEL));
1 change: 1 addition & 0 deletions src/vs/workbench/workbench.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ define([
'vs/workbench/browser/actions/triggerNavigation',
'vs/workbench/browser/actions/showPerformanceBox',
'vs/workbench/browser/actions/openSettings',
'vs/workbench/browser/actions/configureLocale',

'vs/workbench/parts/quickopen/browser/quickopen.contribution',

Expand Down

0 comments on commit 06ca87b

Please sign in to comment.