So, I discovered a problem where if I had notes for Japanese and then say Simplified Chinese, Anki could display the incorrect kanji according to my system localisation settings. So, after searching a bit, I couldn’t find or come up with a nice solution.
Using Noto Sans CJK
/Noto Serif CJK
, there’s 5 variants for CJK characters: SC, TC, HK, JP and KR. I don’t really want to maintain 5 separate note types for each of these font variants, for each of my custom note types.
So, currently I added a Language
field which accepts a language code from a list of common language codes. This field is then parsed so I can add an @font-face
rule to overwrite Noto Sans CJK
/Noto Serif CJK
(by default, I’ve set it to Japanese).
(An idea that would make this much more pleasant is an addon that prefills contents of fields when adding to certain decks, say adding to Japanese would set Language to “jp-JA”. this aside…)
I’m wondering if there’s a better way to approach this problem because to me, embedding CSS in JS strings like this is ugly.
<!-- front side -->
<div id="language" class="hide">{{Language}}</div>
<!-- cardside stylesheet, different from Anki's card stylesheet -->
<style id="body-style"></style>
<script>
var languages = document.getElementById("language").innerText.split(" ");
/* Render correct kanji */
languages.forEach(function (language) {
var lang = language.split("-");
var ss = document.querySelector("#body-style");
if (language == "zh-HK") {
ss.append(
`@font-face {
font-family: Noto Sans CJK;
src: local(\"Noto Sans CJK HK\");
}\n
@font-face {
font-family: Noto Serif CJK;
src: local(\"Noto Serif CJK HK\");
}\n`
);
} else if (language == "zh-TW") {
ss.append(
`@font-face {
font-family: Noto Sans CJK;
src: local(\"Noto Sans CJK TC\");
}\n
@font-face {
font-family: Noto Serif CJK;
src: local(\"Noto Serif CJK TC\");
}\n`
);
} else if (lang[0] == "zh") {
ss.append(
`@font-face {
font-family: Noto Sans CJK;
src: local(\"Noto Sans CJK SC\");
}\n
@font-face {
font-family: Noto Serif CJK;
src: local(\"Noto Serif CJK SC\");
}\n`
);
} else if (lang[0] == "ko") {
ss.append(
`@font-face {
font-family: Noto Sans CJK;
src: local(\"Noto Sans CJK KR\");
}\n
@font-face {
font-family: Noto Serif CJK;
src: local(\"Noto Serif CJK KR\");
}\n`
);
}
});
</script>
/* card style */
.card {
font-family: Noto Sans, Noto Sans CJK, sans;
}
/* default to Japanese kanji */
@font-face {
font-family: Noto Sans CJK;
src: local("Noto Sans CJK JP");
}