在多语言版本的PHP程序中,使用变量,还是宣常量显示页面内容好?为什么? 在多语言版本的PHP程序中,使用变量,还是使用常量显示页面内容好?为什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如:$userid = "用户名";ORdefine("USERID", "用户名"); 你定义一个$lang的数组,不同的语言调用不同的$lang 用模板+数组语言包,给你个例子://index.phprequire "zh-cn.php";$tpl = new Smarty();$tpl->assign( $lang );$tpl->assign( $errors );$tpl->display( 'index.tpl' );//index.tpl<title>{$logging_page_title}</title><h2>用户登陆</h2><form action="logging.php" name="logging" method="post">{$logging_email_title}<input type="text" name="email_addr" value="{php}echo(isset($_POST['email_addr'])?$_POST['email_addr']:'');{/php}"/><br><br>{$logging_password_title}<input type="password" name="password" value=""/> {if $user eq "error"}<font color="red">{$logging_error_password}</font>{/if}<br><br>{$logging_savetime_title}<select name="customer_id"> {html_options options=$cust_options selected=$customer_id}</select><br><br>{$register_check_code_title}<br><input type="text" name="check_code" value=""/>  {$register_check_code_notice} <img src="include/verification_img.php"/><br>{$logging_check_code_notice}{if $verification eq "error"}<font color="red">{$logging_error_check_code}</font>{/if}<br><hr><input type="submit" name="login" value="{$logging_button_lable}"/><br><br><br>{$logging_page_notice}</form>//zh-cn.php$lang = array( "register_page_title" => " 用户注册", "register_email_title" => "您的Email地址:", "register_email_notice" => "*请你输入有效的Email", "register_password_title" => "给自己设一个密码:", "register_password_notice" => "*密码至少6位,由字母和数字组成", "register_re_password_title" => "确认您的密码:", "register_nickname_title" => "给自己起一个昵称:", "register_nickname_notice" => "中、英文均可( 4-20个字符 )。", "register_check_code_title" => "验证码:", "register_check_code_notice" => "(请输入右图中数字)", "register_agree_notice" => "同意本站的<a herf='#'>服务条款</a>", "register_button_lable" => "完 成 注 册", "logging_page_title" => "用户登陆", "logging_email_title" => "Email地址:", "logging_password_title" => "登陆密码:", "logging_savetime_title" => "保存期限:", "register_check_code_title" => "效检码:", "register_check_code_notice" => "请输入右图中数字", "logging_button_lable" => "登 陆", "logging_page_notice" => "注:登陆后,跳转到首页。");$errors = array( "register_error_email" => "此email已经被注册!", "register_error_password" => "密码不能为空且两次密码输入必须相同,请重新输入!", "register_error_nickname" => "此昵称已经被注册", "register_error_check_code" => "验证码错误!", "register_error_argee" => "您没有接受本站服务条款!", "logging_error_password" => "密码不匹配!", "logging_error_check_code" => "验证码错误!");当需要改变语言的时候,你只需要改变下头部的require "zh-cn.php";并确保你已经定义好其他的语言包,如zh-en.php 都可以使用常量较使用独立变量可以有更多的有意义的变量名可用使用常量时,要注意判断常量是否已定义楼上的都是使用数组的,这已不在你的问题之列了何况$lang['register_page_title']也没有$register_page_titleREGISTER_PAGE_TITLE来的顺眼 或者你再加上cache,吧替换后的模版缓存,让更换语言的时候刷新缓存 gettext只能用到linux上。windows下不可用。我现在一直把每个语言放在单独的文件中。然后每个文件里用数组来保存值。 php escape乱码问题,求解决 这么设置宽屏和窄屏的切换 php mysql 获得字段最大值赋给一个变量的问题 请问这样的怎么排列呢?? 如何自定义函数处理Fatal error? session问题,高分求救,在线等 求微信公众平台代码的漏洞测试! PHP如何访问Access?一定给分~~ 为什么我的 PHP 不能显示图形呢? 使用phpexcel导出excel乱码 http和https的转化问题? 根据IP判断地区
$userid = "用户名";ORdefine("USERID", "用户名");
//index.php
require "zh-cn.php";
$tpl = new Smarty();
$tpl->assign( $lang );
$tpl->assign( $errors );
$tpl->display( 'index.tpl' );//index.tpl
<title>{$logging_page_title}</title>
<h2>用户登陆</h2>
<form action="logging.php" name="logging" method="post">
{$logging_email_title}
<input type="text" name="email_addr" value="{php}echo(isset($_POST['email_addr'])?$_POST['email_addr']:'');{/php}"/><br><br>
{$logging_password_title}
<input type="password" name="password" value=""/> {if $user eq "error"}<font color="red">{$logging_error_password}</font>{/if}<br><br>
{$logging_savetime_title}
<select name="customer_id">
{html_options options=$cust_options selected=$customer_id}
</select>
<br><br>
{$register_check_code_title}<br>
<input type="text" name="check_code" value=""/>  {$register_check_code_notice} <img src="include/verification_img.php"/><br>{$logging_check_code_notice}{if $verification eq "error"}<font color="red">{$logging_error_check_code}</font>{/if}<br><hr>
<input type="submit" name="login" value="{$logging_button_lable}"/><br><br><br>{$logging_page_notice}
</form>//zh-cn.php
$lang = array(
"register_page_title" => " 用户注册",
"register_email_title" => "您的Email地址:",
"register_email_notice" => "*请你输入有效的Email",
"register_password_title" => "给自己设一个密码:",
"register_password_notice" => "*密码至少6位,由字母和数字组
成",
"register_re_password_title" => "确认您的密码:",
"register_nickname_title" => "给自己起一个昵称:",
"register_nickname_notice" => "中、英文均可( 4-20个字符 )。",
"register_check_code_title" => "验证码:",
"register_check_code_notice" => "(请输入右图中数字)",
"register_agree_notice" => "同意本站的<a herf='#'>服务条款</a>",
"register_button_lable" => "完 成 注 册",
"logging_page_title" => "用户登陆",
"logging_email_title" => "Email地址:",
"logging_password_title" => "登陆密码:",
"logging_savetime_title" => "保存期限:",
"register_check_code_title" => "效检码:",
"register_check_code_notice" => "请输入右图中数字",
"logging_button_lable" => "登 陆",
"logging_page_notice" => "注:登陆后,跳转到首页。"
);
$errors = array(
"register_error_email" => "此email已经被注册!",
"register_error_password" => "密码不能为空且两次密码输入必须相同,请重新输入!",
"register_error_nickname" => "此昵称已经被注册",
"register_error_check_code" => "验证码错误!",
"register_error_argee" => "您没有接受本站服务条款!",
"logging_error_password" => "密码不匹配!",
"logging_error_check_code" => "验证码错误!"
);
当需要改变语言的时候,你只需要改变下头部的require "zh-cn.php";并确保你已经定义好其他的语言包,如zh-en.php
使用常量较使用独立变量可以有更多的有意义的变量名可用
使用常量时,要注意判断常量是否已定义楼上的都是使用数组的,这已不在你的问题之列了
何况
$lang['register_page_title']
也没有
$register_page_title
REGISTER_PAGE_TITLE
来的顺眼