search

php国际化

Jun 23, 2016 pm 02:34 PM
php internationalization

I18N internationalization(国际化)简写为I18N,中间的数字为省略的字母个数。 在 Linux 系统中,通过定义一组环境变量来设置程序的语言环境,以实现对I18N的支持,这种机制称为:locale(本地化) LANG 低优先级全局 locale 变量 如果下面的变量未赋值,默认使用此变量的值 LC_COLLATE 比较和排序习惯 会影响目录列表的分类显示等 LC_CTYPE 定义系统的字符处理特性 哪些字符能被视为字母、数字,等等;与中文输入关系密切 LC_MESSAGES 提示信息,错误信息, 状态信息, 标题, 标签, 按钮和菜单等 LC_MONETARY 定义货币单位和货币型数值的格式 LC_NUMERIC 定义非货币型数值的格式 影响到千位分隔符和小数分隔符等 LC_TIME 定义日期和时间的格式 LC_NAME 姓名书写方式 LC_ADDRESS 地址书写方式 LC_TELEPHONE 电话号码书写方式 LC_MEASUREMENT 度量衡表达方式 LC_PAPER 定义默认的纸张尺寸 LC_IDENTIFICATION 对 locale 自身包含信息的概述 LC_ALL 高优先级全局 locale 变量 为此变量赋值会强行覆盖上面变量的值,不推荐 步骤一:搭建环境 1,首先查看你的php扩展目录下是否有php_gettext.dll这个文件,如果没有,这就需要你 下载一个或是从其他地方拷贝一个,然后放到php扩展目录。 2,打开php.ini,查找”;extension=php_gettext.dll“ ,然后去除注释,重启apache。 步骤二:原理讲解 假如你的没有国际化的程序里有这样的代码,echo "你好";,而国际化的程序你要写成 echo gettext("你好");,然后再在配置文件里添加“你好”相对应的英文“Hi”。 这时,中国地区浏览都会在屏幕上输出“你好”,而美国地区浏览都会在屏幕上输出 “Hi”。也就是说,最终显示什么是根据你的配置文件而定的,如果找不到配置文件, 才会输出程序里面的内容。 步骤三:编码测试 1,我们在d:\www下面新建文件hi.php,详细代码如下 1 这时你运行改程序,只会输出“Hi”。但我们是中国人,我们不认识“Hi”, 我们只认识“你好”,这时就要配置文件出马。配置文件的生成一般借助一款工具。 下载地址:http://nchc.dl.sourceforge.net/sourceforge/gnuwin32/gettext-0.14.4.exe 安装好以后,为了在任意目录里使用,需要把“安装路径/bin”添加到系统环境变量里。 步骤四:配置文件的生成 1,我们假设你的工具已经安装好,并且可以在任意目录使用。现在就要运行cmd,并把 路径切换到d:\www下面,也就是hi.php所在目录。 键入xgettext -d hi hi.php --from-code=gb2312,然后执行,这时你可以看到新生成一个 hi.po文件,注意:--from-code=gb2312,其中gb2312还可以是utf-8。 2,打开hi.po文件,显示如下: # SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR , YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2009-01-19 17:47+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" #: hi.php:6 msgid "Hi!" msgstr "" 现在有两个地方需要修改, 1:"Content-Type: text/plain; charset=CHARSET\n" 2:msgstr "" 把1中的CHARSET修改成gb2312,然后把2修改成msgstr "你好"。 3,键入msgfmt -o hi.mo hi.po,执行,这时生成hi.mo文件。 然后在d:\www下新建locale\zh_Cn\LC_MESSAGES目录,把hi.mo拷到这里就行了。 4,现在重启apache,再次运行,屏幕上可以输出“你好”。 其他: 如果使用utft-8编码的话,需要使用 bind_textdomain_codeset($domain,'UTF-8'); 相应的要把hi.po里的CHARSET改成utf-8,还需要把hi.po保存成utf-8格式, 再次生成hi.mo就行了。 总结: 我们都希望我们写的程序可以被大众甚至国际普遍使用,像大名鼎鼎的wordpress的 国际化使用的也是他。gettext还是非常不错的,简单易用, http://www.cnblogs.com/suimengsiqu/archive/2009/01/19/1378466.html

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool