2017 © Pedro Peláez
 

library simple-php-translation

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

image

diversen/simple-php-translation

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

  • Friday, December 30, 2016
  • by diversen
  • Repository
  • 1 Watchers
  • 10 Stars
  • 582 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 15 Versions
  • 4 % Grown

The README.md

Brief Overview

The simple-php-translation is a simple solution for adding translations to your PHP apps., (*1)

Install:, (*2)

composer require diversen/simple-php-translation

If you want to use google auto translate, require the following:, (*3)

composer require google/cloud-translate

Translations are placed in files named:, (*4)

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

E.g. inside your app test_app, (*5)

test_app/lang/en/language.php

The language.php file could consist of this:, (*6)

~~~.php $LANG = array (); $LANG['Welcome to my blog'] = 'Welcome to my blog';, (*7)


A Danish translation could be found in: test_app/lang/da/language.php And this `language.php` could consist of: ~~~.php $LANG = array (); $LANG['Welcome to my blog'] = 'Velkommen til min blog';

Load language

~~~.php use Diversen\Lang;, (*8)

$l = new Lang();, (*9)

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

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

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

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

// now all language files are loaded, and we can translate, (*14)


# Translate ~~~.php // 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 <span class="notranslate">{ID}</span> has been locked!', array ('ID' => $id))

Extract strings

This will extract all Lang::translate calls, and add new values to translation files., (*15)

~~~.php use Diversen\Translate\Extractor;, (*16)

// same pattern as above for extraction, (*17)

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

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

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

$e->updateLang();, (*21)


> 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 You will need to setup google cloud translation: [https://cloud.google.com/translate/docs/basic/setup-basic](https://cloud.google.com/translate/docs/basic/setup-basic) 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. ~~~.php // 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

Inside the test_app/ directory, there is a small php app consisting of one PHP file: 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., (*22)

The Javascript file test_app/js/lang.js will do the translation., (*23)

Start the app:, (*24)

php -S localhost:8000 -t test_app

Visit http://localhost:8000, (*25)

You can also test the danish translation at:, (*26)

http://localhost:8000?lang=da, (*27)

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

You can test this by removing the en language file directory:, (*29)

rm -rf test_app/lang/en

Extract the english translation en using the script test/extract.php., (*30)

php test/extract.php

Reload the browser., (*31)

Now all translation are loaded from a file and the prefix 'NT: ' is removed., (*32)

There is also a small script for translating into danish (da). test/google_translate.php, (*33)

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

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

To:, (*35)

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

Now you can run:, (*36)

php test/google_translate.php

The translation will look like this: test_app/lang/da/language.php, (*37)

Finally there is 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, (*38)

License

MIT © Dennis Iversen, (*39)

The Versions

30/12 2016

dev-master

9999999-dev https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

29/10 2016

1.0.25

1.0.25.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

01/03 2016

1.0.24

1.0.24.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

01/03 2016

1.0.23

1.0.23.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

01/03 2016

1.0.22

1.0.22.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

01/12 2015

1.0.21

1.0.21.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

08/10 2015

1.0.19

1.0.19.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

10/08 2015

1.0.18

1.0.18.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

10/08 2015

1.0.17

1.0.17.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

10/08 2015

1.0.16

1.0.16.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

09/08 2015

1.0.15

1.0.15.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

08/08 2015

1.0.14

1.0.14.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

08/08 2015

1.0.11

1.0.11.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

08/08 2015

1.0.12

1.0.12.0 https://github.com/diversen/simple-php-translation

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate

08/08 2015

1.0.1

1.0.1.0 http://github.com/diversen/simple-php-github-api

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

  Sources   Download

BSD-3-Clause

The Requires

 

translation i18n simple translate