2017 © Pedro Peláez
 

library charcoal-translator

Charcoal service provider for translating messages and managing locales.

image

locomotivemtl/charcoal-translator

Charcoal service provider for translating messages and managing locales.

  • Tuesday, June 5, 2018
  • by mducharme
  • Repository
  • 10 Watchers
  • 0 Stars
  • 8,205 Installations
  • PHP
  • 9 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 24 Versions
  • 13 % Grown

The README.md

Charcoal Translator

![License][badge-license] Latest Stable Version ![Code Quality][badge-scrutinizer] Coverage Status ![SensioLabs Insight][badge-sensiolabs] Build Status, (*1)

A Charcoal service provider for the Symfony Translation component., (*2)

Provides internationalization (I18N) tools for translating messages and managing locales in multilingual applications., (*3)

Table of Contents

Installation

  1. The preferred (and only supported) method is with Composer:, (*4)

    ★ composer require locomotivemtl/charcoal-translator
    
  2. Add the service provider and configure the default translator / locale services via the application configset:, (*5)

    "service_providers": {
        "charcoal/translator/service-provider/translator": {}
    },
    
    "translator": {
        // …
    },
    
    "locales": {
        // …
    }
    

    or via the service container:, (*6)

    $container->register(new \Charcoal\Translator\ServiceProvider\TranslatorServiceProvider());
    
    $container['translator/config'] = new \Charcoal\Translator\TranslatorConfig([
        // …
    ]);
    
    $container['locales/config'] = new \Charcoal\Translator\LocalesConfig([
        // …
    ]);
    

If you are using locomotivemtl/charcoal-app, the [TranslatorServiceProvider][translator-provider] is automatically registered by the AppServiceProvider., (*7)

Dependencies

Required

PSR

  • PSR-7: Common interface for HTTP messages. Followed by LanguageMiddleware.
  • PSR-11: Common interface for dependency containers. Fulfilled by Pimple.

Dependents

Features

The Translation Object

Charcoal\Translator\Translation, (*8)

The Translation Object holds the translation data for a given string in all available languages / locales., (*9)

// Get a translation object from the Translator
$translation = $container['translator']->translation([
    'en' => 'Hello World',
    'fr' => 'Bonjour'
]);

// If cast to string, the default language will be used.
echo $translation;

// Use ArrayAccess to get (or set) a translated value.
echo $translation['fr'];
$translation['fr'] => 'Bonjour le monde';

// To loop through all translations:
foreach ($translation->data() as $lang => $translatedValue) {
    // ...
}

The Translator Service

Charcoal\Translator\Translator, (*10)

Charcoal's Translator extends Symfony's Translator to also provide two new translation methods (translation($val) and translator($val)) which can both accept mixed arguments to return either a Translation object, in the case of translation() or a string, in the case of translate($val)., (*11)

The Locales Manager

Charcoal\Translator\LocalesManager, (*12)

The Locales Manager is used to manage available locales / languages and keep track of current language., (*13)

The Parser Script

Charcoal\Translator\Script\TranslationParserScript, (*14)

The Parser Script is used to scrape files that contain translatable content. Add the following route to your application configuration:, (*15)

"scripts": {
    "charcoal/translator/parse": {
        "ident": "charcoal/translator/script/translation-parser"
    }
}

Service Provider

The TranslatorServiceProvider provides services and options for translating your application into different languages., (*16)

Parameters

  • locales/config: Configuration object for defining the available languages, fallbacks, and defaults.
  • locales/default-language: Default language of the application, optionally the navigator's preferred language.
  • locales/browser-language: Accepted language from the navigator.
  • locales/fallback-languages: List of fallback language codes for the translator.
  • locales/available-languages: List of language codes from the available locales.
  • locales/languages: List of available language structures of the application.
  • translator/config: Configuration object for translation service, message catalogs, and catalog loaders.
  • translator/translations: Dictionary of additional translations grouped by domain and locale.

Services

Configuration

Here is an example of configuration:, (*17)

"locales": {
    "languages": {
        "de": {},
        "en": {},
        "es": {
            "active": false
        },
        "fr": {}
    },
    "default_language": "fr",
    "fallback_languages": [
        "en", 
        "fr"
    ],
    "auto_detect": true
},
"translator": {
    "loaders": [
        "xliff",
        "json",
        "php"
    ],
    "paths": [
        "translations/",
        "vendor/locomotivemtl/charcoal-app/translations/"
    ],
    "debug": false,
    "cache_dir": "cache/translation/",
    "translations": {
        "messages": {
            "de": {
                "hello": "Hallo {{ name }}",
                "goodbye": "Auf Wiedersehen!"
            },
            "en": {
                "hello": "Hello {{ name }}",
                "goodbye": "Goodbye!"
            },
            "es": {
                "hello": "Hallo {{ name }}",
                "goodbye": "Adios!"
            },
            "fr": {
                "hello": "Bonjour {{ name }}",
                "goodbye": "Au revoir!"
            }
        },
        "admin": {
            "fr": {
                "Save": "Enregistrer"
            }
        }
    }
}

Middleware

The LanguageMiddleware is available for PSR-7 applications that support middleware. The middleware detects the preferred language using the Accept-Language HTTP header, the URI path, query string, or host., (*18)

If you are using locomotivemtl/charcoal-app, you can add the middleware via the application configset:, (*19)

"middlewares": {
    "charcoal/translator/middleware/language": {
        "active": true,
        "use_params": true,
        "param_key": "hl"
    }
}

Otherwise, with Slim, for example:, (*20)

$app = new \Slim\App();

// Register middleware
$app->add(new \Charcoal\Translator\Middleware\LanguageMiddleware([
    'default_language' => 'fr',
    'use_params'       => true,
    'param_key'        => 'hl',
]));

The middleware comes with a set of default options which can be individually overridden., (*21)

Setting Type Default Description
active boolean FALSE Whether to enable or disable the middleware (locomotivemtl/charcoal-app only).
default_language string null The default language to use if no other languages is choosen.
browser_language string null The client's preferred language (Accept-Language).
use_browser boolean true Whether to use browser_language as the default language.
use_path boolean true Whether to lookup the HTTP request's URI path for a language code.
excluded_path `string array` ^/admin\b | One or more RegEx patterns to ignore from localization, when matching the URI path.
path_regexp `string array` ^/([a-z]{2})\b | One or more RegEx patterns to include from localization, when matching the URI path.
use_params boolean false Whether to lookup the HTTP request's URI query string for a language code.
param_key `string array` current_language | One or more RegEx patterns to include from localization, when matching the query string keys.
use_session boolean true Whether to lookup the client's PHP session for a preferred language.
session_key `string array` current_language | One or more RegEx patterns to include from localization, when matching the session keys.
use_host boolean false Whether to lookup the server host for a language code.
host_map `string array` [] | One or more RegEx patterns to include from localization, when matching the host.
set_locale boolean true Whether to set the environment's locale.

Helpers

TranslatorAwareTrait

Charcoal\Translator\TranslatorAwareTrait, (*22)

The TranslatorAwareTrait is offered as convenience to avoid duplicate / boilerplate code. It simply sets and gets a Translator service property., (*23)

Set with setTranslator() and get with translator(). Both are protected method. (This trait has no public interface.), (*24)

Development

To install the development environment:, (*25)

★ composer install --prefer-source

To run the scripts (phplint, phpcs and phpunit):, (*26)

★ composer tests

API Documentation

Development Dependencies

Coding Style

The charcoal-translator module follows the Charcoal coding-style:, (*27)

Coding style validation / enforcement can be performed with composer phpcs. An auto-fixer is also available with composer phpcbf., (*28)

Credits

License

Charcoal is licensed under the MIT license. See LICENSE for details., (*29)

The Versions

05/06 2018

dev-master

9999999-dev https://locomotivemtl.github.io/charcoal-translator/

Charcoal service provider for translating messages and managing locales.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

translation translator i18n l10n g11n charcoal

29/05 2018

dev-mcaskill-patch-scripts

dev-mcaskill-patch-scripts https://locomotivemtl.github.io/charcoal-translator/

Charcoal service provider for translating messages and managing locales.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

translation translator i18n l10n g11n charcoal

29/05 2018

0.3.5

0.3.5.0 https://locomotivemtl.github.io/charcoal-translator/

Charcoal service provider for translating messages and managing locales.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

translation translator i18n l10n g11n charcoal

18/05 2018

dev-mcaskill-patch-exists

dev-mcaskill-patch-exists https://locomotivemtl.github.io/charcoal-translator/

Charcoal service provider for translating messages and managing locales.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

translation translator i18n l10n g11n charcoal

16/05 2018

0.3.4

0.3.4.0 https://locomotivemtl.github.io/charcoal-translator/

Charcoal service provider for translating messages and managing locales.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

translation translator i18n l10n g11n charcoal

14/05 2018

0.3.3

0.3.3.0 https://locomotivemtl.github.io/charcoal-translator/

Charcoal service provider for translating messages and managing locales.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

translation translator i18n l10n g11n charcoal

20/02 2018

dev-mcaskill-patch-5

dev-mcaskill-patch-5

Charcoal Translator and locales management.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

19/02 2018

dev-mcaskill-patch-4

dev-mcaskill-patch-4

Charcoal Translator and locales management.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

06/10 2017

dev-locale-from-host

dev-locale-from-host

Charcoal Translator and locales management.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

21/06 2017

0.1.3

0.1.3.0

Charcoal Translator and locales management.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

27/02 2017

0.1.2

0.1.2.0

Charcoal Translator and locales management.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

08/02 2017

0.1.1

0.1.1.0

Charcoal Translator and locales management.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

07/02 2017

0.1

0.1.0.0

Charcoal Translator and locales management.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme