« get me outta code hell

language.js « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/language.js
blob: 09466907946eea5e68e4a054027b59f412d5ef0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {readFile} from 'node:fs/promises';

// It stands for "HTML Entities", apparently. Cursed.
import he from 'he';

import T from '#things';

export async function processLanguageFile(file) {
  const contents = await readFile(file, 'utf-8');
  const json = JSON.parse(contents);

  const code = json['meta.languageCode'];
  if (!code) {
    throw new Error(`Missing language code (file: ${file})`);
  }
  delete json['meta.languageCode'];

  const intlCode = json['meta.languageIntlCode'] ?? null;
  delete json['meta.languageIntlCode'];

  const name = json['meta.languageName'];
  if (!name) {
    throw new Error(`Missing language name (${code})`);
  }
  delete json['meta.languageName'];

  const hidden = json['meta.hidden'] ?? false;
  delete json['meta.hidden'];

  const language = new T.Language();
  language.code = code;
  language.intlCode = intlCode;
  language.name = name;
  language.hidden = hidden;
  language.escapeHTML = (string) =>
    he.encode(string, {useNamedReferences: true});
  language.strings = json;
  return language;
}