Elegant i18n for React in TypeScript
hermes-react is a TypeScript library for React dedicated to i18n - especially elegant decorated typography, for example romanization of non-latin writing systems - inspired by my general-purpose hermes library.
The package on NPM is:
The public API entirely resides in the root package index, so you shouldn't reference specific modules.
The library comes with a ready-made CSS stylesheet - providing typographic enhancements such as pinyin tone colors - which you can optionally reference in your projects.
In particular, if you are using Next.js, you just need to add one line to the import block of your
Once referenced the CSS stylesheet, if you start decorating characters with the provided metadata - such as the ones in the
Pinyin namespace - you'll see them rendered in color by the
<DecoratedText/> is a component with the following properties:
text- the text to be formatted
metadataByChar- mapping each decorated character to its typographic metadata
metadataByChar is any
RMap (provided by the Rimbu library) whose entries have:
for key, a single character that should be decorated
for value, the related
CharMetadataobject containing at least one of:
annotation string- to be printed on top of the character
class namestring used to style the whole character box
The component formats the given
text according to the following algorithm:
The text is trimmed
\r\nis converted to
\nat the end of a
\n\nsequence is ignored
The text is split into paragraphs -
<p>blocks - separated by
\n\nsequences that are used as separators and then ignored
Within every paragraph:
\nis mapped to
for every other character:
if the character has metadata in the
the related annotation string is printed on top of the character
the whole structure is packed into a
<ruby>tag, styled with the (optional) requested classes
otherwise, the character is printed verbatim, with no additional HTML tags
Pinyin - Simplified Chinese
When annotating a pinyin logogram, you can associate to its
className metadata one of the following values (actually, strings) of the
If you need more granular control, you can reference the constants declared in the
PinyinTone namespace - having the same names but providing just the tone class name.
If you import the stylesheet provided by the library, the component will render the characters in color.
Glyph character classes
Glyph namespace provides general-purpose class names - such as
logogram, ..., which you can use to style your decorated characters.