Introduction
After going international, I didn’t pay much attention to posts in other languages. That is, until I saw some nice Japanese fonts while browsing. I had to snag them, only to realize the fonts for my other languages were… well, pretty rough.
Importing Fonts
Google Fonts
Using a service like Google Fonts is usually the fastest and easiest way.
For example, I’m using Comic Neue, Noto Sans JP, Noto Sans SC, Noto Sans TC, and Yomogi. After selecting them, Google Fonts generates an HTML tag to drop into your <head>.
| |
Custom Imports
You can also host your own .ttf, .woff, or .woff2 files. I recommend .woff2 or .woff for smaller file sizes. (Advanced optimization involves font subsetting/splitting, which is how Google Fonts handles it).
I’m using the
Yozai Font
. The original .ttf is massive, so I compressed it using a tool like
Transfonter
before importing.
First, place the font files in a directory, then define the font family in your SCSS.
Here’s my setup. Usually, .woff2 and .woff are enough. I kept .ttf for legacy browser support (honestly, I just uploaded it before I knew better and had to optimize later).
Set font-display: swap; so the browser uses a fallback font while downloading. This prevents blank text and improves the user experience.
| |
Where to Put It
For Google Fonts, my theme has a custom.html for code that needs to go into the <head>.
For self-hosted files, the theme provides a custom.scss.
Font configurations are handled in variables.scss in my theme, so I modified that directly.
Configuring Fonts
Set specific fonts based on the language.
| |
I added font variables for other languages following the existing naming convention. Then, I used the :lang selector to dynamically swap fonts based on the lang attribute of the <html> tag.
| |