CLOVER🍀

That was when it all began.

Vagrantで使っているUbuntu Linux 20.04を日本語化する

これは、なにをしたくて書いたもの?

Vagrantで、UbuntuのBoxはこちらを使っているのですが、ちょっと日本語にしたいなぁと思うことがあったので。

Vagrant box generic/ubuntu2004 - Vagrant Cloud

まあ、Vagrantで使うかどうかに限らず使えそうな気はしますが、今回書いた背景として。

初期状態

最初の状態を書いてお生きます。

datellの状態。

$ date
Mon 30 Nov 2020 07:05:02 AM UTC



$ ll
total 28
drwxr-xr-x 4 vagrant vagrant 4096 Nov 18 03:39 ./
drwxr-xr-x 3 root    root    4096 Nov 18 03:29 ../
-rw-r--r-- 1 vagrant vagrant  220 Nov 18 03:29 .bash_logout
-rw-r--r-- 1 vagrant vagrant 3775 Nov 18 03:39 .bashrc
drwxr-xr-x 2 vagrant vagrant 4096 Nov 18 03:39 .cache/
-rw-r--r-- 1 vagrant vagrant  807 Nov 18 03:29 .profile
drwx------ 2 vagrant vagrant 4096 Nov 30 07:04 .ssh/

現在のロケール。日本語ロケールは入っていません。

$ env | grep LANG
LANGUAGE=en_US:
LANG=en_US.UTF-8


$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US:
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=


$ localectl 
   System Locale: LANG=en_US.UTF-8
                  LANGUAGE=en_US:
       VC Keymap: n/a
      X11 Layout: us
       X11 Model: pc105

使えるロケールの一覧。

$ locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IL
en_IL.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
POSIX

タイムゾーンの設定。

$ cat /etc/timezone 
America/Los_Angeles


$ timedatectl 
               Local time: Mon 2020-11-30 07:05:31 UTC
           Universal time: Mon 2020-11-30 07:05:31 UTC
                 RTC time: Mon 2020-11-30 07:05:32    
                Time zone: UTC (UTC, +0000)           
System clock synchronized: yes                        
              NTP service: active                     
          RTC in local TZ: no

Asia/Tokyoタイムゾーンはインストールされています。

$ timedatectl list-timezones | grep Tokyo
Asia/Tokyo

manページを見た時の状態。

$ man grep
GREP(1)                                                                              User Commands                                                                              GREP(1)

NAME
       grep, egrep, fgrep, rgrep - print lines that match patterns

SYNOPSIS
       grep [OPTION...] PATTERNS [FILE...]
       grep [OPTION...] -e PATTERNS ... [FILE...]
       grep [OPTION...] -f PATTERN_FILE ... [FILE...]

DESCRIPTION
       grep  searches  for  PATTERNS  in  each  FILE.   PATTERNS  is one or more patterns separated by newline characters, and grep prints each line that matches a pattern.  Typically
       PATTERNS should be quoted when grep is used in a shell command.

       A FILE of “-” stands for standard input.  If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input.

       In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively.  These variants  are  deprecated,  but  are  provided  for
       backward compatibility.

OPTIONS
   Generic Program Information
       --help Output a usage message and exit.

〜省略〜

apt showを見た時の状態。

$ apt show grep
Package: grep
Version: 3.4-1
Priority: required
Essential: yes
Section: utils
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Anibal Monsalve Salazar <anibal@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 508 kB
Provides: rgrep
Pre-Depends: libc6 (>= 2.14), libpcre3
Depends: dpkg (>= 1.15.4) | install-info
Suggests: libpcre3 (>= 7.7)
Conflicts: rgrep
Homepage: http://www.gnu.org/software/grep/
Task: minimal
Download-Size: 151 kB
APT-Manual-Installed: yes
APT-Sources: http://us.archive.ubuntu.com/ubuntu focal/main amd64 Packages
Description: GNU grep, egrep and fgrep
 'grep' is a utility to search for text in files; it can be used from the
 command line or in scripts.  Even if you don't want to use it, other packages
 on your system probably will.
 .
 The GNU family of grep utilities may be the "fastest grep in the west".
 GNU grep is based on a fast lazy-state deterministic matcher (about
 twice as fast as stock Unix egrep) hybridized with a Boyer-Moore-Gosper
 search for a fixed string that eliminates impossible text from being
 considered by the full regexp matcher without necessarily having to
 look at every character. The result is typically many times faster
 than Unix grep or egrep. (Regular expressions containing backreferencing
 will run more slowly, however.)

環境

今回の環境は、冒頭にあったBoxの通り、Ubuntu Linux 20.04 LTSです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:    20.04
Codename:   focal


$ uname -srvmpio
Linux 5.4.0-54-generic #60-Ubuntu SMP Fri Nov 6 10:37:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Let's 日本語化

locale-genで、日本語用ロケールを作成します。

$ sudo locale-gen ja_JP.UTF-8
Generating locales (this might take a while)...
  ja_JP.UTF-8... done
Generation complete.

もしくは、まるっと日本語言語パックをインストールしてもいいです。

$ sudo apt install language-pack-ja

localeの一覧に現れるようになります。

$ locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IL
en_IL.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
ja_JP.utf8
POSIX

LANGおよびLANGUAGE環境変数を設定。

$ export LANG=ja_JP.UTF-8
$ export LANGUAGE=ja_JP:  ## 合わせておいた方がいい

dateなどの結果が日本語化されます。

$ date
2020年 11月 30日 月曜日 06:27:42 UTC


$ ll
total 28
drwxr-xr-x 4 vagrant vagrant 4096 11月 18 03:39 ./
drwxr-xr-x 3 root    root    4096 11月 18 03:29 ../
-rw-r--r-- 1 vagrant vagrant  220 11月 18 03:29 .bash_logout
-rw-r--r-- 1 vagrant vagrant 3775 11月 18 03:39 .bashrc
drwxr-xr-x 2 vagrant vagrant 4096 11月 18 03:39 .cache/
-rw-r--r-- 1 vagrant vagrant  807 11月 18 03:29 .profile
drwx------ 2 vagrant vagrant 4096 11月 30 06:18 .ssh/

システム全体の設定を変えるには、こちらで。

$ sudo update-locale LANG=ja_JP.UTF-8 LANGUAGE=ja_JP:

### または
$ sudo localectl set-locale LANG=ja_JP.UTF-8 LANGUAGE=ja_JP:

シェルに再ログイン後、日本語環境になります。

$ env | grep LANG
LANGUAGE=ja_JP:
LANG=ja_JP.UTF-8


$ localectl 
   System Locale: LANG=ja_JP.UTF-8
                  LANGUAGE=ja_JP:
       VC Keymap: n/a
      X11 Layout: us
       X11 Model: pc105


$ locale
LANG=ja_JP.UTF-8
LANGUAGE=ja_JP:
LC_CTYPE="ja_JP.UTF-8"
LC_NUMERIC="ja_JP.UTF-8"
LC_TIME="ja_JP.UTF-8"
LC_COLLATE="ja_JP.UTF-8"
LC_MONETARY="ja_JP.UTF-8"
LC_MESSAGES="ja_JP.UTF-8"
LC_PAPER="ja_JP.UTF-8"
LC_NAME="ja_JP.UTF-8"
LC_ADDRESS="ja_JP.UTF-8"
LC_TELEPHONE="ja_JP.UTF-8"
LC_MEASUREMENT="ja_JP.UTF-8"
LC_IDENTIFICATION="ja_JP.UTF-8"
LC_ALL=

タイムゾーンも変えてみましょうか。

## tzdataが入っていること
$ sudo timedatectl set-timezone Asia/Tokyo

確認。

$ cat /etc/timezone 
Asia/Tokyo


$ timedatectl 
               Local time: 月 2020-11-30 16:11:43 JST
           Universal time: 月 2020-11-30 07:11:43 UTC
                 RTC time: 月 2020-11-30 07:11:44    
                Time zone: Asia/Tokyo (JST, +0900)   
System clock synchronized: yes                       
              NTP service: active                    
          RTC in local TZ: no

dateの結果がJSTになりました。

$ date
2020年 11月 30日 月曜日 16:14:26 JST

日本語manページのインストール。

$ sudo apt install manpages-ja manpages-ja-dev

manページが日本語化されます。

$ man grep
GREP(1)                                                                         General Commands Manual                                                                         GREP(1)

名前
       grep, egrep, fgrep - パターンにマッチする行を表示する

書式
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

説明
       grep  は FILE で名前を指定されたファイルを検索して、与えられた PATTERN にマッチする部分を含む行を探します。 ファイルが指定されていない場合や、 ファイル名の代わりに 1 個のマイナ
       ス記号 “-” が指定されている場合は、 grep は標準入力から検索します。 デフォルトでは、 grep はマッチした行を表示します。

       さらに、兄弟プログラム egrep と fgrep は、それぞれ grep -E と grep -F と同じです。 これらの兄弟プログラムは非推奨ですが、後方互換性のために用意されています。

オプション
   プログラムについての一般情報
       --help 使用法を出力して終了します。

       -V, --version
              grep のバージョン番号を出力して終了します。

〜省略〜

最後に、apt系の結果について。LANGを指定してapt updateするか

$ sudo LANG=ja_JP.UTF-8 apt update

または、以下のファイルを作成して
/etc/apt/apt.conf.d/99translations

Acquire::Language "ja_JP";

apt update

$ sudo apt update

すると、apt showの結果も日本語化されます。

$ apt show grep
Package: grep
Version: 3.4-1
Priority: required
Essential: yes
Section: utils
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Anibal Monsalve Salazar <anibal@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 508 kB
Provides: rgrep
Pre-Depends: libc6 (>= 2.14), libpcre3
Depends: dpkg (>= 1.15.4) | install-info
Suggests: libpcre3 (>= 7.7)
Conflicts: rgrep
Homepage: http://www.gnu.org/software/grep/
Task: minimal
Download-Size: 151 kB
APT-Manual-Installed: yes
APT-Sources: http://us.archive.ubuntu.com/ubuntu focal/main amd64 Packages
Description: GNU grep, egrep, fgrep
 'grep' はファイル内の文字列を検索するユーティリティです。コマンドラインか
 ら、またスクリプトから使用できます。あなたが使用しないとしても、おそらくあ なたのシステムにある他のパッケージが使用するでしょう。
 .
 grep ユーティリティの GNU ファミリは "西洋最速の grep" かもしれません。GNU grep は、高速な遅延状態決定性照合器 (元祖
 Unix の egrep より約 2 倍速い) を 元に固定長文字列に対して Boyer-Moore-Gosper 検索を併用し、一致するはずのな
 い文字列を完全正規表現照合器で除いており、不必要なすべての文字の探索はしな いようになっています。このため、一般には Unix の grep や
 egrep より数倍高速 です (しかし後方参照を含む正規表現では他より遅くなるでしょう)。

apt updateで日本語翻訳分を取得した後は、こんなファイルができます。そして、その後更新されていきます。

$ ll /var/lib/apt/lists/*ja*
-rw-r--r-- 1 root root 1835063  4月 24  2020 /var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_focal_main_i18n_Translation-ja
-rw-r--r-- 1 root root   25506  4月 20  2020 /var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_focal_multiverse_i18n_Translation-ja
-rw-r--r-- 1 root root 8665683  4月 24  2020 /var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_focal_universe_i18n_Translation-ja