Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] i18n: Fresh Start #1074

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions locales/pulsar.en.cson
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'pulsar': {
'config': {
'excludeVcsIgnoredPaths': {
'title': 'Exclude VCS Ignored Paths'
}
}
'menu': {
'settings-view:view-installed-packages': 'Open Package Manager'
}
'context-menu': {
'core:undo': 'Undo'
}
}
4 changes: 2 additions & 2 deletions menus/win32.cson
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
{
label: '&Packages'
submenu: [
{ label: 'Open Package Manager', command: 'settings-view:view-installed-packages' }
{ label: '%pulsar.menu.settings-view:view-installed-packages%', command: 'settings-view:view-installed-packages' }
{ type: 'separator' }
]
}
Expand Down Expand Up @@ -209,7 +209,7 @@

'context-menu':
'atom-text-editor, .overlayer': [
{label: 'Undo', command: 'core:undo'}
{label: '%pulsar.context-menu.core:undo%', command: 'core:undo'}
{label: 'Redo', command: 'core:redo'}
{type: 'separator'}
{label: 'Cut', command: 'core:cut'}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"grim": "2.0.3",
"image-view": "file:packages/image-view",
"incompatible-packages": "file:packages/incompatible-packages",
"intl-messageformat": "^10.5.14",
"jasmine-json": "~0.0",
"jasmine-reporters": "1.1.0",
"jasmine-tagged": "^1.1.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/command-palette/lib/command-palette-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class CommandPaletteView {
initiallyVisibleItemCount: initiallyVisibleItemCount, // just for being able to disable visible-on-render in spec
items: [],
filter: this.filter,
emptyMessage: 'No matches found',
emptyMessage: atom.i18n.t("command-palette.commandPaletteView.emptyMessage"),
elementForItem: (item, {index, selected, visible}) => {
if (!visible) {
return document.createElement("li")
Expand Down Expand Up @@ -232,7 +232,7 @@ export default class CommandPaletteView {
})

const introEl = document.createElement('strong')
introEl.textContent = 'matching tags: '
introEl.textContent = atom.i18n.t("command-palette.commandPaletteView.matchingTags");

tagsEl.appendChild(introEl)
matchingTags.map(t => this.createTag(t, query)).forEach((tagEl, i) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/command-palette/locales/command-palette.en.cson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'commandPaletteView': {
'emptyMessage': 'No matches found'
'matchingTags': 'matching tags: '
}
6 changes: 6 additions & 0 deletions packages/settings-view/lib/rich-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module.exports = {
if (schema && schema.description) {
description = schema.description
}

// Localize
if (atom.i18n.isAutoTranslateLabel(description)) {
description = atom.i18n.translateLabel(description);
}

return atom.ui.markdown.render(
description,
{
Expand Down
6 changes: 5 additions & 1 deletion packages/settings-view/lib/rich-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module.exports = {
name = ''
}
const schema = atom.config.getSchema(keyPath)
const title = schema != null ? schema.title : null
let title = schema != null ? schema.title : null
// Localize
if (typeof title === "string" && atom.i18n.isAutoTranslateLabel(title)) {
title = atom.i18n.translateLabel(title);
}
return title || _.uncamelcase(name).split('.').map(_.capitalize).join(' ')
}
}
55 changes: 54 additions & 1 deletion spec/context-menu-manager-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('ContextMenuManager', function() {

beforeEach(function() {
const { resourcePath } = atom.getLoadSettings();
contextMenu = new ContextMenuManager({ keymapManager: atom.keymaps });
contextMenu = new ContextMenuManager({ keymapManager: atom.keymaps, i18n: atom.i18n });
contextMenu.initialize({ resourcePath });

parent = document.createElement('div');
Expand Down Expand Up @@ -518,5 +518,58 @@ describe('ContextMenuManager', function() {
}
]);
});

it('translates labels when a LocaleLabel is present', function() {
const I18n = require("../src/i18n.js");
atom.i18n.localeFallbackList = I18n.LocaleNegotiation(
"es-MX",
[ "zh-Hant" ]
);
atom.i18n.addStrings({
example: {
stringKey: "Hello Pulsar",
otherStringKey: "Goodbye Pulsar"
}
}, "en");

contextMenu.add({
'.parent': [
{
label: '%example.stringKey%',
submenu: [
{
label: 'My Command',
command: 'test:my-command',
after: ['test:my-other-command']
},
{
label: '%example.otherStringKey%',
command: 'test:my-other-command'
}
]
}
]
});
const dispatchedEvent = { target: parent };
expect(contextMenu.templateForEvent(dispatchedEvent)).toEqual([
{
label: 'Hello Pulsar',
id: `Parent`,
submenu: [
{
label: 'My Other Command',
id: 'My Other Command',
command: 'test:my-other-command'
},
{
label: 'Goodbye Pulsar',
id: 'My Command',
command: 'test:my-command',
after: ['test:my-other-command']
}
]
}
]);
});
});
});
241 changes: 241 additions & 0 deletions spec/i18n-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
const I18n = require("../src/i18n.js");

describe("I18n", () => {
let i18n;

beforeEach(() => {
const { resourcePath } = atom.getLoadSettings();
i18n = new I18n({
config: atom.config
});
i18n.initialize({ resourcePath });
});

describe("Handles Locales logic", () => {
it("Locale Lookup Filtering Fallback Pattern Array: Follows RFC4647", () => {
const primaryLocale = "es-MX";
const priorityListLocale = [
"zh-Hant-CN-x-private1-private2",
"en-US"
];
const fallbackSet = I18n.LocaleNegotiation(primaryLocale, priorityListLocale);

expect(Array.from(fallbackSet)).toEqual([
"es-MX",
"es",
"zh-Hant-CN-x-private1-private2",
"zh-Hant-CN-x-private1",
"zh-Hant-CN",
"zh-Hant",
"zh",
"en-US",
"en"
]);
});

it("Locale Lookup Filtering Fallback Pattern Array: Excludes duplicates from hardcoded fallback", () => {
const primaryLocale = "en-US";
const priorityListLocale = [ "es-MX" ];
const fallbackSet = I18n.LocaleNegotiation(primaryLocale, priorityListLocale);

expect(Array.from(fallbackSet)).toEqual([
"en-US",
"en",
"es-MX",
"es"
]);
});

it("Accurately determines if Locale Should be included when it should", () => {
const primaryLocale = "en-US";
const priorityListLocale = [ "es-MX" ];
const questionedLocale = "en";
const shouldBeIncluded = I18n.ShouldIncludeLocale(questionedLocale, primaryLocale, priorityListLocale);
expect(shouldBeIncluded).toEqual(true);
});

it("Accurately determines if Locale Should be included when it shoudln't", () => {
const primaryLocale = "zh-Hant";
const priorityListLocale = [ "es-MX" ];
const questionedLocale = "ja-JP";
const shouldBeIncluded = I18n.ShouldIncludeLocale(questionedLocale, primaryLocale, priorityListLocale);
expect(shouldBeIncluded).toEqual(false);
});

it("Accurately determines if Locale should be included when the locale is the hardcoded fallback", () => {
const primaryLocale = "zh-Hant";
const priorityListLocale = [ "es-MX" ];
const questionedLocale = "en";
const shouldBeIncluded = I18n.ShouldIncludeLocale(questionedLocale, primaryLocale, priorityListLocale);
expect(shouldBeIncluded).toEqual(true);
});

it("Can determine if basic locales match", () => {
const want = "en-US";
const have = "en-US";
expect(I18n.DoLocalesMatch(want, have)).toEqual(true);
});

it("Can determine if wildcard locales match", () => {
const want = "en-*";
const have = "en-US";
expect(I18n.DoLocalesMatch(want, have)).toEqual(true);
});

it("Can determine if locales do not match", () => {
const want = "en-US";
const have = "ja-JP";
expect(I18n.DoLocalesMatch(want, have)).toEqual(false);
});
});

describe("Crafts strings object correctly", () => {
it("Properly adds locale key", () => {
i18n.addStrings({
example: {
stringKey: "String Value"
}
}, "en-US");

expect(i18n.strings.example.stringKey).toEqual({
"en-US": "String Value"
});
});

it("Handles deep objects", () => {
i18n.addStrings({
example: {
deepExampleKey: {
stringKey: "String Value"
}
}
}, "en-US");

expect(i18n.strings.example.deepExampleKey.stringKey).toEqual({
"en-US": "String Value"
});
});

it("Adds new locales to existing objects", () => {
i18n.addStrings({
example: {
stringKey: "Hello Pulsar"
}
}, "en-US");
i18n.addStrings({
example: {
stringKey: "Hola Pulsar"
}
}, "es-MX");

expect(i18n.strings.example.stringKey).toEqual({
"en-US": "Hello Pulsar",
"es-MX": "Hola Pulsar"
});
});

it("Adds new locale to object", () => {
i18n.addStrings({
example: {
stringKey: "Hello Pulsar"
}
}, "en-US");

i18n.addString("example.stringKey", "Hola Pulsar", "es-MX");

expect(i18n.strings.example.stringKey).toEqual({
"en-US": "Hello Pulsar",
"es-MX": "Hola Pulsar"
});
});
});

describe("Is able to translate properly", () => {

beforeEach(() => {
const primary = "es-MX";
const priorityListLocale = [ "zh-Hant" ];
i18n.localeFallbackList = I18n.LocaleNegotiation(primary, priorityListLocale);
});

it("Returns the proper string based on user setting", () => {
i18n.addStrings({
example: {
stringKey: "Hello Pulsar"
}
}, "en");

i18n.addStrings({
example: {
stringKey: "Hola Pulsar"
}
}, "es-MX");

expect(i18n.t("example.stringKey")).toEqual("Hola Pulsar");
});

it("Handles ICU MessageFormat Replacements", () => {
i18n.addStrings({
example: {
stringKey: "Hello {value}"
}
}, "en");

expect(i18n.t("example.stringKey", { value: "confused-Techie" })).toEqual(
"Hello confused-Techie"
);
});

it("Handles namespace translations", () => {
i18n.addStrings({
example: {
stringKey: "Hello Pulsar"
}
}, "en");

const t = i18n.getT("example");

expect(t.t("stringKey")).toEqual("Hello Pulsar");
});
});

describe("Translates LocaleLabels", () => {
it("Identifies a LocaleLabel", () => {
const str1 = "%this-is-a-locale-label%";
const str2 = "this-is-not-a-locale-label";
const str3 = "%nor-is-this";
const str4 = "%or-%this";

expect(i18n.isAutoTranslateLabel(str1)).toEqual(true);
expect(i18n.isAutoTranslateLabel(str2)).toEqual(false);
expect(i18n.isAutoTranslateLabel(str3)).toEqual(false);
expect(i18n.isAutoTranslateLabel(str4)).toEqual(false);
});

it("Successfully translates a LocaleLabel", () => {
const primary = "es-MX";
const priorityListLocale = [ "zh-Hant" ];
i18n.localeFallbackList = I18n.LocaleNegotiation(primary, priorityListLocale);

i18n.addStrings({
example: {
stringKey: "Hello Pulsar"
}
}, "en");

const localeLabel = "%example.stringKey%";

expect(i18n.translateLabel(localeLabel)).toEqual("Hello Pulsar");
});

it("Falls back to the original label if unable to translate", () => {
const primary = "es-MX";
const priorityListLocale = [ "zh-Hant" ];
i18n.localeFallbackList = I18n.LocaleNegotiation(primary, priorityListLocale);

const localeLabel = "%example.stringKey%";

expect(i18n.translateLabel(localeLabel)).toEqual("%example.stringKey%");
});
});
});
Loading
Loading