PHPackages                             carrooi/translator - 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. [Localization &amp; i18n](/categories/localization)
4. /
5. carrooi/translator

Abandoned → [symfony/translation](/?search=symfony%2Ftranslation)Library[Localization &amp; i18n](/categories/localization)

carrooi/translator
==================

Translator for PHP

1.7.1(10y ago)101571[1 issues](https://github.com/Carrooi/Php-Translator/issues)1MITHTMLPHP &gt;=5.3.0

Since Sep 1Pushed 10y ago2 watchersCompare

[ Source](https://github.com/Carrooi/Php-Translator)[ Packagist](https://packagist.org/packages/carrooi/translator)[ Docs](https://github.com/Carrooi/Php-Translator)[ RSS](/packages/carrooi-translator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (19)Used By (1)

[![Build Status](https://camo.githubusercontent.com/0bbc24b4ae8d8dac47f1e2641fab746e9a2ba5b91248a6834e7b1d022e17ac81/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f436172726f6f692f5068702d5472616e736c61746f722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/Carrooi/Php-Translator)

[![Donate](https://camo.githubusercontent.com/7f8b0c0980ad316210d1ec0c7d3298ace87d2f7c0eb6911977c0644951af5bd2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f6e6174652d50617950616c2d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RY2VVL44CA94Q)

translator
==========

[](#translator)

PHP translator with plural forms support.

This library is compatible with node package [translator](https://npmjs.org/package/translator).

Help
----

[](#help)

Unfortunately I don't have any more time to maintain this repository :-(

Don't you want to save me and this project by taking over it?

[![sad cat](https://raw.githubusercontent.com/sakren/sakren.github.io/master/images/sad-kitten.jpg)](https://raw.githubusercontent.com/sakren/sakren.github.io/master/images/sad-kitten.jpg)

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

[](#installation)

Preferred way is to install via [composer](http://getcomposer.org/).

```
php composer.phar require sakren/translator

```

Dictionary files
----------------

[](#dictionary-files)

This translator supposed to be translator working with key -&gt; translation principe. For easier manipulation, you can have many smaller dictionaries for smaller group of translations.

These dictionaries are json files with language code on the beginning. Below is example of few files.

```
/app/lang/homepage/en.menu.json
/app/lang/homepage/promo/en.box.json
/app/lang/en.about.json

```

There we have got three dictionaries, two for homepage and one for about page, but these names are totally up to you.

Dictionary
----------

[](#dictionary)

Here is example of /app/lang/homepage/promo/en.box.json dictionary.

```
{
	"title": "Promo box",
	"description": "some description",
	"text": "and some really long text",
	"someOtherTextToDisplay": "other boring text"
}

```

This is the most simple example of dictionary (and most stupid). Again these translation's names are up to you.

Usage
-----

[](#usage)

When you have got your dictionaries, you can setup translator and start using it.

```
$translator = new \DK\Translator\Translator('/app/lang');
$translator->setLanguage('en');

$message = $translator->translate('homepage.promo.box.text');		// output: and some really long text

```

You just have to set language, and base directory path.

Then you can begin with translating. You can see that messages to translate are paths to your dictionary files but with dots instead of slashes and without language code and .json extension.

Plural forms
------------

[](#plural-forms)

There is already registered 138 plural forms and you can find list of them on [this](http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html)site. If you will miss some language, wrote issue or register it by your own.

First you have to set plural forms rule for language which you want to use. This is the same for like plural forms for gettext.

```
$translator->addPluralForm(
	'en',				// language code
	2,					// total count of plural forms for this language
	'(n===1) ? 0 : 1'	// decision code. In "n" variable is count of items and it says that if it is 1 item, first (0) form will be used, otherwise second form
);

```

For comparing, here is example of czech plural forms.

```
$translator->addPluralForm(
	'cs',
	3,
	'(n===0) ? 2 : ((n===1) ? 0 : ((n>=2 && ntranslate('dictionary.someList');		// output: array(1st item, 2nd item, 3rd item, 4th item)

```

And you can also use it with plural forms.

Dictionary:

```
{
	"fruits": [
		[
			"1 orange",
			"%count% oranges"
		],
		[
			"1 banana",
			"%count% bananas"
		]
	]
}

```

Usage:

```
$messages = $translator->translate('dictionary.fruits', 6);		// output: array(6 oranges, 6 bananas)

```

### Accessing exact item

[](#accessing-exact-item)

```
$message = $translator->translate('dictionary.someList[0]');

```

### Shorter syntax

[](#shorter-syntax)

If your list contains just singular forms translations, you can use shorter syntax for it.

```
{
	"-- myList": [
		"first item",
		"second item",
		"third item"
	]
}

```

List of pairs
-------------

[](#list-of-pairs)

If you have got one list of for example titles or headlines and other list with texts for these titles, you can let this translator to automatically combine these two lists together into associative array.

Dictionary:

```
{
	"titles": [
		["first"],
		["second"]
	],
	"texts": [
		["text for first title"]
		["text for second title"]
	]
}

```

Usage:

```
$translator->translatePairs('dictionary', 'titles', 'texts');

```

Output:

```
[
	'first' => 'text for first title',
	'second' => 'text for second title'
]

```

Translate whole array
---------------------

[](#translate-whole-array)

When you have got some array, which you need to translate, you don't have to iterate through it yourself.

```
$messages = array(
    'homepage.promo.box.title',
    'homepage.promo.box.description',
    'homepage.promo.box.text'
);

$result = $translator->translateMap($messages);

```

If your array to translate contains translations just from one dictionary, you can set some kind of base path.

```
$messages = array(
    'title',
    'description',
    'text'
);

$result = $translator->translateMap($messages, null, null, 'homepage.promo.box.title');

```

Or use count for translations with plural forms.

```
$result = $translator->translateMap($messages, 6);

```

Or with some replacements.

```
$result = $translator->translateMap($messages, null, array('type' => 'book'));

```

Temporary override language
---------------------------

[](#temporary-override-language)

There may be some cases when you need to get translations for different language.

```
$translator = new \DK\Translator\Translator('/app/lang');
$translator->setLanguage('en');

$message = $translator->translate('cs|homepage.title');

```

Now in `message` variable will be translation of `homepage.title` in czech language.

Comments in dictionaries
------------------------

[](#comments-in-dictionaries)

You can write some comments into your dictionaries. These comments has to be enclosed into `#`.

```
{
	"message": [
		"# this message will be displayed in home page #",
		 "translation of message"
	]
}

```

Or with lists:

```
{
	"list": [
		"# this is list of some items #",
		[
			"# first item in list #",
			"first"
		],
		[
			"# second item in list #",
			"second"
		]
	]
}

```

With Nette framework
--------------------

[](#with-nette-framework)

If you want to use this translator with nette, please use [sakren/nette-translator](https://github.com/sakren/nette-translator)library.

Changelog
---------

[](#changelog)

- 1.7.1

    - Move under Carrooi organization
    - Abandon package
- 1.7.0

    - Optimized messages parsing
    - Added translation helpers
    - Added method `getLastTranslated()`
- 1.6.4

    - Method `getMessageInfo()` is now public
- 1.6.3

    - Method `findTranslation()` is now public
- 1.6.2

    - Added methods `getTranslated()` and `getUntranslated()`
    - Added method `save()` into loaders (prepared for future improvements)
- 1.6.1

    - Some internal changes
- 1.6.0

    - Optimizations
    - Added badges and travis
    - Failing tests after clean installation
    - Creating translator from config file (for API)
    - Added method `hasTranslation`
    - Option for temporary overriding language
    - Added filters
- 1.5.0

    - Added translateMap method
- 1.4.0

    - Accessing items from lists in translate method
- 1.3.2

    - Translate method: can pass args as second argument
- 1.3.1

    - Added some information
- 1.3.0

    - Added tests
    - Added shorter syntax for writing lists
    - Added support for comments
    - Removed Nette support (will be in another package)
- 1.2.2

    - Optimized plural forms
    - Replacements were not applied to messages (huge bug, sorry)
- 1.2.1

    - Replacements in messages
- 1.2.0

    - Added translatePairs method

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity67

Established project with proven stability

 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 ~40 days

Recently: every ~114 days

Total

18

Last Release

3957d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/838c6933d498fdb2a31f251ed45006a6ef97935ea2a27f38dab7738038939fc9?d=identicon)[david\_kudera](/maintainers/david_kudera)

---

Top Contributors

[![davidkudera](https://avatars.githubusercontent.com/u/1174072?v=4)](https://github.com/davidkudera "davidkudera (2 commits)")

---

Tags

localizationi18ntranslator

### Embed Badge

![Health badge](/badges/carrooi-translator/health.svg)

```
[![Health](https://phpackages.com/badges/carrooi-translator/health.svg)](https://phpackages.com/packages/carrooi-translator)
```

###  Alternatives

[symfony/intl

Provides access to the localization data of the ICU library

2.6k199.8M1.1k](/packages/symfony-intl)[mariuzzo/laravel-js-localization

Laravel Localization in JavaScript

6073.9M3](/packages/mariuzzo-laravel-js-localization)[gettext/languages

gettext languages with plural rules

7530.3M11](/packages/gettext-languages)[aplus/language

Aplus Framework Language Library

2351.7M15](/packages/aplus-language)[punic/punic

PHP-Unicode CLDR

1542.9M29](/packages/punic-punic)[aura/intl

The Aura Intl package provides internationalization tools, specifically message translation.

898.3M4](/packages/aura-intl)

PHPackages © 2026

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