php简单国际化

本文介绍了如何使用PHP实现网页的简单国际化,通过加载不同的语言文件(如lang-us.php)来切换页面显示的语言,以实现英文版(lang-us.php)和其他语言版本的切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

index.html 显示页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>Document</title>
	<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
	<script>
		$(function() {
			$('#ch').click(function(){
				$.ajax({
					url: 'index.php',
					type: 'post',
					datatype: 'json',
					data: 'lang=ch',
					success:function(msg) {
						$('p').text(msg);
					}
				})
			})
			$('#us').click(function(){
				$.ajax({
					url: 'index.php',
					type: 'post',
					datatype: 'json',
					data: 'lang=us',
					success:function(msg) {
						$('p').text(msg);
					}
				})

			})

		})
	</script>
</head>
<body>
	<p>小明</p>
	<input type="button" value="中文" id="ch"/>
	<input type="button" value="English" id="us"/>
</body>
</html>

index.php 处理页面

<?php
!isset($_POST['lang']) ? $rs = null : $rs = htmlspecialchars($_POST['lang']);

switch ($rs) {
	case 'ch':
		$a = 'ch';
		break;
	default:
		$a = 'en';
		break;
}
require "lang-{$a}.php";
echo $name;

语言包

英文lang-us.php

<?php
/* 
* @Author: xinghuang@hengtiansoft.com
* @Date:   2014-05-23 11:13:22
* @Last Modified by:   xinghuang@hengtiansoft.com
* @Last Modified time: 2014-05-23 11:32:33
*/
$name='xiaoming';
中文lang-ch.php

<?php
/* 
* @Author: xinghuang@hengtiansoft.com
* @Date:   2014-05-23 11:14:16
* @Last Modified by:   xinghuang@hengtiansoft.com
* @Last Modified time: 2014-05-23 11:32:29
*/
$name='小明';


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值