From f5d544bc15ad17cf9bbb30327607990238a00439 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 11 Dec 2019 03:08:33 -0700 Subject: [PATCH] Generate label if type/field name isn't available in global schema E.g., show "Foo Bar" for field fooBar if not available This could happen after downgrading if the global schema couldn't be read from the DB for some reason. --- chrome/content/zotero/xpcom/data/cachedTypes.js | 7 ++++++- chrome/content/zotero/xpcom/data/itemFields.js | 15 ++++++--------- chrome/content/zotero/xpcom/utilities_internal.js | 6 ++++++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/cachedTypes.js b/chrome/content/zotero/xpcom/data/cachedTypes.js index 68009a681d..1d5ce84540 100644 --- a/chrome/content/zotero/xpcom/data/cachedTypes.js +++ b/chrome/content/zotero/xpcom/data/cachedTypes.js @@ -442,7 +442,12 @@ Zotero.ItemTypes = new function() { return _customLabels[id]; } - return Zotero.Schema.globalSchemaLocale.itemTypes[typeName]; + var label = Zotero.Schema.globalSchemaLocale.itemTypes[typeName]; + if (!label) { + Zotero.logError(`Localized string not available for item type '${typeName}'`); + label = Zotero.Utilities.Internal.camelToTitleCase(typeName); + } + return label; } this.getImageSrc = function (itemType) { diff --git a/chrome/content/zotero/xpcom/data/itemFields.js b/chrome/content/zotero/xpcom/data/itemFields.js index d344485b6e..38e50970b4 100644 --- a/chrome/content/zotero/xpcom/data/itemFields.js +++ b/chrome/content/zotero/xpcom/data/itemFields.js @@ -77,15 +77,12 @@ Zotero.ItemFields = new function() { for (let field of fields) { let isBaseField = baseFields.includes(field.fieldID); - let label; - try { - label = field.label || Zotero.Schema.globalSchemaLocale.fields[field.fieldName]; - } - // Some base fields aren't localized - catch (e) { - if (!isBaseField) { - throw e; - } + let label = field.label || Zotero.Schema.globalSchemaLocale.fields[field.fieldName]; + // If string not available, use the field name, except for some base fields that aren't + // used in the UI and therefore aren't localized + if (!label && !['number', 'type', 'medium'].includes(field.fieldName)) { + Zotero.logError(`Localized string not available for field '${field.fieldName}'`); + label = Zotero.Utilities.Internal.camelToTitleCase(field.fieldName); } _fields[field.fieldID] = { diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index cdf50259bd..505d2b525b 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -1375,6 +1375,12 @@ Zotero.Utilities.Internal = { }, + camelToTitleCase: function (str) { + str = str.replace(/([A-Z])/g, " $1"); + return str.charAt(0).toUpperCase() + str.slice(1); + }, + + resolveLocale: function (locale, locales) { // If the locale exists as-is, use it if (locales.includes(locale)) {