Add Zotero.File.getResource for local resources

Use `getResource` in Zotero.Date.init (this turns it into a
synchronous function). Zotero.File.getResource makes it easier
to load local files on platforms that do not support the
`resource://` URLs.
This commit is contained in:
Sylvester Keil 2018-07-05 18:12:22 +02:00 committed by Adomas Venčkauskas
parent 06cb9aff98
commit 53522c2cbe
4 changed files with 46 additions and 44 deletions

View file

@ -47,46 +47,41 @@ Zotero.Date = new function(){
throw new Error("Unimplemented");
}
return Zotero.HTTP.request(
'GET', 'resource://zotero/schema/dateFormats.json', { responseType: 'json' }
).then(function(xmlhttp) {
var json = xmlhttp.response;
var json = JSON.parse(Zotero.File.getResource('schema/dateFormats.json'));
var locale = Zotero.locale;
var english = locale.startsWith('en');
// If no exact match, try first two characters ('de')
if (!json[locale]) {
locale = locale.substr(0, 2);
}
// Try first two characters repeated ('de-DE')
if (!json[locale]) {
locale = locale + "-" + locale.toUpperCase();
}
// Look for another locale with same first two characters
if (!json[locale]) {
let sameLang = Object.keys(json).filter(l => l.startsWith(locale.substr(0, 2)));
if (sameLang.length) {
locale = sameLang[0];
}
}
// If all else fails, use English
if (!json[locale]) {
locale = 'en-US';
english = true;
}
_months = json[locale];
var locale = Zotero.locale;
var english = locale.startsWith('en');
// If no exact match, try first two characters ('de')
if (!json[locale]) {
locale = locale.substr(0, 2);
// Add English versions if not already added
if (english) {
_monthsWithEnglish = _months;
}
else {
_monthsWithEnglish = {};
for (let key in _months) {
_monthsWithEnglish[key] = _months[key].concat(json['en-US'][key]);
}
// Try first two characters repeated ('de-DE')
if (!json[locale]) {
locale = locale + "-" + locale.toUpperCase();
}
// Look for another locale with same first two characters
if (!json[locale]) {
let sameLang = Object.keys(json).filter(l => l.startsWith(locale.substr(0, 2)));
if (sameLang.length) {
locale = sameLang[0];
}
}
// If all else fails, use English
if (!json[locale]) {
locale = 'en-US';
english = true;
}
_months = json[locale];
// Add English versions if not already added
if (english) {
_monthsWithEnglish = _months;
}
else {
_monthsWithEnglish = {};
for (let key in _months) {
_monthsWithEnglish[key] = _months[key].concat(json['en-US'][key]);
}
}
});
}
};

View file

@ -340,6 +340,13 @@ Zotero.File = new function(){
return xmlhttp.responseText;
}
/*
* Returns the contents of the given local resource.
*/
this.getResource = function (res) {
return getContentsFromURL(`resource://zotero/${res}`);
}
/*
* Return a promise for the contents of a URL as a string

View file

@ -824,7 +824,7 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
// Initialize keyboard shortcuts
Zotero.Keys.init();
yield Zotero.Date.init();
Zotero.Date.init();
Zotero.LocateManager.init();
yield Zotero.ID.init();
yield Zotero.Collections.init();

View file

@ -28,7 +28,7 @@ describe("Zotero.Date", function() {
beforeEach(function* () {
if (Zotero.locale != 'en-US') {
Zotero.locale = 'en-US';
yield Zotero.Date.init();
Zotero.Date.init();
}
});
@ -52,7 +52,7 @@ describe("Zotero.Date", function() {
it("should resolve to English from unknown locale", function* () {
Zotero.locale = 'zz';
yield Zotero.Date.init();
Zotero.Date.init();
let months = Zotero.Date.getMonths().short;
assert.lengthOf(months, 12);
assert.sameMembers(months, englishShort);
@ -60,7 +60,7 @@ describe("Zotero.Date", function() {
it("shouldn't repeat English with unknown locale", function* () {
Zotero.locale = 'zz';
yield Zotero.Date.init();
Zotero.Date.init();
let months = Zotero.Date.getMonths(true).short;
assert.lengthOf(months, 12);
assert.sameMembers(months, englishShort);
@ -71,7 +71,7 @@ describe("Zotero.Date", function() {
beforeEach(function* () {
if (Zotero.locale != 'fr-FR') {
Zotero.locale = 'fr-FR';
yield Zotero.Date.init();
Zotero.Date.init();
}
});
@ -101,7 +101,7 @@ describe("Zotero.Date", function() {
it("should resolve from two-letter locale", function* () {
Zotero.locale = 'fr';
yield Zotero.Date.init();
Zotero.Date.init();
let months = Zotero.Date.getMonths().short;
assert.lengthOf(months, 12);
assert.sameMembers(months, frenchShort);
@ -109,7 +109,7 @@ describe("Zotero.Date", function() {
it("should resolve from unknown four-letter locale with common prefix", function* () {
Zotero.locale = 'fr-ZZ';
yield Zotero.Date.init();
Zotero.Date.init();
let months = Zotero.Date.getMonths().short;
assert.lengthOf(months, 12);
assert.sameMembers(months, frenchShort);