# Solution for locale and character encoding issues in Japanese games on Steam Deck

> Source: <https://gist.github.com/cfillion/4394c3b8cd051fb45721187053e92296>
> Published: 2022-08-22 05:20:40+00:00

# Problem 1

Cannot access filenames containing non-ASCII characters.

Steam Deck's SteamOS 3 provides only one locale named `en_US.utf8` (as configured in /etc/locale.gen) but /etc/locale.conf sets `LANG` to `en_US.UTF-8`. They don't match, so the effective locale for programs is `C` and the character set becomes limited to ASCII.

Set the game's launch options in Steam to `LANG=en_US.utf8 %command%` to solve this.

To fix this for all applications running in desktop mode (eg. to correctly extract games), create a file named `~/.config/plasma-workspace/env/utf8.sh` containing (restart desktop mode to apply):

```sh
#!/bin/sh
export LANG=en_US.utf8
```

# Problem 2

Japanese text displays as mojibake or question marks. Some games may even completely fail to load due to script compilation errors (common in visual novels). The Windows ANSI codepage must be set to SHIFT-JIS to fix this.

Wine uses the current system locale (set via `LANG`) or the `LC_ALL` environment variable to determine which ANSI codepage to use. This is equivalent to using Locale Emulator on Windows.

Steam Deck's SteamOS 3 doesn't provide a `ja_JP.UTF-8` locale so this leaves `LC_ALL`.

However Steam may override it, so instead set the game's launch options to `HOST_LC_ALL=ja_JP.UTF-8 %command%` to have Proton set `LC_ALL` to the desired value.

# Problem 3

The simple solutions shown above to problem 1 and 2 are unfortunately mutually exclusive.

`ja_JP.UTF-8` is not provided by SteamOS 3 so requesting it makes the effective Unix locale fallback to `C` (ASCII) again.

# Complete solution

This sets the Wine codepage to Japanese and the Unix character set to UTF-8 without requiring root access or unlocking the readonly system partition of the Steam Deck.

1. Extract the archive [steam-deck-jp-locale-utf8.tar.zst](https://gist.github.com/cfillion/4394c3b8cd051fb45721187053e92296/raw/28178bed85fb027e99a4cfae431a36240f21acd8/steam-deck-jp-locale-utf8.tar.zst) using Ark into the home directory on the Steam Deck (`/home/deck`).
2. Double-check that `/home/deck/locales/run.sh` is executable (right click > properties > permissions).
3. Set the game's launch options to `LANG=ja_JP.UTF-8 ~/locales/run.sh %command%` to fix locale and encoding issues.

## Alternative steps

Follow these steps instead to generate the files manually. A full install of Arch Linux is required (commands may need adjustments to work in other distributions).  

1. On an Arch Linux install:
    1. Uncomment `ja_JP.UTF-8` in /etc/locale.gen
    2. Run the following commands:
       ```sh
       sudo locale-gen

       mkdir locales
       export LOCPATH="$HOME/locales"
       localedef -f UTF-8 -i ja_JP "$LOCPATH/ja_JP.UTF-8"
       ```
2. Transfer the `locales` directory created above into `/home/deck` on your Steam Deck (SteamOS 3).
3. Create a file named `run.sh` inside that folder containing:
   ```sh
   #!/bin/bash
   # https://gist.github.com/cfillion/4394c3b8cd051fb45721187053e92296
   newlocpath=$(dirname "$(realpath "$0")")
   script='
     BEGIN { FS=OFS=" '"'--'"' " }
     NF > 1 { for(i = 1; i < NF; ++i) printf "%s", $i OFS; }
     { print "env LOCPATH='"'"'" newlocpath "'"'"' " $NF }
   '
   command="$(awk -v newlocpath="$newlocpath" "$script" <<< "${@@Q}")"
   eval "$command"
   ```
4. Make `run.sh` executable: `chmod +x ~/locales/run.sh`
5. Set the game's launch options to `LANG=ja_JP.UTF-8 ~/locales/run.sh %command%` to fix locale and encoding issues.

# Sources and tips

- https://github.com/wine-mirror/wine/blob/120ca2ff52d145faa925a41d494035842d47a967/dlls/ntdll/unix/env.c#L804-L805
- https://github.com/wine-mirror/wine/blob/120ca2ff52d145faa925a41d494035842d47a967/dlls/ntdll/unix/env.c#L715-L719
- https://github.com/ValveSoftware/Proton/blob/4221d9ef07cc38209ff93dbbbca9473581a38255/proton#L1081-L1087
- `PROTON_LOG=1 WINEDEBUG=+file,+nls,+msgbox %command%` to troubleshoot file accesses, locales/character set conversions and message box contents.

