2017 © Pedro Pelรกez
 

library bono-lang

Bono Localization Package

image

krisanalfa/bono-lang

Bono Localization Package

  • Tuesday, November 4, 2014
  • by krisanalfa
  • Repository
  • 1 Watchers
  • 0 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Bono Localization Package

Want some translation in your Bono-Based Application? Use this component. Code like a boss., (*1)

Installation

Add this package to composer.json require file:, (*2)

```lang-js "require": { "krisanalfa/bono-lang": "dev-master", },, (*3)


# Activate Localization Package Put the Provider to your `bono.providers` configuration: ```lang-php 'bono.providers' => array( '\\Bono\\Lang\\Provider\\LangProvider', ),

Configuration

Basic configuration is:, (*4)

```lang-php 'bono.providers' => array( '\Bono\Lang\Provider\LangProvider' => array( 'driver' => '\Bono\Lang\Driver\FileDriver', 'lang' => 'en', 'debug' => true ), ),, (*5)


- `driver` is a class that responsible to get all list of registered Language and get words for each language available in list - `lang` is active language you want to use to translate, you can use a `closure` to set the default language, see example below - `debug` when you set this config to `false`, and you get nothing from translation, the result is empty string, but if it sets to `true`, you get some debug keyword Using closure to determine which language is set to be default one ```lang-php array( 'driver' => '\\Bono\\Lang\\Driver\\FileDriver', 'lang' => function() { return $_SESSION['user']['config']['lang']; }, 'debug' => true ),

Basic FileDriver Dictionary Knowledge

You can change the location of your dictionary, by add lang.path in configuration files:, (*6)

```lang-php 'bono.providers' => array( '\Bono\Lang\Provider\LangProvider' => array( 'driver' => '\Bono\Lang\Driver\FileDriver', 'lang' => 'en', 'debug' => true, 'lang.path' => 'lang' ), ),, (*7)


So in your **root project**, you should create a `lang` folder , and put your dictionary inside that folder.

{{ ROOT }} โ”œโ”€โ”€ composer.json โ””โ”€โ”€ lang โ”œโ”€โ”€ en โ”‚ โ””โ”€โ”€ list.php โ”‚ โ””โ”€โ”€ anotherList.php โ””โ”€โ”€ id โ””โ”€โ”€ list.php โ””โ”€โ”€ extraList.php, (*8)


When you want Spanish language exists in your repository, make an `es` folder in `lang` folder. The structure of your dictionary should be like this:

{{ ROOT }} โ”œโ”€โ”€ composer.json โ””โ”€โ”€ lang โ”œโ”€โ”€ en โ”‚ โ””โ”€โ”€ list.php โ”‚ โ””โ”€โ”€ anotherList.php โ”œโ”€โ”€ id โ”‚ โ””โ”€โ”€ list.php โ”‚ โ””โ”€โ”€ extraList.php โ””โ”€โ”€ es โ””โ”€โ”€ list.php โ””โ”€โ”€ spanishList.php, (*9)


```lang-php return array( 'full_name' => 'Full Name', 'birth_date' => 'Birth Date', 'message' => 'Hello', 'flash' => array( 'fail' => 'Sorry, app is currently fucked up', // nested content 'success' => 'Hurray, that is that I am talking about', // yet another nested content ), );

Example dictionary

Let's say this is our dictionary, (*10)

```lang-php return array( 'full_name' => 'Full Name', 'birth_date' => 'Birth Date', 'message' => 'Hello', 'flash' => array( 'fail' => 'Sorry, app is currently fucked up', // nested content 'success' => 'Hurray, that is that I am talking about', // yet another nested content ), );, (*11)


#Usage (Based on example dictionary) You can access translation from application container context, such as: ```lang-php $app = \Bono\App::getInstance(); $translator = $app->translator; $word = $translator->translate('message'); echo $word; // output => 'Hello'

Or via alias function:, (*12)

```lang-php $word = t('message'); // 't' is an alias for 'translate' method in $translator echo $word; // output => 'Hello', (*13)


--- You can append some parameter to your language. But, first, you have to confirm that your line, accept the parameter by change it to ```lang-php // List of English dictionary array( // ... snip 'message' => 'Hello, :name', // ... snip );

So you can append parameter to your dictionary by:, (*14)

```lang-php $app = \Bono\App::getInstance(); $translator = $app->translator; $word = $translator->translate('message', array('name' => 'Alfa')); echo $word; // output => 'Hello, Alfa', (*15)


--- The third argument in `translate` method is option to give translator a default translation if there's no translation in dictionary: ```lang-php $app = \Bono\App::getInstance(); $translator = $app->translator; $word = $translator->translate('n00p', array(), 'Default one'); echo $word; // output => 'Default one.'

When you want to access your nested dictionary:, (*16)

```lang-php $app = \Bono\App::getInstance(); $translator = $app->translator; $flash = $translator->translate('flash.fail'); echo $flash; // output => 'Sorry, app is currently fucked up', (*17)


--- This package also support for choice if there's multiple message in a key, something like this: ```lang-php // List of English dictionary array( // ... snip 'options' => 'Sagara|Xinix|Solusitama', // ... snip );

If you want to take Sagara in options key, you can access them by:, (*18)

```lang-php $app = \Bono\App::getInstance(); $translator = $app->translator; $word = $translator->choice('options', 1); echo $word; // output => 'Sagara', (*19)


--- You can also put any placeholder in `choice`, something like this: ```lang-php // List of English dictionary array( // ... snip 'options' => 'Sagara|Xinix :string|Solusitama', // ... snip );

```lang-php $app = \Bono\App::getInstance(); $translator = $app->translator; $word = $translator->choice('options', 2, array('string' => 'Technology')); echo $word; // output => 'Xinix Technology', (*20)


--- The `count` variable is automatically appended in `choice` method: ```lang-php // List of English dictionary array( // ... snip 'options' => 'Sagara|Xinix :string|Solusitama :count', // ... snip );

```lang-php $app = \Bono\App::getInstance(); $translator = $app->translator; $word = $translator->choice('options', 3); echo $word; // output => 'Solusitama 3', (*21)


# Some other method you can access ```lang-php $app = \Bono\App::getInstance(); $translator = $app->translator; // Get current active driver $translator->getDriver(); // Get current active language $translator->getLanguage(); // Set Spanish to our current active language $translator->setLanguage('es'); // Determine if we have Spanish language in our repository $translator->hasLanguage('es');

LICENSE

MIT LICENSE

Copyright (c) 2014 Krisan Alfa Timur (PT. Sagara Xinix Solusitama)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The Versions

04/11 2014

dev-master

9999999-dev

Bono Localization Package

  Sources   Download

MIT

04/11 2014

0.1.1

0.1.1.0

Bono Localization Package

  Sources   Download

MIT

03/11 2014

0.1.0

0.1.0.0

Bono Localization Package

  Sources   Download

MIT

17/06 2014

0.0.2

0.0.2.0

Bono Localization Package

  Sources   Download

MIT

04/06 2014

0.0.1

0.0.1.0

Bono Localization Package

  Sources   Download

MIT