Coder Social home page Coder Social logo

borges's Introduction

PHP I18n

PHP I18n is a simple library for storing localized strings outside of your code.

Installing

Add the following require to your composer.json file.

{
    "require": {
        "teacup/borges": "1.*"
    }
}

Setup

Language Folder

In order to use i18n, you will need a language folder. By convention this folder is config/locales, however, you can choose whichever you want. In this folder, each supported language requires its own folder (config/locales/en, config/locales/de, ...).

Use in PHP

You will most likely be using the I18n class in your own implementation, so you are just given a class which you can write a wrapper class for in your own code. All you need to do to create this class is give it the path to the language folder you decided on before.

<?php

use Teacup\I18n\I18n as Lang;

$lang = new Lang( dirname(__FILE__) . '/config/locales' );

Example Usage

Notice: Currently, only yaml files are supported.

For this example, the language folder is at config/locales.

Create a file config/locales/en/example.yml

foo: bar
nested:
  foo: bar
var: foo :var
plural:
  one: A :color apple
  other: :count :color apples

And a dialect file config/locales/en_US/example.yml

foo: murica

Note: Take note of the yml file name as it will be the first part of your key (example.yml => example).

Now we can load the translation class and get our translated string.

<?php

use Teacup\I18n\I18n as Lang;

$lang = new Lang( dirname(__FILE__) . 'config/locales' );

/**
 * normal translated string
 *
 * @param string $locale
 * @param string $key
 */
$lang->get( 'en', 'example.foo' ); // returns 'bar'


/**
 * nested translated strings
 *
 * @param string $locale
 * @param string $key
 */
$lang->get( 'en', 'example.nested.foo' ); // returns 'bar'

/**
 * Variable translated string
 *
 * @param string $locale
 * @param string $key
 */
$lang->get( 'en', 'example.var', ['var' => 'bar']); // returns 'foo bar'

/**
 * Plural tranlated strings
 *
 * @param string $locale
 * @param string $key
 * @param int    $count
 */
$lang->get( 'en', 'example.plural', ['color' => 'red'], 1); // returns 'A red apple'
$lang->get( 'en', 'example.plural', ['color' => 'red'], 2); // returns '2 red apples'

/**
 * Dialect translated strings
 */
$lang->get( 'en_US', 'example.foo' );         // returns 'murica'
$lang->get( 'en_US', 'example.nested.foo' );  // returns 'bar' as it falls back to en

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.