Skip to content

Commit

Permalink
Switch to using Intl.DisplayNames
Browse files Browse the repository at this point in the history
Use built-in JS support for multi-lingual labels for languages, rather
than including them in the viewer JSON.

Fixes Arelle#573
  • Loading branch information
Paul Warren committed Mar 9, 2024
1 parent 969dbce commit 25b0afe
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 47 deletions.
19 changes: 0 additions & 19 deletions iXBRLViewerPlugin/iXBRLViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def __init__(self, reports: list[ModelXbrl], basenameSuffix: str = ''):
self.taxonomyData = {
"sourceReports": [],
"features": [],
"languages": {},
}
self.basenameSuffix = basenameSuffix
self.currentTargetReport = None
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
11 changes: 10 additions & 1 deletion iXBRLViewerPlugin/viewer/src/js/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 0 additions & 7 deletions iXBRLViewerPlugin/viewer/src/js/report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
4 changes: 0 additions & 4 deletions iXBRLViewerPlugin/viewer/src/js/reportset.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ export class ReportSet {
return usedScalesMap;
}

languageNames() {
return this._data.languages;
}

roleMap() {
return this._data.roles;
}
Expand Down
16 changes: 0 additions & 16 deletions iXBRLViewerPlugin/viewer/src/js/reportset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit 25b0afe

Please sign in to comment.