國際化:redhat7 如何設定系統語言
公司與華為合作在科威特實施一個專案,之前部門做的專案都是國內的,還從來沒有做過國外的,所以公司領導特別重視,只許成功不許失敗;
首先要做的就是國際化的支援,之前系統只有部分做過國際化,而且很多做的不完善,有的只有中文資原始檔,沒有英文資原始檔,有的直接就沒做;這個任務落在我和另一位同事身上,我負責做國際化,同事負責國際化後介面的調整;這麼多一個一個去改是不可能的,工程量巨大,而且還會遺漏,這種活怎麼能讓我們這懶懶的程式猿去做,於是寫了三個國際化用的掃描程式,分別掃描java,js和jsp檔案,提取出其中的中文替換為國際化的key,同時生成國際化資原始檔,然後拿給翻譯去翻;最後將個別沒有掃描到的人工修改;
除了系統介面,還有一部分需要做國際化!那就後臺任務,自動傳送報表、報警郵件、簡訊之類的功能,這個改寫成了根據伺服器本地的設定來選擇語言;
全部改好了後,本來以為萬事大吉,只欠東風了,結果實施的童鞋發難了,如果對方伺服器預設語言不是英文怎麼辦,那好辦,度娘一下全部解決,只需要修改linux系統檔案/etc/sysconfig/i18n
LANG="en_US.UTF-8"
然後執行一下source /etc/sysconfig/i18n即可;
後來客戶說要在redhat7上進行安裝,實施的童鞋為了確保萬無一失,專門做了個redhat7的系統,安裝了一遍,更改系統語言的時候發現竟然沒有/etc/sysconfig/i18n這個檔案;於是自己建立了一個,放上去,為了驗證器正不正確,先改為了中文,結果沒有起作用,系統傳送的郵件仍然是英文郵件,於是各種百度;發現全都是更改/etc/sysconfig/i18n這個檔案,要麼就是在桌面上更改,我去,我們伺服器端安裝桌面是不可能的;看樣子還是自己動手豐衣食足;
想了一下解決思路,既然redhat7沒有i18n這個檔案那麼有可能是寫在別的地方了, 我們要找的就是看看/etc/sysconfig/i18n會在什麼時候載入,找到其他系統載入這個檔案的位置,再去看redhat7上是不是也在這個位置載入了其它檔案,想了一下linux系統啟動時載入配置檔案的過程
- /etc/profile
- ~/.bash_profile | ~/.bash_login | ~/.profile
- ~/.bashrc
- /etc/bashrc
- ~/.bash_logout
先從/etc/profile開始檢視
vi /etc/profile,在該檔案的67行,有了這樣的發現
for i in <span style="background-color: rgb(255, 255, 102);"><span style="color:#FF0000;">/etc/profile.d/*.sh</span></span> ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null 2>&1
fi
fi
done
原來它會執行/etc/profile.d/目錄下所有的 *.sh指令碼檔案;
於是進入到/etc/profile.d/ 目錄:
[root@localhost profile.d]# ll
total 44
-rw-r--r--. 1 root root 1127 Oct 17 2013 colorls.csh
-rw-r--r--. 1 root root 1143 Oct 17 2013 colorls.sh
-rw-r--r--. 1 root root 192 Aug 27 2013 glib2.csh
-rw-r--r--. 1 root root 192 Aug 27 2013 glib2.sh
-rw-r--r--. 1 root root 1741 Nov 23 2013 lang.csh
<span style="color:#FF0000;">-rw-r--r--. 1 root root 2706 Nov 23 2013 lang.sh</span>
-rw-r--r--. 1 root root 122 Feb 7 2007 less.csh
-rw-r--r--. 1 root root 108 Feb 7 2007 less.sh
-rw-r--r--. 1 root root 97 Apr 5 2012 vim.csh
-rw-r--r--. 1 root root 269 Apr 5 2012 vim.sh
-rw-r--r--. 1 root root 169 May 20 2009 which2.sh
一眼就瞄到了lang.sh,開啟它看看裡面有什麼內容:
# /etc/profile.d/lang.sh - set i18n stuff
sourced=0
if [ -n "$LANG" ]; then
saved_lang="$LANG"
[ -f "$HOME/.i18n" ] && . "$HOME/.i18n" && sourced=1
LANG="$saved_lang"
unset saved_lang
else
for langfile in <span style="color:#FF0000;">/etc/sysconfig/i18n</span> <span style="color:#FF0000;">"$HOME/.i18n"</span> ; do
[ -f $langfile ] && . $langfile && sourced=1
done
fi
來的真是全不費功夫,在開頭的位置發現了標紅的這兩個檔案/etc/sysconfig/i18bn 和 使用者目錄下的.i18n檔案;
然後如出一轍,在redhat7上順藤摸瓜找到了這個檔案lang.sh開啟一看
else
for langfile in <span style="background-color: rgb(255, 255, 153);"><span style="color:#FF0000;">/etc/locale.conf</span></span> <span style="color:#FF0000;">"$HOME/.i18n"</span> ; do
[ -f $langfile ] && . $langfile && sourced=1
done
fi
原來redhat7已經將系統語言的設定遷移到了/etc/locale.conf檔案裡
開啟這個檔案,設定為中文,重啟服務,問題解決!