Skip to content

Instantly share code, notes, and snippets.

@mingtsay
Created May 21, 2011 09:54
Show Gist options
  • Save mingtsay/984408 to your computer and use it in GitHub Desktop.
Save mingtsay/984408 to your computer and use it in GitHub Desktop.
[PHP] l18n/i10n Language
Open Source
You can use these code if you want to.
Please note that it is not allowed to use on commercial.
# Let all pages including the script `header.php`
php_value auto_prepend_file "header.php"
<?php
# The example for l18n
# mixed _gettext(string)
# If the data is not found in lang.ini, it will teturn false;
# for else, it will return the string.
$lang = "ja";
$text = _gettext("Language"); # Then $text will be "言語選択"
# array getLang(string)
# Put the val $_SERVER["HTTP_ACCEPT_LANGUAGE"] into this function
# and it will prase into an array; just like the following one:
#
# Array(
# "count" => 3,
# "lang" =>
# Array(
# 0 => "zh-tw",
# 1 => "en-us",
# 2 => "en"
# )
# )
$lang_r = getLang($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
# mixed getLangInArray(array, array)
# Put the return from the function `getLang` and it will find
# the kindest language for the users; if it doesn't find one,
# it will return false.
$langlist = array("en-us", "en", "zh-tw", "ja");
if(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
$language = getLangInArray(getLang($_SERVER["HTTP_ACCEPT_LANGUAGE"]), $langlist);
if(is_array($language)) $lang = $language["lang"];
else $lang = "en";
}
?>
<?php
# Will be included in header.php.
function getLang($strLang) {
$lng1 = explode(",", $strLang);
$i = 0;
while(isset($lng1[$i])) {
$lng2 = explode(";q=", $lng1[$i]);
$lng[$i] = strtolower($lng2[0]);
if(isset($lng2[1]))
$lngQ[$i] = (double)$lng2[1];
else
$lngQ[$i] = (double)1;
++$i;
}
$flag = true;
while($flag) {
$flag = false;
for($j = 0; $j < $i - 1; ++$j)
if($lngQ[$j] < $lngQ[$j + 1]) {
$tmpQ = $lngQ[$j]; $tmp = $lng[$j];
$lngQ[$j] = $lngQ[$j + 1]; $lng[$j] = $lng[$j + 1];
$lngQ[$j + 1] = $tmpQ; $lng[$j + 1] = $tmp;
$flag = true;
break;
}
}
$rtn = array('count' => $i, 'lang' => $lng);
return $rtn;
}
function getLangInArray($getLang, $array) {
for($i = 0; $i < $getLang['count']; ++$i) {
if(in_array(strtolower($getLang['lang'][$i]), $array))
return array('index' => $i, 'lang' => $getLang['lang'][$i]);
}
return false;
}
function _gettext($text) {
global $lang;
$langs = parse_ini_file(FILE_ROOT_PATH . "lang.ini", true);
return ((isset($langs[$lang][$text]))? ($langs[$lang][$text]): ((isset($langs['en'][$text]))? ($langs['en'][$text]): ($text)));
}
?>
<?php
# Will be included in each page by .htaccess.
ob_start();
define("PHP_ROOT_PATH", "/webdisk/");
define("HTML_ROOT_PATH", "/webdisk/");
define("FILE_ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "webdisk/");
$langlist = array(
"en-us",
"en",
"zh-tw",
"zh-hk",
"zh-cn",
"zh",
"ja",
);
require_once("functions.php");
session_save_path(FILE_ROOT_PATH . "session");
session_start();
if(!isset($_SESSION["language"]) || $_SESSION["language"] == "") {
$language = getLangInArray(getLang($_SERVER["HTTP_ACCEPT_LANGUAGE"]), $langlist);
if(is_array($language))
$_SESSION["language"] = $language["lang"];
else
$_SESSION["language"] = "en";
}
$lang = $_SESSION["language"];
?>
[en]
Webdisk = Webdisk
Webdisk Homepage = Webdisk Homepage
File Upload = File Upload
File List = File List
Logout = Logout
Login = Login
Sign Up = Sign Up
Contact Us = Contact Us
Language = Language
CU_Name = "Name: "
CU_Username = "Username: "
CU_E-Mail = "E-Mail: "
CU_Tell Us = "Tell Us: "
CU_Selected = "(Selected)"
Submit = Submit
About = About
Homepage = Homepage
MSG_RequestNotFound = "Sorry, but the page you request was not found in Webdisk. Maybe it wasn't created."
MSG_ForMoreHelp = For more help, you can contact with Ming Tsay.
Latest Uploaded File = Latest Uploaded File
Filename = Filename
Uploaded By = Uploaded By
Date n Time = "Date & Time"
FORM_Password = "Password: "
LostPasswd = Lost your password?
Successfully Login = You have successfully loged in.
Successfully Logout = You have successfully loged out.
Back To Homepage = "Click %shere%s or wait the browser taking you back to the homepage."
Back To Login = "Click %shere%s or wait the browser taking you back to the login page."
LOGIN_UsernamePassword = Please enter your Username and Password.
LOGIN_UsernameNotFound = "The Username you have entered hasn't been signed up."
LOGIN_PasswordIncorrect = You have entered a incorrect password.
MSG_DidNotFinish = "This function didn't finished yet."
Page Not Found = Page Not Found
[zh-tw]
Webdisk = 網路硬碟
Webdisk Homepage = 網路硬碟首頁
File Upload = 上傳檔案
File List = 檔案清單
Logout = 登出
Login = 登入
Sign Up = 註冊
Contact Us = 聯絡我們
Language = 變更語言
CU_Name = 姓名:
CU_Username = 使用者名稱:
CU_E-Mail = 電子信箱:
CU_Tell Us = 告訴我們:
CU_Selected = "(選填)"
Submit = 送出
About = 關於
Homepage = 首頁
MSG_RequestNotFound = 抱歉,我們找不到您瀏覽的頁面,也許該頁面尚未被建立。
MSG_ForMoreHelp = 您可以與 Ming Tsay 聯絡以取得更多資訊。
Latest Uploaded File = 最近上傳的檔案
Filename = 檔案名稱
Uploaded By = 上傳使用者
Date n Time = 日期與時間
FORM_Password = 密碼:
LostPasswd = 忘記密碼
Successfully Login = 您已成功登入。
Successfully Logout = 您已成功登出。
Back To Homepage = "您可以%s按此%s或等候您的瀏覽器轉跳回首頁。"
Back To Login = "您可以%s按此%s或等候您的瀏覽器轉跳回登入頁面。"
LOGIN_UsernamePassword = 請輸入您的使用者名稱及密碼。
LOGIN_UsernameNotFound = 您輸入的使用者名稱尚未被註冊。
LOGIN_PasswordIncorrect = 您輸入的密碼不正確。
MSG_DidNotFinish = 此功能尚未完成。
Page Not Found = 找不到頁面
[zh-hk]
[zh-cn]
Webdisk = 网路硬盘
Webdisk Homepage = 网路硬盘首页
File Upload = 上载挡案
File List = 挡案清单
Logout = 注销
Login = 登入
Sign Up = 注册
Contact Us = 联络我们
Language = 变更语言
CU_Name = 姓名:
CU_Username = 使用者名称:
CU_E-Mail = 电子邮箱:
CU_Tell Us = 告诉我们:
CU_Selected = "(选填)"
Submit = 送出
About = 关于
Homepage = 首页
MSG_RequestNotFound = 抱歉,我们找不到您浏览的页面,也许该页面尚未被建立。
MSG_ForMoreHelp = 您可以与 Ming Tsay 联络以取得更多资讯。
Latest Uploaded File = 最近上载的挡案
Filename = 挡案名称
Uploaded By = 上传使用者
Date n Time = 日子与时间
FORM_Password = 密码:
LostPasswd = 忘记密码
Successfully Login = 您已成功登入。
Successfully Logout = 您已成功注销。
Back To Homepage = "您可以%s按此%s或等候您的浏览器转跳回首页。"
Back To Login = "您可以%s按此%s或等候您的浏览器转跳回登入页面。"
LOGIN_UsernamePassword = 请输入您的使用者名称及密码。
LOGIN_UsernameNotFound = 您输入的使用者名称尚未被注册。
LOGIN_PasswordIncorrect = 您输入的密码不正确。
MSG_DidNotFinish = 此功能尚未完成。
Page Not Found = 找不到页面
[ja]
Language = 言語選択
<?php
# Changing the UI language.
ob_clean();
if(isset($_GET["lang"]) && in_array(strtolower($_GET["lang"]), $langlist))
$_SESSION["language"] = strtolower($_GET["lang"]);
header("Location: " . HTML_ROOT_PATH . "index.php");
header("HTTP/ 302");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment