Skip to content

Commit

Permalink
make "show as words" follow document language, with default to Dutch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben committed Oct 6, 2023
1 parent e952beb commit 5ceca68
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-schools-clap.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion addon/components/variable-plugin/number/nodeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class NumberNodeviewComponent extends Component<Args> {
if (!this.writtenNumber) {
return value;
} else {
return numberToWords(Number(value), { lang: 'nl' });
return numberToWords(Number(value), { lang: this.documentLanguage });
}
}

Expand Down
11 changes: 8 additions & 3 deletions addon/plugins/variable-plugin/utils/number-to-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
4 changes: 2 additions & 2 deletions addon/plugins/variable-plugin/variables/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5ceca68

Please sign in to comment.