Skip to content

Commit 37bb96c

Browse files
fehmerMiodec
andauthored
feat: allow languages to set specific fonts (@Shamsuddin45, @fehmer) (monkeytypegame#8208)
Co-authored-by: Jack <jack@monkeytype.com>
1 parent f4cc210 commit 37bb96c

6 files changed

Lines changed: 24 additions & 7 deletions

File tree

docs/LANGUAGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The contents of the file should be as follows:
2424
"joiningScript": boolean,
2525
"orderedByFrequency": boolean,
2626
"bcp47": string,
27+
"preferredFont":string,
2728
"words": string[]
2829
}
2930
```
@@ -34,6 +35,7 @@ It is recommended that you familiarize yourselves with JSON before adding a lang
3435
For `bcp47` put your languages [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag).
3536
If the words you're adding are ordered by frequency (most common words at the top, least at the bottom) set the value of `orderedByFrequency` to `true`, otherwise `false`.
3637
Finally, add your list of words to the `words` field.
38+
If the language is not rendered correctly, you can provide a `preferredFont`. The font needs to be one of monkeytypes fonts. Check the [fonts documentation](./FONTS.md).
3739

3840
Then, go to `packages/schemas/src/languages.ts` and add your new language name at the _end_ of the `LanguageSchema` enum. Make sure to end the line with a comma. Make sure to add all your language names if you have created multiple word lists of differing lengths in the same language.
3941

frontend/src/ts/ui.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { qs, qsr } from "./utils/dom";
1717
import { createEffect } from "solid-js";
1818
import fileStorage from "./utils/file-storage";
1919
import { convertRemToPixels } from "./utils/numbers";
20+
import { getLanguage } from "./utils/json-data";
21+
import { replaceUnderscoresWithSpaces } from "./utils/strings";
2022
import { isTestActive } from "./states/test";
2123

2224
let isPreviewingFont = false;
@@ -30,7 +32,7 @@ export function previewFontFamily(font: FontName): void {
3032
}
3133

3234
export async function applyFontFamily(): Promise<void> {
33-
let font = Config.fontFamily.replace(/_/g, " ");
35+
let font = replaceUnderscoresWithSpaces(Config.fontFamily);
3436

3537
const localFont = await fileStorage.getFile("LocalFontFamilyFile");
3638
if (localFont === undefined) {
@@ -49,10 +51,19 @@ export async function applyFontFamily(): Promise<void> {
4951
}`);
5052
}
5153

52-
document.documentElement.style.setProperty(
53-
"--font",
54-
`"${font}", "Roboto Mono", "Vazirharf", monospace`,
55-
);
54+
const preferredFont = (await getLanguage(Config.language))?.preferredFont;
55+
56+
const fonts = [
57+
font,
58+
preferredFont !== undefined
59+
? `"${replaceUnderscoresWithSpaces(preferredFont)}"`
60+
: undefined,
61+
'"Roboto Mono"',
62+
'"Vazirharf"',
63+
"monospace",
64+
].filter((it) => it !== undefined);
65+
66+
document.documentElement.style.setProperty("--font", fonts.join(","));
5667
}
5768

5869
export function clearFontPreview(): void {
@@ -136,7 +147,7 @@ createEffect(() => {
136147
});
137148

138149
configEvent.subscribe(async ({ key }) => {
139-
if (key === "fontFamily") {
150+
if (key === "fontFamily" || key === "language") {
140151
await applyFontFamily();
141152
}
142153
});

frontend/static/languages/lao.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"joiningScript": false,
55
"orderedByFrequency": false,
66
"bcp47": "lo",
7+
"preferredFont": "Noto_Sans_Lao",
78
"words": [
89
"ຂ້ອຍ",
910
"ເຈົ້າ",

frontend/static/languages/sindhi.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"joiningScript": true,
55
"orderedByFrequency": false,
66
"bcp47": "sd",
7+
"preferredFont": "Noto_Naskh_Arabic",
78
"words": [
89
"جي",
910
"۾",

packages/schemas/src/fonts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { z } from "zod";
22
import { customEnumErrorHandler } from "./util";
33

4-
const KnownFontNameSchema = z.enum(
4+
export const KnownFontNameSchema = z.enum(
55
[
66
"Roboto_Mono",
77
"Noto_Naskh_Arabic",

packages/schemas/src/languages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from "zod";
2+
import { KnownFontNameSchema } from "./fonts";
23
import { customEnumErrorHandler } from "./util";
34

45
export const LanguageSchema = z.enum(
@@ -469,6 +470,7 @@ export const LanguageObjectSchema = z
469470
.array(z.tuple([z.string().min(1), z.string().min(1)]))
470471
.optional(),
471472
bcp47: z.string().optional(),
473+
preferredFont: KnownFontNameSchema.optional(),
472474
originalPunctuation: z.boolean().optional(),
473475
})
474476
.strict();

0 commit comments

Comments
 (0)