ch-node-utils
set of node utils which can be used across repos/projects.
Available as a normal npm dependency(here on npmjs)
Areas for the different utilities stored in this repo:
i18n (Internationalisation)
Internationalisation has been added to CHS, after already being available (at least for Welsh) on legacy systems (EWF/SCRS/CHD/...)
The implementation is described in Confluence for both Java and Node.js services.
Some common utilities for Node.js are here provided. The chosen Node.js i18n library is i18next, which, at the time of writing, can be considered the standard:


-
i18nCh.ts
: wrapper to i18next
-
languageNames.ts
: to manage Language names and their associated ISO codes (iso-639). It mainly provides:
- a function to get, from an array of submitted iso-codes, a structured array of iso-codes & native names.
Ex:
[... "en", "cy", ...] ==> [ ... {"IsoCode": "en", "Name": "English" }, {"IsoCode": "cy", "Name": "Cymraeg" } ...]
- a custom function to sort the supported locales (apart from leaving 'en' always at 1st position, it sorts alphabetically)
-
subDirs.ts
: to manage files and dirs, so that i18n locales' folders can be sourced automatically.
This allows for example to have an array of iso-codes if the ./locales
dir follows the normal convention.
Ex, this array of iso-codes [ "ar", "bn", "cy", "de", "en", "gd", "ja", "uk"]
is retrieved from the following tree:
│ ├── locales
│ │ ├── ar
│ │ │ ├── errors.json
│ │ │ ├── global.json
│ │ │ ├── landing.json
│ │ │ ├── layout.json
│ │ │ ├── search-company.json
│ │ │ ├── view-company-information.json
│ │ │ └── who-to-tell.json
│ │ ├── bn
│ │ │ └── *.json
│ │ ├── cy
│ │ │ └── *.json
│ │ ├── de
│ │ │ └── *.json
│ │ ├── en
│ │ │ └── *.json
│ │ ├── gd
│ │ │ └── *.json
│ │ ├── ja
│ │ │ └── *.json
│ │ ├── uk
│ │ │ └── *.json
-
manageLocales.middleware.ts
: middleware that can be reused to manage locales while dealing with http requests.
-
add-lang-to-url.njk
: to add the lang=xx
query param to urls:
https://.....<self>....../?lang=cy

locales-banner.njk
: to add the locales banner

ENV VARS
: the following ENV vars are used:
A menu navigation bar component has been added to make it easier to add links pointing to new services when needed. There are two Nunjucks macros: one that adds a menu navigation bar with the provided items and another that adds a menu navigation bar with predefined items. The latter internally uses the former and at the moment contains links to the following service:
- Authorised agent
- Your companies
- Your filings
- Companies you follow
- Basket
- Manage account
- Sign out
The "Authorised agent" menu item appears conditionally if the logged user has ACSP membership.
The navbar requires importing language files from the locales
folder in ch-node-utils and making them available to all Nunjuck templates using the addGlobal()
function in app.ts
. At the moment only English and Welsh language versions are supported. It also requires a flag displayAuthorisedAgent
set the same way for displaying the "Authorised agent" menu item. If the logged user has ACSP membership, this flag should be set to 'yes'
.
The navbar also requires the use of styles provided in the ch.gov.uk.css stylesheet (you can either add the provided link to the head section or include specific styles in your own stylesheet)
<link href="{{ cdnHost }}/stylesheets/ch.gov.uk.css" rel="stylesheet"/>
It also requires the navbar.js script to be added to the footer to make the navbar work in mobile mode
<script src="{{ cdnHost }}/javascripts/lib/navbar.js"></script>