Download the PHP package locomotivemtl/charcoal-translator without Composer
On this page you can find all versions of the php package locomotivemtl/charcoal-translator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package charcoal-translator
Charcoal Translator
A Charcoal service provider for the Symfony Translation component.
Provides internationalization (I18N) tools for translating messages and managing locales in multilingual applications.
Table of Contents
- Installation
- Dependencies
- Features
- The Translation Object
- The Translator Service
- The Locales Manager
- The Parser Script
- Service Provider
- Parameters
- Services
- Configuration
- Middleware
- Helpers
- TranslatorAwareTrait
- Development
- Development dependencies
- Continuous Integration
- Coding Style
- Credits
- License
Installation
-
The preferred (and only supported) method is with Composer:
-
Add the service provider and configure the default translator / locale services via the application configset:
or via the service container:
If you are using locomotivemtl/charcoal-app, the [TranslatorServiceProvider
][translator-provider] is automatically registered by the AppServiceProvider
.
Dependencies
Required
- PHP 5.6+: PHP 7 is recommended.
- symfony/translation: Translation component which Charcoal's translator service extends.
- pimple/pimple: PSR-11 compliant service container and provider library.
- locomotivemtl/charcoal-config: For configuring the translator service and locales.
PSR
- PSR-7: Common interface for HTTP messages. Followed by
LanguageMiddleware
. - PSR-11: Common interface for dependency containers. Fulfilled by Pimple.
Dependents
- locomotivemtl/charcoal-admin: Admin interface for Charcoal applications.
- locomotivemtl/charcoal-app: PSR-7 compliant framework for web applications.
For resolving the current locale via theLanguageMiddleware
. - locomotivemtl/charcoal-cms: Pre-designed models and basic utilities for content management (pages, news, events).
For supporting multilingual model properties and localizing model descriptors. - locomotivemtl/charcoal-property: Model property values and metadata.
For supporting multilingual values and localizing property descriptors.
Features
The Translation Object
Charcoal\Translator\Translation
The Translation Object holds the translation data for a given string in all available languages / locales.
The Translator Service
Charcoal\Translator\Translator
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)
.
The Locales Manager
Charcoal\Translator\LocalesManager
The Locales Manager is used to manage available locales / languages and keep track of current language.
The Parser Script
Charcoal\Translator\Script\TranslationParserScript
The Parser Script is used to scrape files that contain translatable content. Add the following route to your application configuration:
Service Provider
The TranslatorServiceProvider
provides services and options for translating your application into different languages.
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
- locales/manager: An instance of
LocalesManager
, used for handling available languages, their definitions, the default language, and tracks the current language. - translator: An instance of
Translator
, that is used for translation. - translator/message-selector: An instance of
Symfony\Component\Translation\MessageSelector
. - translator/loader/*: Instances of the translation
Symfony\Component\Translation\Loader\LoaderInterface
.
Configuration
Here is an example of configuration:
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.
If you are using locomotivemtl/charcoal-app, you can add the middleware via the application configset:
Otherwise, with Slim, for example:
The middleware comes with a set of default options which can be individually overridden.
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
The TranslatorAwareTrait
is offered as convenience to avoid duplicate / boilerplate code. It simply sets and gets a Translator
service property.
Set with setTranslator()
and get with translator()
. Both are protected method. (This trait has no public interface.)
Development
To install the development environment:
To run the scripts (phplint, phpcs and phpunit):
API Documentation
- The auto-generated
phpDocumentor
API documentation is available at:
https://locomotivemtl.github.io/charcoal-translator/docs/master/ - The auto-generated
apigen
API documentation is available at:
https://codedoc.pub/locomotivemtl/charcoal-translator/master/
Development Dependencies
Coding Style
The charcoal-translator module follows the Charcoal coding-style:
- PSR-1
- PSR-2
- PSR-4, autoloading is therefore provided by Composer.
- phpDocumentor comments.
- .editorconfig for coding standards.
Coding style validation / enforcement can be performed with
composer phpcs
. An auto-fixer is also available withcomposer phpcbf
.
Credits
License
Charcoal is licensed under the MIT license. See LICENSE for details.