PHPackages                             diversen/simple-php-translation - 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. diversen/simple-php-translation

ActiveLibrary[Localization &amp; i18n](/categories/localization)

diversen/simple-php-translation
===============================

simple solution for doing translation, extracting translations, and doing auto translation through google

v3.1.0(2y ago)227.8k↓50%101MITPHPPHP &gt;=5.3.0CI failing

Since Aug 8Pushed 2y ago1 watchersCompare

[ Source](https://github.com/diversen/simple-php-translation)[ Packagist](https://packagist.org/packages/diversen/simple-php-translation)[ Docs](https://github.com/diversen/simple-php-translation)[ RSS](/packages/diversen-simple-php-translation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (29)Used By (1)

Brief Overview
==============

[](#brief-overview)

The `simple-php-translation` is a simple solution for adding translations to your PHP apps.

Install:

```
composer require diversen/simple-php-translation

```

If you want to use google auto translate, require the following:

```
composer require google/cloud-translate

```

Translations are placed in files named:

```
lang/en/language.php
lang/da/language.php

```

E.g. inside your app `test_app`

```
test_app/lang/en/language.php

```

The `language.php` file could consist of this:

```
$LANG = array ();
$LANG['Welcome to my blog'] = 'Welcome to my blog';
```

A Danish translation could be found in:

```
test_app/lang/da/language.php

```

And this `language.php` could consist of:

```
    $LANG = array ();
    $LANG['Welcome to my blog'] = 'Velkommen til min blog';
```

Load language
=============

[](#load-language)

```
use Diversen\Lang;

$l = new Lang();

// Most often all translations are placed in a single folder
$l->setSingleDir("test_app");

// But you can also set dirs, and look for language files inside multiple language dirs:
// $l->setDirsInsideDir("modules/");

// load language. E.g. danish ('da')
// $l->loadLanguage('da');

// Or english
// e.g. test_app/lang/da/language.php
$l->loadLanguage('en');

// now all language files are loaded, and we can translate
```

Translate
=========

[](#translate)

```
// simple

use Diversen\Lang;

echo Lang::translate('Here is a text');

// with substitution and a span to indicate that a part of a string should not be translated

echo Lang::translate('User with ID {ID} has been locked!', array ('ID' => $id))
```

Extract strings
===============

[](#extract-strings)

This will extract all `Lang::translate` calls, and add new values to translation files.

```
use Diversen\Translate\Extractor;

// same pattern as above for extraction

$e = new Extractor();
$e->defaultLanguage ='en'; // which language will we extract to

// Most often you will just use a single dir. Like this
$e->setSingleDir("test_app");

// Set multiple dirs, like this:
// This will create translation folders in e.g. modules/blog, modules/account
// $e->setDirsInsideDir('modules/');

$e->updateLang();
```

> The `$e->updateLang()` call only add new strings found in the source, and remove strings that are removed from the source. It also knows if you have changed the value of a translation key, then it will leave the value as it is. It only updates the translation files, when a new key value is found.

Auto translate using google translate API
=========================================

[](#auto-translate-using-google-translate-api)

You will need to setup google cloud translation:

Then require the composer package `google/cloud-translate`

```
composer require google/cloud-translate

```

Remember to export the GOOGLE\_APPLICATION\_CREDENTIALS or you will not be able to run the script.

```
// same pattern as above for google auto translation.
use Diversen\Translate\GoogleTranslate;

include_once "vendor/autoload.php";

// Google translator needs this. Substitue with path to your own .json file
putenv("GOOGLE_APPLICATION_CREDENTIALS=config-locale/pebble-2c949028ebcc.json");

$t = new GoogleTranslate();
$t->target = 'da'; // danish
$t->source = 'en';

$t->setSingleDir("app");

// Or set multiple dirs like this:
// This will create translation folders in e.g. modules/blog, modules/account
// $e->setDirsInsideDir('modules/');

$t->updateLang();
```

Demo
====

[](#demo)

Inside the [test\_app/](test_app/) directory, there is a small php app consisting of one PHP file: [test\_app/index.php](test_app/index.php). There is also included javascript in this file. The javascript `Lang.translate` method will also be extracted when using the `Extractor` class.

The Javascript file [test\_app/js/lang.js](test_app/js/lang.js) will do the translation.

Start the app:

```
php -S localhost:8000 -t test_app

```

Visit

You can also test the danish translation at:

I there is no translations then any string will get the "NT: " (Not Translated) prefix.

You can test this by removing the `en` language file directory:

```
rm -rf test_app/lang/en

```

Extract the english translation `en` using the script [test/extract.php](test/extract.php).

```
php test/extract.php

```

Reload the browser.

Now all translation are loaded from a file and the prefix 'NT: ' is removed.

There is also a small script for translating into danish (`da`). [test/google\_translate.php](test/google_translate.php)

In order to use this script, you will need to setup a `google service account`. Then change this part of the script:

```
putenv("GOOGLE_APPLICATION_CREDENTIALS=google_json/pebble-2c949028ebcc.json");

```

To:

```
putenv("GOOGLE_APPLICATION_CREDENTIALS=path/to/service-account-your-key.json");

```

Now you can run:

```
php test/google_translate.php

```

The translation will look like this: [test\_app/lang/da/language.php](test_app/lang/da/language.php)

Finally there is [test/to\_js.php](test/to_js.php) script which translates into js module exports, which then can be loaded and translated by the EMS module [test\_app/js/lang.js](test_app/js/lang.js)

License
=======

[](#license)

MIT © [Dennis Iversen](https://github.com/diversen)

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 97.4% 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 ~111 days

Recently: every ~121 days

Total

28

Last Release

940d ago

Major Versions

1.0.26 → v2.0.02019-11-10

v2.0.2 → v3.0.02022-04-13

### Community

Maintainers

![](https://www.gravatar.com/avatar/23ed91d104f16d10414811ecdafbd5c80a660e658d80ccaadf84f512cfb50629?d=identicon)[diversen](/maintainers/diversen)

---

Top Contributors

[![diversen](https://avatars.githubusercontent.com/u/142675?v=4)](https://github.com/diversen "diversen (74 commits)")[![drsdre](https://avatars.githubusercontent.com/u/809827?v=4)](https://github.com/drsdre "drsdre (1 commits)")[![waibcam](https://avatars.githubusercontent.com/u/7987454?v=4)](https://github.com/waibcam "waibcam (1 commits)")

---

Tags

auto-translationextract-stringsphp-translationtranslationtranslation-keyi18ntranslatetranslationSimple

### Embed Badge

![Health badge](/badges/diversen-simple-php-translation/health.svg)

```
[![Health](https://phpackages.com/badges/diversen-simple-php-translation/health.svg)](https://phpackages.com/packages/diversen-simple-php-translation)
```

###  Alternatives

[gettext/gettext

PHP gettext manager

70130.2M102](/packages/gettext-gettext)[gettext/languages

gettext languages with plural rules

7530.3M11](/packages/gettext-languages)[skillshare/formatphp

Internationalize PHP apps. This library provides an API to format dates, numbers, and strings, including pluralization and handling translations.

8029.6k](/packages/skillshare-formatphp)[om/potrans

Command line tool for translate Gettext with Google Translator API or DeepL API

10515.0k4](/packages/om-potrans)[fisharebest/localization

A lightweight localization database and translation tools, with data from the CLDR, IANA, ISO, etc.

3191.1k2](/packages/fisharebest-localization)[instaclick/translation-editor-bundle

Web editor UI to manage Symfony2 translations (Symfony2 bundle)

2019.5k](/packages/instaclick-translation-editor-bundle)

PHPackages © 2026

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