PHPackages                             sroehrl/php-i18n-translate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. sroehrl/php-i18n-translate

ActiveLibrary[Templating &amp; Views](/categories/templating)

sroehrl/php-i18n-translate
==========================

Simple yet powerful i18n support for PHP projects

v2.0.2(3y ago)037MITPHP

Since Aug 19Pushed 3y ago1 watchersCompare

[ Source](https://github.com/sroehrl/php-i18n-translate)[ Packagist](https://packagist.org/packages/sroehrl/php-i18n-translate)[ RSS](/packages/sroehrl-php-i18n-translate/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (11)Used By (0)

PHP i18n translate
==================

[](#php-i18n-translate)

> Straight forward. Convenient. Fast.

[![Build](https://github.com/sroehrl/php-i18n-translate/actions/workflows/php.yml/badge.svg)](https://github.com/sroehrl/php-i18n-translate/actions/workflows/php.yml)[![Coverage](https://raw.githubusercontent.com/sroehrl/php-i18n-translate/badges/.github/badges/test-badge.svg)](https://github.com/sroehrl/php-i18n-translate/actions/workflows/php.yml)[![php](https://camo.githubusercontent.com/517137a4e47de40310c6f0793027823ef33510d454a9ad226e10d1f4e3943a5a/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d504850266d6573736167653d576974682532304c6f766526636f6c6f723d373737424234266c6f676f3d706870)](https://php.net)[![vegan](https://camo.githubusercontent.com/02de5d126e82172bd802704061a4626dc8dbdc7284bbe86c01c4df028874a590/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d31303025266d6573736167653d766567616e26636f6c6f723d343761323434266c6f676f3d6d6f6e676f6462)](https://www.whyveganism.com/)[![Maintainability](https://camo.githubusercontent.com/d6f4106ae2a43614af753e9989ab548c59dcb85354054e4da3eacf566a391eaa/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35623635306465373230656238303261383461392f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/sroehrl/php-i18n-translate/maintainability)

Installation
------------

[](#installation)

`composer require sroehrl/php-i18n-translate`

```
require_once 'vendor/autoload.php';

$i18n = new I18nTranslate\Translate();

$i18n->setTranslations('de', [
    'hello' => 'hallo',
    'goose' => ['Gans', 'Gänse']
]);
```

Quick start:
------------

[](#quick-start)

### 1. In Code

[](#1-in-code)

```
echo "a: " . $i18n->t('hello') . "";
echo "b: " . $i18n->t('goose') . "";
echo "c: " . $i18n->t('goose.plural') . "";

// detect plural by numeric value
foreach([0,1,2] as $number){
    echo $number . " " . $i18n->t('goose', $number) . ", ";
}
```

Outputs:

```
a: hallo
b: Gans
c: Gänse
0 Gänse, 1 Gans, 2 Gänse
```

### 2. In HTML

[](#2-in-html)

main.html

```

03/06/2009

next monday

    Event starts @ 12:30 (That's 12:30 your time)

goose
goose.plural
```

```
...

echo $i18n->translate(file_get_contents('main.html'));
```

Outputs:

```

06.03.2009

`22 13:14

22.06

    Event starts @ 12:30 EDT (That's 9:30 am your time)

Gans
Gänse
```

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Quick Start](#quick-start)
- Table of Contents
- [Initialization](#installation)
- [Setting up translations](#setting-up-translations)
- [Time, Date, Currencies &amp; Numbers](#time-date-currencies--numbers)
- [Time &amp; Date Formats](#time--date-formats)
- [Usage with "Template" ***recommended***](#using-i18ntranslate-with-version-2-of-the-neoan3-appstemplate-engine)
- [Placeholders &amp; Dynamic values](#placeholders--dynamic-values)

Initialization
--------------

[](#initialization)

`$t = new I18nTranslate(string $locale = null, string $clientTimezone = null)`

You can initialize either with or without a ISO-locale (e.g. 'en-US'). If no value is provided, the class first tries to set the locale by the ACCEPT\_LANGUAGE header and if that fails defaults to "en-US". If you don't pass a $clientTimeZone (e.g. 'Europe/Paris'), then a guess is made based on the locale. This can potentially lead to time offsets in countries with multiple timezones.

> TIPS:
>
> Timezones: There are several "timezone-guessing" mechanisms around. Using JavaScript is usually the most reliable way.
>
> Settings: When dealing with internationalization, setting your server &amp; database to UTC is a battle-tested approach.

Setting up translations
-----------------------

[](#setting-up-translations)

Whether you read your translations from a database or a file: gettext is not required, and you are expected to run the method `setTranslations` for every language you support.

```
$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-&gt;setDebug(true)
>
> Will output a missing key message when a translation isn't set, rather than the following default behavior. This can be useful while developing/translating.

- If the language is not found, the class will default to the first defined language
- If a key is not found, the class will return the key
- If the key has a suffix (.plural), it will be removed

**About locale-translations:** The decision to ignore the country-specification on the locale on translations is intended. While formatting reacts to the differences of country localisation, translations do not. Example en-US vs. en-EN: the date formatting will react to these differences, but translations like 'color' &lt;=&gt; 'colour' are not supported.

Time, Date, Currencies &amp; Numbers
------------------------------------

[](#time-date-currencies--numbers)

This package includes a formatter for currencies, numbers and dates. If you want to use its functionality outside of an HTML template, you can initialize it yourself.

The following converters are at your disposal:

- date (accepts optional [format](#time--date-formats))
- date-local (accepts optional [format](#time--date-formats))
- time (accepts optional [format](#time--date-formats))
- time-local (accepts optional [format](#time--date-formats))
- currency (accepts optional, but recommended, ISO currency code like "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
```

In most scenarios the templating attributes will be sufficient to handle your needs:

```
12.34

12.34

tomorrow
tomorrow
9:30
9:30
```

For a better understanding of how to pass values to your HTML, read [here](#using-i18ntranslate-with-version-2-of-the-neoan3-appstemplate-engine)

Time &amp; Date formats
-----------------------

[](#time--date-formats)

This package uses the Intl-extension for PHP but has a fallback mechanisms. If you do not have Intl installed, localized transformation does not work.

Date &amp; Time inputs are interpreted either as UNIX timestamps or strings supported by PHP's [strtotime](https://www.php.net/manual/en/function.strtotime) function.

Date &amp; Time formats accepts strings in the format of ISO8601 date format **So not PHP's date notation**

I kindly ask contributors to find an appropriate list. Until then, this dated Zend list is the best I could find: [formats](https://framework.zend.com/manual/1.12/en/zend.date.constants.html#zend.date.constants.selfdefinedformats)

*Default fallback formats:*

MetricImperialdatedd.MM.YMM/dd/YtimeHH:mmhh:mm A zNumbers and currencies work with or without the Intl-extension, but might not conform to the ISO 8601 standard without the Intl-extension.

The examples at [Quick Start](#quick-start) should help.

Using i18nTranslate with version 2+ of the neoan3-apps/template engine.
-----------------------------------------------------------------------

[](#using-i18ntranslate-with-version-2-of-the-neoan3-appstemplate-engine)

Under the hood this package interprets html-files with the help of [neoan3-apps/template](https://packagist.org/packages/neoan3-apps/template). It is therefore already available to you once you installed this package. The following setup allows the template engine to run **PRIOR** to translations, making dynamic formats and values possible:

```
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

```
{{fromCode}}
Hackathon @ {{tomorrow}}
next monday

    {{number}} {{t(woman, {{number}})}}

```

Example-output:

```
Mann
Hackathon @ 20.08.2022
22-08-22

    0 Frauen
    1 Frau
    2 Frauen

```

Placeholders &amp; Dynamic values
---------------------------------

[](#placeholders--dynamic-values)

Placeholders are written embraced by `[%` ans `%]` (e.g. `[% my-var %]`). They enable dynamic values within a translation.

```
...
$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));
```

```
Today I want to talk about [%subject%](% Tom Brady %)
```

... or when using template values:

```
Today I want to talk about [%subject%](% {{personOfInterest}} %)
```

### Attributes as functions

[](#attributes-as-functions)

When using the attributes within t-tags as functions, they can be referenced as follows:

- i18n-currency -&gt; \[%currency-value%\]
- i18n-time, i18n-time-local -&gt; \[%time-value%\]
- i18n-date, i18n-date-local -&gt; \[%date-value%\]
- i18n-number -&gt; \[%number-value%\]

CONTRIBUTION
------------

[](#contribution)

[rules](/contributing.md)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~38 days

Total

5

Last Release

1211d ago

Major Versions

v1.0.1 → v2.0.02022-08-19

### Community

Maintainers

![](https://www.gravatar.com/avatar/92d2361b646651e3452a62d07274076346c4096480098a6c43d6c27ee28d460d?d=identicon)[neoan](/maintainers/neoan)

---

Top Contributors

[![sroehrl](https://avatars.githubusercontent.com/u/28542911?v=4)](https://github.com/sroehrl "sroehrl (40 commits)")

---

Tags

i18nphpphp8template-enginetranslation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sroehrl-php-i18n-translate/health.svg)

```
[![Health](https://phpackages.com/badges/sroehrl-php-i18n-translate/health.svg)](https://phpackages.com/packages/sroehrl-php-i18n-translate)
```

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
