Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Support package.json i18n key and show a hint in case the extension is t... #7995

Merged
merged 8 commits into from
Jul 21, 2014
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/extensibility/ExtensionManagerView.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ define(function (require, exports, module) {

var Strings = require("strings"),
StringUtils = require("utils/StringUtils"),
NativeApp = require("utils/NativeApp"),
ExtensionManager = require("extensibility/ExtensionManager"),
registry_utils = require("extensibility/registry_utils"),
InstallExtensionDialog = require("extensibility/InstallExtensionDialog"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
LocalizationUtils = require("utils/LocalizationUtils"),
itemTemplate = require("text!htmlContent/extension-manager-view-item.html");

/**
Expand Down Expand Up @@ -217,6 +215,43 @@ define(function (require, exports, module) {

context.allowInstall = context.isCompatible && !context.isInstalled;

if (Array.isArray(info.metadata.i18n) && info.metadata.i18n.length > 0) {
var lang = brackets.getLocale(),
shortLang = lang.split("-")[0];

context.translated = true;
context.translatedLangs =
info.metadata.i18n.map(function (value) {
return { name: LocalizationUtils.getLocalizedLabel(value), locale: value };
})
.sort(function (lang1, lang2) {
// List users language first
var locales = [lang1.locale, lang2.locale],
userLangIndex = locales.indexOf(lang);
if (userLangIndex > -1) {
return userLangIndex;
}
userLangIndex = locales.indexOf(shortLang);
if (userLangIndex > -1) {
return userLangIndex;
}

return lang1.name.localeCompare(lang2.name);
})
.map(function (value) {
return value.name;
})
.join(", ");
context.translatedLangs = StringUtils.format(Strings.EXTENSION_TRANSLATED_LANGS, context.translatedLangs);

// If the selected language is System Default, match both the short (2-char) language code
// and the long one
var translatedIntoUserLang =
(brackets.isLocaleDefault() && info.metadata.i18n.indexOf(shortLang) > -1) ||
info.metadata.i18n.indexOf(lang) > -1;
context.extensionTranslated = translatedIntoUserLang ? Strings.EXTENSION_TRANSLATED_USER_LANG : Strings.EXTENSION_TRANSLATED_GENERAL;
}

var isInstalledInUserFolder = (entry.installInfo && entry.installInfo.locationType === ExtensionManager.LOCATION_USER);
context.allowRemove = isInstalledInUserFolder;
context.allowUpdate = context.showUpdateButton && context.isCompatible && context.updateCompatible && isInstalledInUserFolder;
Expand Down
16 changes: 4 additions & 12 deletions src/extensions/default/DebugCommands/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ define(function (require, exports, module) {
Dialogs = brackets.getModule("widgets/Dialogs"),
Strings = brackets.getModule("strings"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
LocalizationUtils = brackets.getModule("utils/LocalizationUtils"),
ErrorNotification = require("ErrorNotification"),
NodeDebugUtils = require("NodeDebugUtils"),
PerfDialogTemplate = require("text!htmlContent/perf-dialog.html"),
Expand Down Expand Up @@ -168,16 +169,7 @@ define(function (require, exports, module) {
locale = $select.val();
$submit.prop("disabled", locale === (curLocale || ""));
};

// returns the localized label for the given locale
// or the locale, if nothing found
var getLocalizedLabel = function (locale) {
var key = "LOCALE_" + locale.toUpperCase().replace("-", "_"),
i18n = Strings[key];

return i18n === undefined ? locale : i18n;
};


// inspect all children of dirEntry
entries.forEach(function (entry) {
if (entry.isDirectory) {
Expand All @@ -191,12 +183,12 @@ define(function (require, exports, module) {
label += match[2].toUpperCase();
}

languages.push({label: getLocalizedLabel(label), language: language});
languages.push({label: LocalizationUtils.getLocalizedLabel(label), language: language});
}
}
});
// add English (US), which is the root folder and should be sorted as well
languages.push({label: getLocalizedLabel("en"), language: "en"});
languages.push({label: LocalizationUtils.getLocalizedLabel("en"), language: "en"});

// sort the languages via their display name
languages.sort(function (lang1, lang2) {
Expand Down
4 changes: 4 additions & 0 deletions src/htmlContent/extension-manager-view-item.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
{{/metadata.keywords}}
</span>
{{/metadata.keywords.length}}
{{#translated}}
<br/>
<span class="ext-translated" title="{{translatedLangs}}">{{extensionTranslated}}</span>
{{/translated}}
</td>
<td class="ext-action">
{{#showInstallButton}}
Expand Down
3 changes: 3 additions & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ define({
"EXTENSION_MORE_INFO" : "More info...",
"EXTENSION_ERROR" : "Extension error",
"EXTENSION_KEYWORDS" : "Keywords",
"EXTENSION_TRANSLATED_USER_LANG" : "Translated into your language",
"EXTENSION_TRANSLATED_GENERAL" : "Translated into some languages",
"EXTENSION_TRANSLATED_LANGS" : "This extension has been translated into these languages: {0}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely need some wordsmithing here and maybe even another EXTENSION_TRANSLATED_LANGS string for the "not translated into user's lang" case (like Contribute a translation to make the extension even more awesome)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I think that the way you have it here makes a good compromise between being concise and presenting what the user needs to know. I'd be inclined to go with this as you have it for the time being.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Translated into {1} languages and respectively Translated into {1} languages, including yours?
The problem about that is that we have to provide singular and plural translations...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree that's nicer wording. Is it worth having to define two strings for? It's not even clear to me how many extensions will get translations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we usually use two strings everytime there's singular/plural, so I guess in this case it's the same.
And many (common) extensions already got translated, like Brackets Git, Theseus, Brackets Code Folding, HTML Skeleton, Brackets Todo, ... (OK, not too many, but those are very common)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an extension is translated into another language, that means that is is in English and in another language. So there are always at least 2 languages when it is translated. Which means that we don't really need a singular form.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomMalbran Yes, it should be, but it's still possible that an extension author uses an array like ["en"] or ["de"].
But I'm ok with having only a plural form, even though it will sound odd in such cases.

"EXTENSION_INSTALLED" : "Installed",
"EXTENSION_UPDATE_INSTALLED" : "This extension update has been downloaded and will be installed after {APP_NAME} reloads.",
"EXTENSION_SEARCH_PLACEHOLDER" : "Search",
Expand Down
4 changes: 4 additions & 0 deletions src/styles/brackets_patterns_override.less
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,10 @@ a[href^="http"] {
.ext-keywords {
color: @tc-input-placeholder-text;
}
.ext-translated {
color: @tc-input-placeholder-text;
font-weight: 600;
}
.muted {
color: @tc-input-placeholder-text;
}
Expand Down
53 changes: 53 additions & 0 deletions src/utils/LocalizationUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define */

/**
* Utilities functions related to localization/i18n
*
*/
define(function (require, exports, module) {
"use strict";

var Strings = require("strings");

/*
* Converts a language code to its written name, if possible.
* If not possible, the language code is simply returned.
*
* @param {string} locale The two-char language code
* @return {string} The language's name or the given language code
*/
function getLocalizedLabel(locale) {
var key = "LOCALE_" + locale.toUpperCase().replace("-", "_"),
i18n = Strings[key];

return i18n === undefined ? locale : i18n;
}


// Define public API
exports.getLocalizedLabel = getLocalizedLabel;
});
8 changes: 4 additions & 4 deletions src/utils/StringUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ define(function (require, exports, module) {
}

/**
* Convert number of bytes into human readable format.
* Converts number of bytes into human readable format.
* If param bytes is negative it returns the number without any changes.
*
* @param number bytes Number of bytes to convert
* @param number precision Number of digits after the decimal separator
* @return string
* @param {number} bytes Number of bytes to convert
* @param {number} precision Number of digits after the decimal separator
* @return {string}
*/
function prettyPrintBytes(bytes, precision) {
var kilobyte = 1024,
Expand Down
52 changes: 44 additions & 8 deletions test/spec/ExtensionManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ define(function (require, exports, module) {
FileSystem = require("filesystem/FileSystem"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils"),
LocalizationUtils = require("utils/LocalizationUtils"),
mockRegistryText = require("text!spec/ExtensionManager-test-files/mockRegistry.json"),
mockRegistryForSearch = require("text!spec/ExtensionManager-test-files/mockRegistryForSearch.json"),
mockExtensionList = require("text!spec/ExtensionManager-test-files/mockExtensionList.json"),
Expand Down Expand Up @@ -158,7 +159,6 @@ define(function (require, exports, module) {

describe("ExtensionManager", function () {
it("should download the extension list from the registry", function () {
var registry;
runs(function () {
waitsForDone(ExtensionManager.downloadRegistry(), "fetching registry");
});
Expand All @@ -172,7 +172,7 @@ define(function (require, exports, module) {
});

it("should trigger a registryUpdate event when updating the extension list from the registry", function () {
var registry, registryUpdateSpy;
var registryUpdateSpy;
runs(function () {
registryUpdateSpy = jasmine.createSpy();
$(ExtensionManager).on("registryUpdate", registryUpdateSpy);
Expand Down Expand Up @@ -257,7 +257,6 @@ define(function (require, exports, module) {
it("should set the title for a legacy extension based on its folder name", function () {
mockLoadExtensions();
runs(function () {
var mockPath = SpecRunnerUtils.getTestPath("/spec/ExtensionManager-test-files");
expect(ExtensionManager.extensions["mock-legacy-extension"].installInfo.metadata.title).toEqual("mock-legacy-extension");
});
});
Expand All @@ -267,7 +266,6 @@ define(function (require, exports, module) {
runs(function () {
expect(ExtensionManager.extensions["mock-extension-1"].installInfo.locationType).toEqual(ExtensionManager.LOCATION_DEFAULT);
expect(ExtensionManager.extensions["mock-extension-2"].installInfo.locationType).toEqual(ExtensionManager.LOCATION_DEV);
var mockPath = SpecRunnerUtils.getTestPath("/spec/ExtensionManager-test-files");
expect(ExtensionManager.extensions["mock-legacy-extension"].installInfo.locationType).toEqual(ExtensionManager.LOCATION_USER);
});
});
Expand All @@ -290,7 +288,6 @@ define(function (require, exports, module) {
mockLoadExtensions(["user/mock-legacy-extension"]);
});
runs(function () {
var mockPath = SpecRunnerUtils.getTestPath("/spec/ExtensionManager-test-files");
expect(spy).toHaveBeenCalledWith(jasmine.any(Object), "mock-legacy-extension");
});
});
Expand Down Expand Up @@ -708,7 +705,7 @@ define(function (require, exports, module) {
});

describe("ExtensionManagerView", function () {
var testWindow, view, model, fakeLoadDeferred, modelDisposed;
var view, model, fakeLoadDeferred, modelDisposed;

// Sets up the view using the normal (mock) ExtensionManager data.
function setupViewWithMockData(ModelClass) {
Expand Down Expand Up @@ -745,6 +742,7 @@ define(function (require, exports, module) {
var id = url.match(/fake-repository\.com\/([^\/]+)/)[1];
mockLoadExtensions(["user/" + id]);
});
spyOn(brackets, "getLocale").andReturn("en");
});


Expand Down Expand Up @@ -1191,8 +1189,7 @@ define(function (require, exports, module) {
mockLoadExtensions(["user/" + id]);
setupViewWithMockData(ExtensionManagerViewModel.InstalledViewModel);
runs(function () {
var mockPath = SpecRunnerUtils.getTestPath("/spec/ExtensionManager-test-files/user/" + id),
$button = $("button.remove[data-extension-id='" + id + "']", view.$el);
var $button = $("button.remove[data-extension-id='" + id + "']", view.$el);
$button.click();
expect(ExtensionManager.isMarkedForRemoval(id)).toBe(true);
});
Expand Down Expand Up @@ -1329,6 +1326,45 @@ define(function (require, exports, module) {
});
});

// i18n info
it("should show correct i18n info if the extension is translated in user's lang", function () {
var mockInstallInfo = { "mock-extension-4": { installInfo: mockRegistry["mock-extension-4"] } };
mockInstallInfo["mock-extension-4"].installInfo.metadata.i18n = ["zh-cn", "foo", "en"];
ExtensionManager._setExtensions(mockInstallInfo);
setupViewWithMockData(ExtensionManagerViewModel.InstalledViewModel);
runs(function () {
var $extTranslated = $(".ext-translated", view.$el),
languages = [LocalizationUtils.getLocalizedLabel("en"), "foo", LocalizationUtils.getLocalizedLabel("zh-cn")];
expect($extTranslated.length).toBe(1);
expect($extTranslated.text()).toBe(Strings.EXTENSION_TRANSLATED_USER_LANG);
expect($extTranslated.attr("title")).toBe(StringUtils.format(Strings.EXTENSION_TRANSLATED_LANGS, languages.join(", ")));
});
});
it("should show correct i18n info if the extension is translated in some other languages", function () {
var mockInstallInfo = { "mock-extension-4": { installInfo: mockRegistry["mock-extension-4"] } };
mockInstallInfo["mock-extension-4"].installInfo.metadata.i18n = ["zh-cn"];
ExtensionManager._setExtensions(mockInstallInfo);
setupViewWithMockData(ExtensionManagerViewModel.InstalledViewModel);
runs(function () {
var $extTranslated = $(".ext-translated", view.$el),
languages = [LocalizationUtils.getLocalizedLabel("zh-cn")];
expect($extTranslated.length).toBe(1);
expect($extTranslated.text()).toBe(Strings.EXTENSION_TRANSLATED_GENERAL);
expect($extTranslated.attr("title")).toBe(StringUtils.format(Strings.EXTENSION_TRANSLATED_LANGS, languages.join(", ")));
});
});
it("should not show i18n info if the extension isn't translated", function () {
var mockInstallInfo = { "mock-extension-4": { installInfo: mockRegistry["mock-extension-4"] } };
mockInstallInfo["mock-extension-4"].installInfo.metadata.i18n = [];
ExtensionManager._setExtensions(mockInstallInfo);
setupViewWithMockData(ExtensionManagerViewModel.InstalledViewModel);
runs(function () {
var $extTranslated = $(".ext-translated", view.$el);
expect($extTranslated.length).toBe(0);
expect($extTranslated.attr("title")).toBe(undefined);
});
});

});


Expand Down