search
Homephp教程php手册已解决:Ubuntu php gettext 无效

使用gettext做多文字国际化,按PHP官方文档,在CentOS上OK,在Ubuntu上不行,调试了快1天,试了网上提供的无数方法,终于有一个OK了 看到此文的同学别再被坑了 感谢:http://stackoverflow.com/questions/18551553/php-gettext-translate-in-wrong-language-

使用gettext做多文字国际化,按PHP官方文档,在CentOS上OK,在Ubuntu上不行,调试了快1天,试了网上提供的无数方法,终于有一个OK了……

看到此文的同学别再被坑了……

 

感谢:http://stackoverflow.com/questions/18551553/php-gettext-translate-in-wrong-language-when-running-from-shell

BS官网文档:http://www.php.net/manual/en/function.gettext.php

 

代码:

<span>php
</span><span>$lang</span> = 'en_US'; <span>//</span><span>linux不支持zh-Hans,所以gettext、php都不支持。使用locale -a可以看到os支持的语言文字国家地区码。</span>
<span>putenv</span>('LANGUAGE=' . <span>$lang</span>); <span>//</span><span>ubuntu 需要,centos不需要此行</span>
<span>setlocale</span>(LC_ALL, <span>$lang</span> . '.utf8'); <span>//</span><span>ubuntu 需要.utf8,centos可有可无</span>
<span>$domain</span> = 'strings'<span>;
bindtextdomain(</span><span>$domain</span>, <span>dirname</span>(<span>__FILE__</span>) . '/locale'<span>);
bind_textdomain_codeset(</span><span>$domain</span> , 'UTF-8'<span>);
textdomain(</span><span>$domain</span><span>);

</span><span>echo</span> _('asdf'<span>);
</span><span>echo</span> "\n"<span>;
</span><span>exit</span><span>;
</span>?>

 

目录为en_US,如下:

|     |---
|     |---<span>en_US
</span>|     |    |---<span>LC_MESSAGES
</span>|     |    |          |---strings.mo

 

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools