sroehrl / php-i18n-translate
PHP项目简单而强大的i18n支持
Requires
- ext-dom: *
- ext-mbstring: *
- neoan3-apps/template: ^2.0
Requires (Dev)
- phpunit/phpunit: 9.5.21
This package is auto-updated.
Last update: 2024-09-17 05:00:42 UTC
README
简单。方便。快速。
安装
composer require sroehrl/php-i18n-translate
require_once 'vendor/autoload.php'; $i18n = new I18nTranslate\Translate(); $i18n->setTranslations('de', [ 'hello' => 'hallo', 'goose' => ['Gans', 'Gänse'] ]);
快速开始
1. 在代码中
echo "a: " . $i18n->t('hello') . "<br>"; echo "b: " . $i18n->t('goose') . "<br>"; echo "c: " . $i18n->t('goose.plural') . "<br>"; // detect plural by numeric value foreach([0,1,2] as $number){ echo $number . " " . $i18n->t('goose', $number) . ", "; }
输出
a: hallo <br> b: Gans <br> c: Gänse <br> 0 Gänse, 1 Gans, 2 Gänse
2. 在HTML中
main.html
<!-- We haven't provided any locale, so the ACCEPTED LANGUAGE header is used --> <p i18n-date>03/06/2009</p> <!-- If no value is present, the current timestamp is used --> <p i18n-date="\`y H:i"></p> <!-- If it's not a timestamp, things become smart --> <p i18n-date="d.m">next monday</p> <div> <p>Event starts @ <strong i18n-time>12:30</strong> (That's <span i18n-time-local="hh:mm a">12:30</span> your time)</p> </div> <!-- let's translate again, using the t-tag --> <p><t>goose</t></p> <p><t>goose.plural</t></p>
... echo $i18n->translate(file_get_contents('main.html'));
输出
<!-- We haven't provided any locale, so the ACCEPTED LANGUAGE header is used --> <p>06.03.2009</p> <!-- In no value is present, the current timestamp is used --> <p>`22 13:14</p> <!-- If it's not a timestamp, things become smart --> <p>22.06</p> <div> <p>Event starts @ <strong>12:30 EDT</strong> (That's <span>9:30 am</span> your time)</p> </div> <!-- let's translate again, using the t-tag --> <p><t>Gans</t></p> <p><t>Gänse</t></p>
目录
初始化
$t = new I18nTranslate(string $locale = null, string $clientTimezone = null)
你可以使用或不用ISO-locale(例如‘en-US’)初始化。如果没有提供值,类将首先尝试通过ACCEPT_LANGUAGE头设置地区,如果失败,则默认为“en-US”。如果你不传递$ clientTimeZone(例如‘Europe/Paris’),则基于地区进行猜测。这可能导致多时区国家的时差。
提示
时区:周围有几个“时区猜测”机制。通常使用JavaScript是最可靠的方式。
设置:在处理国际化时,将服务器和数据库设置为UTC是一种经过考验的方法。
设置翻译
无论你从数据库还是文件中读取翻译:gettext不是必需的,并且你预计将为每个你支持的语言运行setTranslations
方法。
$t = new I18nTranslate(); $all = [ ['eo' => ['blue' => 'blua',...]], ['jp' => ['blue' => '青い',...]], // BTW: make sure to consider encoding ['de' => ['blue' => 'blau',...]], ]; foreach($all as $lang => $translations){ $t->setTranslations($lang, $translations); }
$t->setDebug(true)
当未设置翻译时,将输出缺失键消息,而不是以下默认行为。这在开发/翻译时可能很有用。
- 如果找不到语言,类将默认为第一个定义的语言
- 如果找不到键,类将返回键
- 如果键有后缀(.plural),则将其删除
关于地区翻译:忽略地区在地区上的决定是有意的。虽然格式化会根据国家本地化做出反应,但翻译不会。例如en-US与en-EN:日期格式将对此类差异做出反应,但像‘color’ <=> ‘colour’这样的翻译不受支持。
时间、日期、货币和数字
此软件包包括货币、数字和日期的格式化程序。如果你想在HTML模板之外使用其功能,你可以自行初始化它。
以下转换器可供您使用
- date(接受可选的格式)
- date-local(接受可选的格式)
- time(接受可选的格式)
- time-local(接受可选的格式)
- currency(接受可选,但推荐,ISO货币代码如“USD”)
- number
$locale = 'en-US'; $clientTimeZone = 'America/New_York'; // or null to let the class make an educated guess $formatter = new I18nTranslate\Formatter(string $locale, $clientTimeZone); $convertToClientTime = $formatter->format('time-local'); $serverTime = time(); $clientTime = $convertToClientTime($serverTime); // e.g. 09:30 AM EDT $clientTime = $convertToClientTime($serverTime, 'h:mm'); // e.g. 9:30
在大多数场景中,模板属性足以满足您的需求
<section i18n-number>12.34</section> <!-- with currencies we recommend setting a currency as it otherwise defaults to the user's locale... --> <section i18n-currency="CAD">12.34</section> <!-- ... But the following attributes accept formatting, but don't need it --> <section i18n-date="EEEE">tomorrow</section> <section i18n-date-local>tomorrow</section> <section i18n-time>9:30</section> <section i18n-time-local>9:30</section>
要了解如何将值传递到HTML中,请阅读此处
时间与日期格式
此软件包使用PHP的Intl扩展,但具有后备机制。如果您没有安装Intl,则本地化转换将不起作用。
日期和时间输入被解释为UNIX时间戳或PHP的strtotime函数支持的字符串。
日期和时间格式接受ISO8601日期格式的字符串因此不是PHP的日期表示法
我恳请贡献者找到合适的列表。在此之前,这是我所能找到的最适合的过时的Zend列表:[格式]
默认回退格式
数字和货币在或没有Intl扩展的情况下都能工作,但如果没有Intl扩展,可能不符合ISO 8601标准。
快速入门中的示例应有所帮助。
使用版本2+的neoan3-apps/template模板引擎的i18nTranslate。
在内部,此包使用neoan3-apps/template帮助解释html文件。因此,一旦您安装了此包,它就可供您使用。以下设置允许模板引擎在翻译之前运行,从而使得动态格式和值成为可能
use I18nTranslate\Translate; use Neoan3\Apps\Template\Constants; use Neoan3\Apps\Template\Template; ... // your template path Constants::setPath(__DIR__ . '/view'); // initialize i18n $t = new Translate('de-DE); $t->setTranslations('de', [ 'woman' => ['Frau', 'Frauen'], 'man' => ['Mann', 'Männer'] ]); $regularRenderData = [ 'tomorrow' => time() + 60*60*24, 'format' => 'dd-MM-Y', 'iterateMe' => [0,1,2], 'fromCode' => $t->('man') ]; echo $t->translate(Template::embraceFromFile('/test.html', $regularRenderData));
test.html
<h2>{{fromCode}}</h2> <p>Hackathon @ <span i18n-date>{{tomorrow}}</span></p> <p i18n-date="{{format}}">next monday</p> <div> <p n-for="iterateMe as number">{{number}} {{t(woman, {{number}})}}</p> </div>
示例输出
<h2>Mann</h2> <p>Hackathon @ <span>20.08.2022</span></p> <p>22-08-22</p> <div> <p>0 Frauen</p> <p>1 Frau</p> <p>2 Frauen</p> </div>
占位符和动态值
占位符由[%
和%]
括起来(例如[% my-var %]
)。它们允许在翻译中启用动态值。
... $t->setTranslations('de',[ 'Today I want to talk about [%subject%]' => 'Heute möchte ich über [%subject%] sprechen' ]) $context = [ 'personOfInterest' => 'Mr. T' ] echo $t->translate(Template::embraceFromFile('/belowHtml.html', $context));
<t>Today I want to talk about [%subject%](% Tom Brady %)</t>
...或者在使用模板值时
<t>Today I want to talk about [%subject%](% {{personOfInterest}} %)</t>
作为函数的属性
当将属性作为函数在t标签中使用时,可以按照以下方式引用
- i18n-currency -> [%currency-value%]
- i18n-time, i18n-time-local -> [%time-value%]
- i18n-date, i18n-date-local -> [%date-value%]
- i18n-number -> [%number-value%]