概述 所谓 国际化 [通常写为I18N],是指对一个只为某个地区设计的程序进行重构,使其能够在更多的地区使用的过程。而所谓的 本地化 [通常写为L10N],则是指在一个面向 国际化 的程序中增加对一个新地区应用支持的过程。 作为开发本地应用的开发者来说,这一
概述
所谓国际化[通常写为I18N],是指对一个只为某个地区设计的程序进行重构,使其能够在更多的地区使用的过程。而所谓的本地化[通常写为L10N],则是指在一个面向国际化的程序中增加对一个新地区应用支持的过程。
作为开发本地应用的开发者来说,这一章节的内容似乎离我们很远,其实不然。就说wordpress,我们很多都是用的国外主机,这时,主机的时区设置和使用者是不一致的,最可能碰到的问题是当你在发博文时,显示的时间是服务器设置的时间而非你本地的时间。这时就需要本地化设置了,通过设置时区来调整时间的显示。
设置时区并没那么容易
现在大多数系统都有地区系统,虽然地区系统有助于解决许多本地化的问题,但由于标准化程度不够,所以使用不便之处经常让人沮丧。不仅不同的系统支持的地区会有所不同,甚至对于相同地区都会有不同的名称。
指定地区
在PHP中可以通过setlocale函数来设定地区信息,该函数只在运行期间有效(即不会影响到服务器上其他WEB程序)
//Linux下的时区设置 setlocale(LC_ALL,'es_MX'); setlocale(LC_ALL,'de_AT'); //Windows下的时区设置 setlocale(LC_ALL,'Spanish_Mexico'); setlocale(LC_ALL,'German_Austria');
本地化文本消息
很多程序为了实现多语言,将所有提示和需要显示文本信息保存到一个lang数组中,然后通过本地设置来调用不同地区的lang语言来实现多语言版本。像下面这样:
$messages = array ('en_US' => array( 'My favorite foods are' => 'My favorite foods are', 'french fries' => 'french fries', 'candy' => 'candy', 'potato chips' => 'potato chips', 'eggplant' => 'eggplant' ), 'en_UK' => array( 'My favorite foods are' => 'My favourite foods are', 'french fries' => 'chips', 'candy' => 'sweets', 'potato chips' => 'crisps', 'eggplant' => 'aubergine' ) ); function msg($s) { global $LANG, $messages; if (isset($messages[$LANG][$s])) { return $messages[$LANG][$s]; } else { error_log("l10n error: LANG: $lang, message: '$s'"); } } $LANG = 'en_UK'; print msg('My favorite foods are').":\n"; print msg('french fries')."\n"; print msg('potato chips')."\n"; print msg('candy')."\n";
本地化时间
这部分内容很多程序的时间格式显示都是在模板中进行设定的,书中介绍的是strftime来格式化时间
本地化货币值
通过money_format()实现的,意义不大,因为这个不涉及到货币的兑换汇率。实际开发中可能需要通过重写__tostring()方法实现。
通过localeconv()可以返回与货币相关的信息
本地化图片和文件
将含有文字的图片或不同语言版本的文件按语言/时区命名的文件夹来区分,例如
images/$Lang/logo.jpg
设置字符编码
通过header可以设施输出数据的编码
header('Content-Type: text/html;charset=utf-8');

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment