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

i18n support #17243

Open
wants to merge 5 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,19 @@ There are multiple customizations and properties able to be instantiated within

## Options

| JS Property | HTML Attribute | Description | Value | Default |
| ----------- | -------------- | ------------------------------------------------------------------------------- | ------- | ------------ |
| list | data-list | Where to find the list of suggestions. | Array of strings, HTML element, CSS selector (no groups, i.e. no commas), String containing a comma-separated list of items | N/A |
| minChars | data-minchars | Minimum characters the user has to type before the autocomplete popup shows up. | Number | 2 |
| maxItems | data-maxitems | Maximum number of suggestions to display. | Number | 10 |
| autoFirst | data-autofirst | Should the first element be automatically | Boolean | false |
| listLabel | data-listlabel | Denotes a label to be used as aria-label on the generated autocomplete list. | String | Results List |
| JS Property | HTML Attribute | Description | Value | Default |
|----------------------|---------------------------|---------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------|
| list | data-list | Where to find the list of suggestions. | Array of strings, HTML element, CSS selector (no groups, i.e. no commas), String containing a comma-separated list of items | N/A |
| minChars | data-minchars | Minimum characters the user has to type before the autocomplete popup shows up. | Number | 2 |
| maxItems | data-maxitems | Maximum number of suggestions to display. | Number | 10 |
| autoFirst | data-autofirst | Should the first element be automatically | Boolean | false |
| listLabel | data-listlabel | Denotes a label to be used as aria-label on the generated autocomplete list. | String | Results List |
| tStatusResult | data-tstatusresult | Allows for translations of the result list status. | String | ${length} results found |
| tNoResults | data-tnoresults | Allows for translations of the notification that no result has been found. | String | No results found |
| tStatusQueryTooShort | data-tstatusquerytooshort | Text output when not enough chars have been entered | String | Type ${minChars} or more characters for results. |
| tStatusStartTyping | data-tstatusstarttyping | Text output when no chars have been entered | String | Begin typing for results. |
| tListItemText | data-tstatusquerytooshort | Text output for found list items | String | list item ${index} of ${length} |


## License

Expand Down
17 changes: 11 additions & 6 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ var _ = function (input, o) {
item: _.ITEM,
replace: _.REPLACE,
tabSelect: false,
listLabel: "Results List"
listLabel: "Results List",
tStatusResult: "${length} results found",
tNoResults: "No results found",
tStatusQueryTooShort: "Type ${minChars} or more characters for results.",
tStatusStartTyping: "Begin typing for results.",
tListItemText: "list item ${index} of ${length}"
Copy link
Owner

Choose a reason for hiding this comment

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

I think these should be in a separate object, not just as raw properties. Also, we do not need them on every instance, what we need is a way to select a language for the instance. So I'd suggest a data structure like:

Awesomplete.languages = {
	"en-us": {
		"list-label": "Results List"
	}
}

Then instances would point to one of these objects either by storing the language, or a reference to the object (I'll leave that up to you).

}, o);

this.index = -1;
Expand All @@ -62,7 +67,7 @@ var _ = function (input, o) {
"aria-live": "assertive",
"aria-atomic": true,
inside: this.container,
textContent: this.minChars != 0 ? ("Type " + this.minChars + " or more characters for results.") : "Begin typing for results."
textContent: this.minChars != 0 ? this.tStatusQueryTooShort.replace("${minChars}", this.minChars) : this.tStatusStartTyping
Copy link
Owner

Choose a reason for hiding this comment

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

We probably need a method for this instead of just raw .replace() calls (also note that if the phrase needs to continue the substitution multiple times, this will only substitute the first one).

For inspiration, check out how Mavo does this: https://github.com/mavoweb/mavo/blob/master/src/locale.js#L25

});

// Bind events
Expand Down Expand Up @@ -259,7 +264,7 @@ _.prototype = {
if (i > -1 && lis.length > 0) {
lis[i].setAttribute("aria-selected", "true");

this.status.textContent = lis[i].textContent + ", list item " + (i + 1) + " of " + lis.length;
this.status.textContent = lis[i].textContent + ", "+ this.tListItemText.replace("${index}", (i + 1)).replace("${length}", lis.length);

this.input.setAttribute("aria-activedescendant", this.ul.id + "_item_" + this.index);

Expand Down Expand Up @@ -328,20 +333,20 @@ _.prototype = {

if (this.ul.children.length === 0) {

this.status.textContent = "No results found";
this.status.textContent = this.tNoResults;

this.close({ reason: "nomatches" });

} else {
this.open();

this.status.textContent = this.ul.children.length + " results found";
this.status.textContent = this.tStatusResult.replace("${length}", this.ul.children.length);
}
}
else {
this.close({ reason: "nomatches" });

this.status.textContent = "No results found";
this.status.textContent = this.tNoResults;
}
}
};
Expand Down
18 changes: 18 additions & 0 deletions test/api/evaluateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("awesomplete.evaluate", function () {
expect(this.subject.close).toHaveBeenCalledWith({
reason: "nomatches"
});
expect(this.subject.status.textContent).toBe("No results found")
});
});

Expand Down Expand Up @@ -93,4 +94,21 @@ describe("awesomplete.evaluate", function () {
expect(this.subject.index).toBe(-1);
});
});

describe("with no results and German text", function () {
beforeEach(function () {
this.subject.tNoResults = "Keine Resultate gefunden";
$.type(this.subject.input, "nosuchitem");
});

it("closes completer", function () {
spyOn(this.subject, "close");
this.subject.evaluate();

expect(this.subject.close).toHaveBeenCalledWith({
reason: "nomatches"
});
expect(this.subject.status.textContent).toBe("Keine Resultate gefunden")
});
});
});
13 changes: 13 additions & 0 deletions test/api/gotoSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ describe("awesomplete.goto", function () {
});
});


describe("with German text and item index > -1", function () {
beforeEach(function () {
this.subject.tListItemText = "Listenelement ${index} von ${length}";
this.subject.goto(0);
});

it("updates status", function () {
expect(this.subject.status.textContent).toBe("item1, Listenelement 1 von 3");
});
});


describe("with item index = -1", function () {
beforeEach(function () {
this.subject.goto(0);
Expand Down
12 changes: 11 additions & 1 deletion test/init/optionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ describe("Constructor options", function () {
it("replaces input value with REPLACE", function () {
expect(this.subject.replace).toEqual(Awesomplete.REPLACE);
});

it("uses 'Results List' as list label", function () {
expect(this.subject.listLabel).toEqual("Results List");
});

it("uses 'No results found' as tNoResults", function () {
expect(this.subject.tNoResults).toEqual("No results found");
});
});

describe("with custom options in constructor", function () {
Expand All @@ -50,14 +58,16 @@ describe("Constructor options", function () {
filter: $.noop,
sort: $.noop,
item: $.noop,
replace: $.noop
replace: $.noop,
listLabel: 'new list label'
};
});

it("overrides simple default options", function () {
expect(this.subject.minChars).toBe(3);
expect(this.subject.maxItems).toBe(9);
expect(this.subject.autoFirst).toBe(true);
expect(this.subject.listLabel).toBe("new list label");
});

it("overrides default functions", function () {
Expand Down