Giter VIP home page Giter VIP logo

translate's Introduction

Fast, powerful, scalable and customizable php translator library

Scrutinizer Code Quality Build Status Code Intelligence Status Total Downloads

You can install the package via composer:

composer require tleckie/translate

Usage

Create an instance of the Translator class to which you will assign a loader. The loader must have the directory in which the translation files are stored, it must also indicate the extension of the translation files.

<?php

use Tleckie\Translate\Loader\ArrayLoader;
use Tleckie\Translate\Translator;

$trans = new Translator(
    new ArrayLoader('./translations/','php'),
    'es_ES'
);

$trans->trans('hello');

$trans->trans('other.value');

Your translations file should look like this:

<?php

return [
  'hello' => "Hola! Bienvenido a mi sitio web!",
  'other.value' => "Otro valor",
  .
  .
  .
];

The name of the file that stores the translation should look like this:

/translations/es_ES.php

The trans() method takes multiple arguments. The first is the key of the translation array, it is the value that will be searched for in its corresponding file.

The second argument (optional) is an array of values to be replaced in the translated value. If you want to add values to your translated text you must indicate it with "%s". Note that the number of arguments must match the number of "%s".

<?php

use Tleckie\Translate\Loader\ArrayLoader;
use Tleckie\Translate\Translator;

$trans = new Translator(
    new ArrayLoader('./translations/','php'),
    'es_ES'
);

$trans->trans('hello',['John']);

Your translations file should look like this:

<?php

return [
  'hello' => "Hola %s! Bienvenido a mi sitio web!"
];

Even if you configure your translator to load a specific language, you can also change the language at any time if you need.

<?php

use Tleckie\Translate\Loader\ArrayLoader;
use Tleckie\Translate\Translator;

$trans = new Translator(
    new ArrayLoader('./translations/','php'),
    'es_ES'
);

$trans->trans('hello',['John'],null, 'en_GB');

You can create your translation files by specifying the language and country defined by the provided locale. You can also decide whether to use the same language for different countries. Regional configuration example: "en_US" and "en_GB" In this case, you just need to create a file that has the following name:

/translations/en.php

In that case the following calls will have the same result and will load the translations from the same file.

<?php

use Tleckie\Translate\Loader\ArrayLoader;
use Tleckie\Translate\Translator;

$trans = new Translator(
    new ArrayLoader('./translations/','php'),
    'es_ES'
);

$trans->trans('hello',['John'],null, 'en_GB');
$trans->trans('hello',['John'],null, 'en_US');

You can also implement your own loader to connect to your preferred data source. You simply have to implement the LoaderInterface interface.

That's all! I hope this helps you ;)

translate's People

Contributors

teodoroleckie avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.