From 5ceca68ec271477dfcf75c7d9fba35c21880642e Mon Sep 17 00:00:00 2001 From: Ruben Date: Fri, 6 Oct 2023 13:39:02 +0200 Subject: [PATCH] make "show as words" follow document language, with default to Dutch --- .changeset/khaki-schools-clap.md | 5 +++++ addon/components/variable-plugin/number/nodeview.ts | 2 +- .../plugins/variable-plugin/utils/number-to-words.ts | 11 ++++++++--- addon/plugins/variable-plugin/variables/number.ts | 4 ++-- package-lock.json | 10 +++++----- package.json | 2 +- 6 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 .changeset/khaki-schools-clap.md diff --git a/.changeset/khaki-schools-clap.md b/.changeset/khaki-schools-clap.md new file mode 100644 index 000000000..48332807c --- /dev/null +++ b/.changeset/khaki-schools-clap.md @@ -0,0 +1,5 @@ +--- +'@lblod/ember-rdfa-editor-lblod-plugins': patch +--- + +Using "show as words" for a number variable will convert the number to a string in the language in the document, instead of always showing Dutch. diff --git a/addon/components/variable-plugin/number/nodeview.ts b/addon/components/variable-plugin/number/nodeview.ts index 08c09e14a..fd118d501 100644 --- a/addon/components/variable-plugin/number/nodeview.ts +++ b/addon/components/variable-plugin/number/nodeview.ts @@ -63,7 +63,7 @@ export default class NumberNodeviewComponent extends Component { if (!this.writtenNumber) { return value; } else { - return numberToWords(Number(value), { lang: 'nl' }); + return numberToWords(Number(value), { lang: this.documentLanguage }); } } diff --git a/addon/plugins/variable-plugin/utils/number-to-words.ts b/addon/plugins/variable-plugin/utils/number-to-words.ts index a72bb5fad..dfaa9bdf8 100644 --- a/addon/plugins/variable-plugin/utils/number-to-words.ts +++ b/addon/plugins/variable-plugin/utils/number-to-words.ts @@ -2,14 +2,19 @@ import n2words from 'n2words'; /** * Wrapper around the `n2words` function which catches possible errors thrown by n2words. + * If an invalid `lang` is used, `nl` is used by default. * If `n2words` throws an error (because of inability to convert the number), * this function displays the error as a warning and returns the provided number as a string. */ export function numberToWords(number: number, options: { lang: string }) { try { return n2words(number, options); - } catch (e) { - console.warn(e); - return number.toString(); + } catch { + try { + return n2words(number, { ...options, lang: 'nl' }); + } catch (e) { + console.warn(e); + return number.toString(); + } } } diff --git a/addon/plugins/variable-plugin/variables/number.ts b/addon/plugins/variable-plugin/variables/number.ts index 9eebe873c..f78d34d8d 100644 --- a/addon/plugins/variable-plugin/variables/number.ts +++ b/addon/plugins/variable-plugin/variables/number.ts @@ -73,7 +73,7 @@ const parseDOM = [ const serialize = (node: PNode, state: EditorState): DOMOutputSpec => { const t = getTranslationFunction(state); - + const docLang = state.doc.attrs.lang as string; const { mappingResource, variableInstance, @@ -88,7 +88,7 @@ const serialize = (node: PNode, state: EditorState): DOMOutputSpec => { if (isNumber(value)) { if (writtenNumber) { - humanReadableContent = numberToWords(Number(value), { lang: 'nl' }); + humanReadableContent = numberToWords(Number(value), { lang: docLang }); } else { humanReadableContent = value as string; } diff --git a/package-lock.json b/package-lock.json index d260189bd..07e0e54cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,7 +31,7 @@ "ember-resources": "^6.1.1", "ember-velcro": "^2.1.0", "fetch-sparql-endpoint": "^3.0.0", - "n2words": "^1.16.4", + "n2words": "^1.18.0", "process": "0.11.10", "rdf-ext": "^2.1.0", "rdf-validate-shacl": "^0.4.5", @@ -25228,11 +25228,11 @@ "license": "ISC" }, "node_modules/n2words": { - "version": "1.16.4", - "resolved": "https://registry.npmjs.org/n2words/-/n2words-1.16.4.tgz", - "integrity": "sha512-t4wGZkLxQpByjWwuN5cJWTBUHer82g3bGUDaK00YJZicQPUYcI9i6eu3KMDg/awLIkTKeH5IQ866lGy+c02d2g==", + "version": "1.18.0", + "resolved": "https://registry.npmjs.org/n2words/-/n2words-1.18.0.tgz", + "integrity": "sha512-FZFNFQhwRDcshGtRrnA4GTabwI9hHow1QndIf10YzFdvKaC3DQsFVN7h2MnjX6hJQLH+vO0ET+K5yN8hBaTW8A==", "engines": { - "node": "16 || >=18" + "node": "16 || 18 || >=20" } }, "node_modules/n3": { diff --git a/package.json b/package.json index 35353e841..417861881 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "ember-resources": "^6.1.1", "ember-velcro": "^2.1.0", "fetch-sparql-endpoint": "^3.0.0", - "n2words": "^1.16.4", + "n2words": "^1.18.0", "process": "0.11.10", "rdf-ext": "^2.1.0", "rdf-validate-shacl": "^0.4.5",