Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create localization of strings (#2757)
Creation of a new module - localize. This doesn't use the vscode-nls and vscode-nls-dev as they interfere with our code coverage. Adding new strings requires adding them to the localize.ts and the localize.nls.<locale>.json files. Future work would be to translate the nls.json files into xlf files for the loc team to own. We'd then write a build time task to turn the xlf files into the output nls.<locale>.json files so that this code works the same. For #463
- Loading branch information
1 parent
2d0a09d
commit 027832d
Showing
8 changed files
with
158 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
{ | ||
"enabled": false, | ||
"relativeSourcePath": "../client", | ||
"relativeCoverageDir": "../../coverage", | ||
"ignorePatterns": [ | ||
"**/node_modules/**" | ||
], | ||
"reports": [ | ||
"text-summary", | ||
"json-summary", | ||
"json", | ||
"html", | ||
"lcov", | ||
"lcovonly", | ||
"cobertura" | ||
], | ||
"verbose": false | ||
} | ||
{ | ||
"enabled": false, | ||
"relativeSourcePath": "../client", | ||
"relativeCoverageDir": "../../coverage", | ||
"ignorePatterns": [ | ||
"**/node_modules/**" | ||
], | ||
"reports": [ | ||
"text-summary", | ||
"json-summary", | ||
"json", | ||
"html", | ||
"lcov", | ||
"lcovonly", | ||
"cobertura" | ||
], | ||
"verbose": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add localization of strings. Localized versions are specified in the package.nls.\<locale\>.json files. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import * as assert from 'assert'; | ||
import * as localize from '../../utils/localize'; | ||
|
||
// Defines a Mocha test suite to group tests of similar kind together | ||
suite('localize tests', () => { | ||
|
||
test('keys', done => { | ||
const val = localize.LanguageServiceSurveyBanner.bannerMessage(); | ||
assert.equal(val, 'Can you please take 2 minutes to tell us how the Python Language Server is working for you?', 'LanguageServiceSurveyBanner string doesnt match'); | ||
done(); | ||
}); | ||
|
||
test('keys italian', done => { | ||
// Force a config change | ||
process.env.VSCODE_NLS_CONFIG = '{ "locale": "it" }'; | ||
|
||
const val = localize.LanguageServiceSurveyBanner.bannerLabelYes(); | ||
assert.equal(val, 'Sì, prenderò il sondaggio ora', 'bannerLabelYes is not being translated'); | ||
done(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { EXTENSION_ROOT_DIR } from '../client/common/constants'; | ||
|
||
// External callers of localize use these tables to retrieve localized values. | ||
export namespace LanguageServiceSurveyBanner { | ||
export const bannerMessage = localize('LanguageServiceSurveyBanner.bannerMessage', 'Can you please take 2 minutes to tell us how the Python Language Server is working for you?'); | ||
export const bannerLabelYes = localize('LanguageServiceSurveyBanner.bannerLabelYes', 'Yes, take survey now'); | ||
export const bannerLabelNo = localize('LanguageServiceSurveyBanner.bannerLabelNo', 'No, thanks'); | ||
} | ||
|
||
// Skip using vscode-nls and instead just compute our strings based on key values. Key values | ||
// can be loaded out of the nls.<locale>.json files | ||
let loadedCollection: { [index: string]: string } | undefined ; | ||
let defaultCollection: { [index: string]: string } | undefined ; | ||
let loadedLocale: string; | ||
|
||
function localize(key: string, defValue: string) { | ||
// Return a pointer to function so that we refetch it on each call. | ||
return () => { | ||
return getString(key, defValue); | ||
}; | ||
} | ||
|
||
function parseLocale() : string { | ||
// Attempt to load from the vscode locale. If not there, use english | ||
const vscodeConfigString = process.env.VSCODE_NLS_CONFIG; | ||
return vscodeConfigString ? JSON.parse(vscodeConfigString).locale : 'en-us'; | ||
} | ||
|
||
function getString(key: string, defValue: string) { | ||
// Load the current collection | ||
if (!loadedCollection || parseLocale() !== loadedLocale) { | ||
load(); | ||
} | ||
|
||
// First lookup in the dictionary that matches the current locale | ||
if (loadedCollection && loadedCollection.hasOwnProperty(key)) { | ||
return loadedCollection[key]; | ||
} | ||
|
||
// Fallback to the default dictionary | ||
if (defaultCollection && defaultCollection.hasOwnProperty(key)) { | ||
return defaultCollection[key]; | ||
} | ||
|
||
// Not found, return the default | ||
return defValue; | ||
} | ||
|
||
function load() { | ||
// Figure out our current locale. | ||
loadedLocale = parseLocale(); | ||
|
||
// Find the nls file that matches (if there is one) | ||
const nlsFile = path.join(EXTENSION_ROOT_DIR, `package.nls.${loadedLocale}.json`); | ||
if (fs.existsSync(nlsFile)) { | ||
const contents = fs.readFileSync(nlsFile, 'utf8'); | ||
loadedCollection = JSON.parse(contents); | ||
} else { | ||
// If there isn't one, at least remember that we looked so we don't try to load a second time | ||
loadedCollection = {}; | ||
} | ||
|
||
// Get the default collection if necessary. Strings may be in the default or the locale json | ||
if (!defaultCollection) { | ||
const defaultNlsFile = path.join(EXTENSION_ROOT_DIR, 'package.nls.json'); | ||
if (fs.existsSync(defaultNlsFile)) { | ||
const contents = fs.readFileSync(defaultNlsFile, 'utf8'); | ||
return JSON.parse(contents); | ||
} else { | ||
defaultCollection = {}; | ||
} | ||
} | ||
} | ||
|
||
// Default to loading the current locale | ||
load(); |