Coder Social home page Coder Social logo

locale's Introduction

Locale Library

Build Status

SensioLabsInsight

Documentation

Installation and usage

Installation and usage is a quick:

  1. Download Locale using composer
  2. Use the library
  3. Customize Data Provider

You can "fdevs/locale-bridge" for use with other libraries/components

Step 1: Download Locale library using composer

Download the bundle by running the command:

$ php composer.phar require fdevs/locale

Composer will install the bundle to your project's vendor/fdevs directory.

Step 2: Use the library

####Basic setup:

<?php

require DIR . '/../vendor/autoload.php';

use FDevs\Locale\Model\LocaleText;
use FDevs\Locale\Translator;

// The same text in different languages
$englishText = new LocaleText('I am a programmer', 'en');
$chineseText = new LocaleText('我是程序员', 'zh');
$russianText = new LocaleText('Я программист', 'ru');

$supportedTexts = [
    $englishText,
    $russianText,
    $chineseText,
];

$trans = new Translator();

####Set current locale:

$trans->setLocale('zh');

####Get text for current locale:

// 1. Get text for current locale - ch (Chinese)
$text = $trans->trans($supportedTexts);
echo $text?$text->getText():'';
// Output: "我是程序员"

####Get text for locale - ru:

$text = $trans->trans($supportedTexts, 'ru');
echo $text?$text->getText():'';
// Output: "Я программист"

####Get text for a locale for which we don't have translation:

$text = $trans->trans($supportedTexts, 'kk');
echo $text?$text->getText():'';
// Output: ""

####Get text using a set of prioritized locales: Here you can treat this as locales fallback, first found locale from your list will be chosen.

use FDevs\Locale\Model\PriorityLocale;
use FDevs\Locale\TranslatorPriority;

$priorityLocale = [
    new PriorityLocale('uk',['en','ru']),
    new PriorityLocale('en',['uk']),
    new PriorityLocale('fa',['zh','en']),
];

$trans = new TranslatorPriority('en',$priorityLocale);
$text = $trans->trans($supportedTexts, 'uk');
echo $text?$text->getText():'';
// Output: "I am programmer"

Step 3: Customize Data Provider

create your awesome provider

use FDevs\Locale\DataProvider\DataProviderInterface;

class MyProvider implements DataProviderInterface
{
//implement interface
}

add your provider

use FDevs\Locale\DataProvider\DataProviderRegistry;
use FDevs\Locale\Translator;
use FDevs\Locale\TranslatorPriority;

$registry = new DataProviderRegistry([new MyProvider()]);
$translator = new Translator('en',$registry);
//or
$translator = new TranslatorPriority('en', $priorityLocale, $registry);

License

This library is under the MIT license. See the complete license in the Library:

LICENSE

Reporting an issue or a feature request

Issues and feature requests are tracked in the Github issue tracker.

locale's People

Contributors

andrey1s avatar vmelnik-ukraine avatar

Watchers

James Cloos avatar Maxim avatar  avatar  avatar

Forkers

madman

locale's Issues

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.