Skip to content

Instantly share code, notes, and snippets.

@shogo82148
Created November 3, 2012 03:56
Show Gist options
  • Save shogo82148/4005784 to your computer and use it in GitHub Desktop.
Save shogo82148/4005784 to your computer and use it in GitHub Desktop.
Linux等のメッセージを自由に変えるスクリプト
#!/bin/sh
# -*- coding: utf-8 -*-
# .moファイルのあるディレクトリ。適宜変えてください。
LOCALE_DIR='/usr/share/locale'
LOCALE='ja'
backup_dir="`pwd`/mo_backup"
pattern_file=''
usage() {
echo "usage: $0 [OPTION]... -p PATTERN_FILE" >&2
exit
}
opt=`getopt -o p:b -l pattern:,backup-dir: -- $*`
eval set -- $opt
while [ -n "$1" ]; do
case "$1" in
-p|--pattern)
pattern_file="$2"
shift 2
;;
-b|--backup-dir)
backup_dir="$2"
shift 2
;;
--)
break
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
[ -z "$pattern_file" ] && usage
mkdir -p "$backup_dir"
replace_mo() {
mofile="$1"
pofile=`mktemp --suffix=.po`
msgunfmt "$mofile" -o "$pofile"
sed -f "$pattern_file" "$pofile" > "$pofile.new"
mv "$pofile".new "$pofile"
cp -v "$mofile" "$backup_dir/" # バックアップを作成
# root以外で起動した場合はsudoを付ける
if [ `whoami` = root ]; then
sudo=''
else
sudo='sudo'
fi
$sudo msgfmt -o "$mofile" "$pofile"
echo "Replacing $mofile ..."
rm "$pofile"
}
find "$LOCALE_DIR/$LOCALE/LC_MESSAGES/" | while read line; do
# .moファイルか確認
if echo "$line" | grep '.mo$' > /dev/null; then
replace_mo "$line"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment