Giter Site home page Giter Site logo

gettext-translator's Introduction

Gettext Translator
==================

This is an add-on for the Nette Framework.


Installation
------------

This library does not require any PHP extension to be installed. It can read and
parse the binary `.mo` files on its own. This library uses several classes and
interfaces from the Nette Framework. You have to load them manually or using
auto-loading.

This library does not use the native PHP extension, so loading of big files can
be very slow. It is recommended to cache the whole object.


Usage
-----

On PHP 5.2.0 you have to remove the `use ...;` statement at the beginning of
the file.

This is how to create a new translator instance:

    $translator = new GettextTranslator('locale.mo');

You can use the following code to get the translations on-the-fly:

    $translator = new GettextTranslator('locale.cs.mo', 'cs'); // the second parameter is optional
    echo $translator->translate("Dog"); // Pes
    echo $translator->translate("Dog", 2); // Psi
    echo $translator->translate("There is %d unread comment in thread %s.", $number, 'Changelog');

    // get locale (optional)
    echo $translator->locale; // cs

Localization of Nette forms:

    $translator = new GettextTranslator('form.cs.mo', 'cs');
    $form = new Form;
    $form->setTranslator($translator);

Localization of Nette Latte templates:

    $template->setTranslator(new GettextTranslator('template.cs.mo'));

    {_'Hello! Welcome to our page!'}
    {_'There is %d unread comment in thread %s.', $number, 'Changelog'}
    {_'In thread %2$s is %1$d unread comment.', $number, 'Changelog'}

This is how to store translators in Nette cache:

    function createGettextTranslator($file, $locale = NULL)
    {
        return new GettextTranslator($file, $locale);
    }

    $cache = new Cache($storage);
    $translator = $cache->call('createGettextTranslator', 'locale.cs.mo', 'cs');


Plural Forms
------------

The translator uses English plural forms by default.

    nplurals=2; plural=n!=1;

You can specify your own in Plural-Forms header of your translation file. For
instance, this can be used for the Czech language:

    nplurals=3; plural=n==1 ? 0 : n>=2 && n<=4 ? 1 : 2;


Minimum requirements
--------------------

- PHP 5.2.0 or 5.3.0 (http://php.net)
- Nette Framework 2.0 (http://nette.org)


Documentation, Examples, Sandbox, Tools
---------------------------------------

homepage: http://addons.nette.org/gettext-translator
repository: http://github.com/fabik/gettext-translator

gettext-translator's People

Contributors

fabik avatar

Stargazers

 avatar  avatar  avatar

Watchers

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