diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 10d4a0c1d3679..a7304eebe4b81 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -43,66 +43,6 @@ namespace ts { } } - /** - * Checks to see if the locale is in the appropriate format, - * and if it is, attempts to set the appropriate language. - */ - function validateLocaleAndSetLanguage(locale: string, errors: Diagnostic[]): boolean { - const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase()); - - if (!matchResult) { - errors.push(createCompilerDiagnostic(Diagnostics.Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1, "en", "ja-jp")); - return false; - } - - const language = matchResult[1]; - const territory = matchResult[3]; - - // First try the entire locale, then fall back to just language if that's all we have. - // Either ways do not fail, and fallback to the English diagnostic strings. - if (!trySetLanguageAndTerritory(language, territory, errors)) { - trySetLanguageAndTerritory(language, undefined, errors); - } - - return true; - } - - function trySetLanguageAndTerritory(language: string, territory: string, errors: Diagnostic[]): boolean { - const compilerFilePath = normalizePath(sys.getExecutingFilePath()); - const containingDirectoryPath = getDirectoryPath(compilerFilePath); - - let filePath = combinePaths(containingDirectoryPath, language); - - if (territory) { - filePath = filePath + "-" + territory; - } - - filePath = sys.resolvePath(combinePaths(filePath, "diagnosticMessages.generated.json")); - - if (!sys.fileExists(filePath)) { - return false; - } - - // TODO: Add codePage support for readFile? - let fileContents = ""; - try { - fileContents = sys.readFile(filePath); - } - catch (e) { - errors.push(createCompilerDiagnostic(Diagnostics.Unable_to_open_file_0, filePath)); - return false; - } - try { - ts.localizedDiagnosticMessages = JSON.parse(fileContents); - } - catch (e) { - errors.push(createCompilerDiagnostic(Diagnostics.Corrupted_locale_file_0, filePath)); - return false; - } - - return true; - } - function countLines(program: Program): number { let count = 0; forEach(program.getSourceFiles(), file => { @@ -263,7 +203,7 @@ namespace ts { reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--locale"), /* host */ undefined); return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped); } - validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors); + validateLocaleAndSetLanguage(commandLine.options.locale, sys, commandLine.errors); } // If there are any errors due to command line parsing and/or diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 546416884f1dc..09318fbe41ddd 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -472,15 +472,15 @@ namespace ts { export function getTextOfPropertyName(name: PropertyName): string { switch (name.kind) { - case SyntaxKind.Identifier: - return (name).text; - case SyntaxKind.StringLiteral: - case SyntaxKind.NumericLiteral: - return (name).text; - case SyntaxKind.ComputedPropertyName: - if (isStringOrNumericLiteral((name).expression)) { - return ((name).expression).text; - } + case SyntaxKind.Identifier: + return (name).text; + case SyntaxKind.StringLiteral: + case SyntaxKind.NumericLiteral: + return (name).text; + case SyntaxKind.ComputedPropertyName: + if (isStringOrNumericLiteral((name).expression)) { + return ((name).expression).text; + } } return undefined; @@ -4554,4 +4554,71 @@ namespace ts { return flags; } + + /** + * Checks to see if the locale is in the appropriate format, + * and if it is, attempts to set the appropriate language. + */ + export function validateLocaleAndSetLanguage( + locale: string, + sys: { getExecutingFilePath(): string, resolvePath(path: string): string, fileExists(fileName: string): boolean, readFile(fileName: string): string }, + errors?: Diagnostic[]) { + const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase()); + + if (!matchResult) { + if (errors) { + errors.push(createCompilerDiagnostic(Diagnostics.Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1, "en", "ja-jp")); + } + return; + } + + const language = matchResult[1]; + const territory = matchResult[3]; + + // First try the entire locale, then fall back to just language if that's all we have. + // Either ways do not fail, and fallback to the English diagnostic strings. + if (!trySetLanguageAndTerritory(language, territory, errors)) { + trySetLanguageAndTerritory(language, /*territory*/ undefined, errors); + } + + function trySetLanguageAndTerritory(language: string, territory: string, errors?: Diagnostic[]): boolean { + const compilerFilePath = normalizePath(sys.getExecutingFilePath()); + const containingDirectoryPath = getDirectoryPath(compilerFilePath); + + let filePath = combinePaths(containingDirectoryPath, language); + + if (territory) { + filePath = filePath + "-" + territory; + } + + filePath = sys.resolvePath(combinePaths(filePath, "diagnosticMessages.generated.json")); + + if (!sys.fileExists(filePath)) { + return false; + } + + // TODO: Add codePage support for readFile? + let fileContents = ""; + try { + fileContents = sys.readFile(filePath); + } + catch (e) { + if (errors) { + errors.push(createCompilerDiagnostic(Diagnostics.Unable_to_open_file_0, filePath)); + } + return false; + } + try { + ts.localizedDiagnosticMessages = JSON.parse(fileContents); + } + catch (e) { + if (errors) { + errors.push(createCompilerDiagnostic(Diagnostics.Corrupted_locale_file_0, filePath)); + } + return false; + } + + return true; + } + } } diff --git a/src/server/server.ts b/src/server/server.ts index 220b0d90c65e6..081673958d6ce 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -577,6 +577,11 @@ namespace ts.server { } } + const localeStr = findArgument("--locale"); + if (localeStr) { + validateLocaleAndSetLanguage(localeStr, sys); + } + const useSingleInferredProject = hasArgument("--useSingleInferredProject"); const disableAutomaticTypingAcquisition = hasArgument("--disableAutomaticTypingAcquisition"); const telemetryEnabled = hasArgument(Arguments.EnableTelemetry);