-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
2 deletions.
There are no files selected for viewing
41 changes: 39 additions & 2 deletions
41
src/components/TransliteratorElement/TransliteratorElement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,42 @@ | ||
import React from 'react'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import {transliterate} from "@interslavic/utils"; | ||
|
||
const CYRILLIC_ALPHABET_LANGUAGES = new Set(['be', 'bg', 'mk', 'ru', 'sr-Cyrl', 'uk']); | ||
|
||
export function TransliteratorElement({ children }) { | ||
// TODO: add support for different transliteration systems | ||
return <span lang="art-x-interslv" translate="no" className="notranslate">{children}</span>; | ||
const { i18n } = useDocusaurusContext(); | ||
const isCyrillic = CYRILLIC_ALPHABET_LANGUAGES.has(i18n.currentLocale); | ||
|
||
const transliterateText = (text) => { | ||
return transliterate(text, 'art-Cyrl-x-interslv-etym').replaceAll('Ѣ', 'Є').replaceAll('ѣ', 'є'); | ||
}; | ||
|
||
const transliterateJSX = (jsxElement) => { | ||
if (typeof jsxElement === 'string') { | ||
return transliterateText(jsxElement); | ||
} | ||
|
||
if (React.isValidElement(jsxElement)) { | ||
return React.cloneElement(jsxElement, { | ||
children: React.Children.map(jsxElement.props.children, (child) => { | ||
if (typeof child === 'string') { | ||
return transliterateText(child); | ||
} | ||
return transliterateJSX(child); | ||
}), | ||
}); | ||
} | ||
|
||
return jsxElement; | ||
}; | ||
|
||
return ( | ||
<span lang="art-x-interslv" translate="no" className="notranslate"> | ||
{isCyrillic | ||
? React.Children.map(children, transliterateJSX) | ||
: children} | ||
</span> | ||
); | ||
} | ||
|