Giter Club home page Giter Club logo

illuminatetranslation's Introduction

#Using Laravel’s localisation component outside of laravel

Using the Illuminate/translator module independently from laravel is surprisingly straightforward.

##Add it to your composer dependencies To use the translation component, you will need to add it to your componser.json file:

"require": {
    "illuminate/translation": "4.*"
}

Once this is done, you will need to update your dependencies:

composer update

##Create an instance of Translator Once composer has updated, you will need to create an instance of Illuminate\Translation\Translator, and it's dependencies.

You will also need to tell it the default localisation, and the path to the localisation files.

use Illuminate\Translation\Translator;
use Illuminate\Translation\FileLoader;
use Illuminate\Filesystem\Filesystem;

$locale = 'en';
$path = __DIR__.'/locale';

//load localisation manager
$translationLoader = new FileLoader(new Filesystem,$path);
$localiser = new Translator($translationLoader,$locale);

##Create your localisation files

with the given setup, the translator will expect to find your localisation files under locale/{language_code}/{localisation_file}

i.e.

|-locale
    |
    |-en
    |  |-localisation.php
    |
    |-fr
    |  |-localisation.php
    |
    |-es
    |  |-localisation.php
    |

These should return a keyed array.

<?php
return [
    'heading' => 'Heading Text'
];

##Using the localiser

You should then be able to use the localiser by calling get on the instance, passing ing the localisation file to use, and the key to look up:

<h1>
    <?= $localiser->get('page.heading');?>
</h1>
<p>
    <em>
        <?= $localiser->get('page.abstract');?>
    </em>
</p>

##Example Project

There is an example project showing this in use at github.com/mattcannon/illuminateTranslation

illuminatetranslation's People

Contributors

mattcannon 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.