From 25b0afec90c40ab96ba943fa60fc58a635f7e665 Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Sat, 9 Mar 2024 21:46:06 +0000 Subject: [PATCH] Switch to using Intl.DisplayNames Use built-in JS support for multi-lingual labels for languages, rather than including them in the viewer JSON. Fixes #573 --- iXBRLViewerPlugin/iXBRLViewer.py | 19 ------------------- iXBRLViewerPlugin/viewer/src/js/inspector.js | 11 ++++++++++- .../viewer/src/js/report.test.js | 7 ------- iXBRLViewerPlugin/viewer/src/js/reportset.js | 4 ---- .../viewer/src/js/reportset.test.js | 16 ---------------- 5 files changed, 10 insertions(+), 47 deletions(-) diff --git a/iXBRLViewerPlugin/iXBRLViewer.py b/iXBRLViewerPlugin/iXBRLViewer.py index 30916681b..ec57dfe3d 100644 --- a/iXBRLViewerPlugin/iXBRLViewer.py +++ b/iXBRLViewerPlugin/iXBRLViewer.py @@ -92,7 +92,6 @@ def __init__(self, reports: list[ModelXbrl], basenameSuffix: str = ''): self.taxonomyData = { "sourceReports": [], "features": [], - "languages": {}, } self.basenameSuffix = basenameSuffix self.currentTargetReport = None @@ -132,23 +131,6 @@ def escapeJSONForScriptTag(self, s): """ return s.replace("<","\\u003C").replace(">","\\u003E").replace("&","\\u0026") - def makeLanguageName(self, langCode): - code = re.sub("-.*","",langCode) - try: - language = pycountry.languages.lookup(code) - match = re.match(r'^[^-]+-(.*)$',langCode) - name = language.name - if match is not None: - name = "%s (%s)" % (name, match.group(1).upper()) - except LookupError: - name = langCode - - return name - - def addLanguage(self, langCode): - if langCode not in self.taxonomyData["languages"]: - self.taxonomyData["languages"][langCode] = self.makeLanguageName(langCode) - def addELR(self, report: ModelXbrl, elr): prefix = self.roleMap.getPrefix(elr) if self.currentTargetReport.setdefault("roleDefs",{}).get(prefix, None) is None: @@ -170,7 +152,6 @@ def addConcept(self, report: ModelXbrl, concept, dimensionType = None): for lr in labels: l = lr.toModelObject conceptData["labels"].setdefault(self.roleMap.getPrefix(l.role),{})[l.xmlLang.lower()] = l.text; - self.addLanguage(l.xmlLang.lower()); refData = [] for _refRel in concept.modelXbrl.relationshipSet(XbrlConst.conceptReference).fromModelObject(concept): diff --git a/iXBRLViewerPlugin/viewer/src/js/inspector.js b/iXBRLViewerPlugin/viewer/src/js/inspector.js index 3b9729dba..202a17342 100644 --- a/iXBRLViewerPlugin/viewer/src/js/inspector.js +++ b/iXBRLViewerPlugin/viewer/src/js/inspector.js @@ -205,7 +205,16 @@ export class Inspector { this._optionsMenu.reset(); if (this._reportSet) { const dl = this.selectDefaultLanguage(); - this._optionsMenu.addCheckboxGroup(this._reportSet.availableLanguages(), this._reportSet.languageNames(), dl, (lang) => { this.setLanguage(lang); this.update() }, "select-language"); + const langs = this._reportSet.availableLanguages(); + const langNames = new Intl.DisplayNames(this.preferredLanguages(), { "type": "language" }); + + this._optionsMenu.addCheckboxGroup( + langs, + Object.fromEntries(langs.map((l) => [l, langNames.of(l)])), + dl, + (lang) => { this.setLanguage(lang); this.update() }, + "select-language" + ); this.setLanguage(dl); if (this._reportSet.filingDocuments()) { this._optionsMenu.addDownloadButton("Download filing documents", this._reportSet.filingDocuments()) diff --git a/iXBRLViewerPlugin/viewer/src/js/report.test.js b/iXBRLViewerPlugin/viewer/src/js/report.test.js index 27f440a2c..efe43a02b 100644 --- a/iXBRLViewerPlugin/viewer/src/js/report.test.js +++ b/iXBRLViewerPlugin/viewer/src/js/report.test.js @@ -74,13 +74,6 @@ describe("Language options", () => { expect(al).toHaveLength(6); expect(al).toEqual(expect.arrayContaining(["en", "en-us", "en-gb", "fr", "de", "es"])); }); - - test("Names for available languages", () => { - const ln = testReportSet.languageNames(); - expect(Object.keys(ln)).toHaveLength(2); - expect(ln['en']).toBe("English"); - expect(ln['en-us']).toBe("English (US)"); - }); }); describe("Fetching facts", () => { diff --git a/iXBRLViewerPlugin/viewer/src/js/reportset.js b/iXBRLViewerPlugin/viewer/src/js/reportset.js index b819b4c79..3f24d88ae 100644 --- a/iXBRLViewerPlugin/viewer/src/js/reportset.js +++ b/iXBRLViewerPlugin/viewer/src/js/reportset.js @@ -178,10 +178,6 @@ export class ReportSet { return usedScalesMap; } - languageNames() { - return this._data.languages; - } - roleMap() { return this._data.roles; } diff --git a/iXBRLViewerPlugin/viewer/src/js/reportset.test.js b/iXBRLViewerPlugin/viewer/src/js/reportset.test.js index b0e74ed22..64999d9b3 100644 --- a/iXBRLViewerPlugin/viewer/src/js/reportset.test.js +++ b/iXBRLViewerPlugin/viewer/src/js/reportset.test.js @@ -185,14 +185,6 @@ describe("Multi report - Language options", () => { expect(al).toHaveLength(3); expect(al).toEqual(expect.arrayContaining(["en", "en-us", "en-gb"])); }); - - test("Names for languages", () => { - const ln = testReportSet.languageNames(); - expect(Object.keys(ln)).toHaveLength(3); - expect(ln['en']).toBe("English"); - expect(ln['en-us']).toBe("English (US)"); - expect(ln['fr']).toBe("French"); - }); }); describe("Multi report - Fetching facts", () => { @@ -294,14 +286,6 @@ describe("Single report - Language options", () => { expect(al).toHaveLength(2); expect(al).toEqual(expect.arrayContaining(["en", "en-us"])); }); - - test("Names for languages", () => { - const ln = testReportSet.languageNames(); - expect(Object.keys(ln)).toHaveLength(3); - expect(ln['en']).toBe("English"); - expect(ln['en-us']).toBe("English (US)"); - expect(ln['fr']).toBe("French"); - }); }); describe("Single report - Fetching facts", () => {