Gentooでlocale周りのエラーが出た場合

Gentooで `/etc/env.d/02locale` が正しく設定されてなくてlocale周りのエラーが出たので対処方法をメモ。

----

USEフラグの管理には[flaggie](http://d.hatena.ne.jp/meech/20120221/1329839829)を使いますが、こんな感じのエラーを吐いてしまいました。

```shell-session
localhost ~ # flaggie -perl dev-vcs/git
Traceback (most recent call last):
  File "/usr/lib/python-exec/python3.3/flaggie", line 25, in <module>
    sys.exit(main(sys.argv))
  File "/usr/lib64/python3.3/site-packages/flaggie/cli.py", line 80, in main
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib64/python3.3/locale.py", line 541, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
```

さっそくlocale設定を確認。

```shell-session
localhost ~ # eselect locale list
/usr/bin/locale: Cannot set LC_CTYPE to default locale: No such file or directory
Available targets for the LANG variable:
  [1]   C
  [2]   POSIX
  [3]   en_US
  [4]   en_US.iso88591
  [5]   en_US.utf8
  [6]   ja_JP
  [7]   ja_JP.eucjp
  [8]   ja_JP.ujis
  [9]   ja_JP.utf8
  [10]  japanese
  [11]  japanese.euc
  [ ]   (free form)
```

設定されてないですね。
`ja_JP.utf8` にでも設定して、 `env-update` && `source /etc/profice` 。

```shell-session
localhost ~ # eselect locale set 9
/usr/bin/locale: Cannot set LC_CTYPE to default locale: No such file or directory
Setting LANG to ja_JP.utf8 ...
Run ". /etc/profile" to update the variable in your shell.
localhost ~ # env-update 
>>> Regenerating /etc/ld.so.cache...
localhost ~ # source /etc/profile
```

しかし同じエラーが・・・。
なんか `LC_ALL` を読もうとして失敗してる…?

```shell-session
localhost ~ # flaggie 
Traceback (most recent call last):
  File "/usr/lib/python-exec/python3.3/flaggie", line 25, in <module>
    sys.exit(main(sys.argv))
  File "/usr/lib64/python3.3/site-packages/flaggie/cli.py", line 80, in main
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib64/python3.3/locale.py", line 541, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
```

`eselect locale set` で設定されるのは `LANG` だけのようなので、

```shell-session
localhost ~ # grep -E '^\s*[^#;]' /etc/env.d/02locale 
LANG="ja_JP.utf8"
```

`LC_ALL` を書いてみます。
再び `env-update` && `source /etc/profice` ！

```shell-session
localhost ~ # vi /etc/env.d/02locale 
localhost ~ # grep -E '^\s*[^#;]' /etc/env.d/02locale 
LC_ALL="ja_JP.utf8"
LANG="ja_JP.utf8"
localhost ~ # env-update 
>>> Regenerating /etc/ld.so.cache...
localhost ~ # source /etc/profile
```

こんどはうまくいきました♪
`flaggie` って色んなオプションあるんですね。

```shell-session
localhost ~ # flaggie 
Synopsis:
flaggie [<options>] [<global-actions>] [<packages> <actions>] [...]

Options:
	--quiet			Silence argument errors and warnings
	--strict		Abort if at least a single flag is invalid

	--drop-ineffective	Drop ineffective flags (those which are
				overriden by later declarations)
	--sort-entries		Sort package.* file entries by package
				(please note this will drop comments)
	--sort-flags		Sort package.* flags by name
	--sort			Shorthand for --sort-entries and --sort-flags
	--cleanup		Shorthand for --drop-ineffective and --sort

	--drop-unmatched-pkgs	Drop packages which no longer are available
				in portdb
	--drop-unmatched-flags	Drop flags which are not found in package's
				IUSE, KEYWORDS and/or LICENSE variables
	--destructive-cleanup	Shorthand for all of the above

	--migrate-files		Migrate the outdated files to newer variants
				(package.keywords -> package.accept_keywords)
		
Global actions are applied to the make.conf file. Actions are applied to
the package.* files, for the packages preceding them.

An action can be one of:
	+arg	explicitly enable arg
	-arg	explicitly disable arg
	%arg	reset arg to the default state (remove it from the file)
	?arg	print the effective status of arg (due to the file)

The action argument must be either a USE flag, a keyword or a license name.
For the '%' and '?' actions, it can be also one of 'use::', 'kw::' or 'lic::'
in order to apply the action to all of the flags, keywords or licenses
respectively.

A package specification can be any atom acceptable for Portage (in the same
format as taken by emerge).
```