dataknightinc/i18n-resource-resolver

Library for internationalizing strings and other resources within an application


Keywords
container, framework, symfony, dependency, injection, slim, interop
License
GPL-2.0+

Documentation

dataknightinc/i18n-resource-resolver

This library is for enabling an internationalization (i18n) in an application. It allows an application to store localized resources in a catalog and to replace the actual resource in the code with a reference to that resource. When needed, the reference in the code can be replaced by retrieving the resource from a catalog according to a list of preferred languages.

Installation

Use composer to install the package:

composer require dataknightinc/i18n-resource-resolver

This package is supported for PHP versions 7.0 and later.

Resource Storage Options

By default, the library supports using either JSON files stored on the local filesystem or a Redis database for storing localized resources. You can write additional drivers by implementing the ResourceCatalogDriverInterface.

You should refer to the class documentation for details on how to configure each of the drivers and how to store resources in each source.

Example Usage

You should refer to the class documentation for details on how to instantiate objects and extend existing classes or interfaces. The code below assumes you are using JSON files stored in the 'strings' folder as the source for resources.

strings/en/main.json:

{
    "string1": "This is string1",
    "string2": "This is string2",
    "string3": "This is %choice[$i:{=0:%message%}{=1:str2}{>1:str3}]%"
}

strings/es/main.json:

{
    "string1": "Este es string1",
    "string2": "Este es string2",
    "string3": "Este es %choice[$i:{=0:%message%}{=1:str2}{>1:str3}]%"
}

strings/en/errors.json:

{
    "error1": "This is %errno%",
    "error2": "This is error2",
    "error3": "This is error3"
}
use DataKnight\Components\i18n\Drivers\LocalResourceCatalogDriver;
use DataKnight\Components\i18n\ResourceCatalog;
use DataKnight\Components\i18n\ResourceResolver;
use DataKnight\Interfaces\i18n\ResourceResolverInterface;

$driver = new LocalResourceCatalogDriver([
    'baseDir' => __DIR__ . '/strings',
    'fileExt' => 'json'
]);
$mainCatalog = new ResourceCatalog([
    'name' => 'main',
    'langs' => ['en', 'es'],
    'driver' => $driver
]);
$errorCatalog = new ResourceCatalog([
    'name' => 'errors',
    'langs' => ['en'],
    'driver' => $driver
]);
$resolver = new ResourceResolver([
    'langPrefs' => ['en-us', 'en-uk', 'es'],
    'langAliases' => [
        'en-us' => 'en',
        'en-uk' => 'en'
    ],
    'catalogs' => [$mainCatalog, $errorCatalog],
    'invalidResourceBehavior' => ResourceResolverInterface::INVALID_RESOURCE_RETURN_NULL,
    'defaultCatalog' => 'main'
]);

printf($resolver->translate('Error: %i18n[errors:error2]%'));
printf($resolver->translate('Error: %i18n[errors:error1]%'), ['errno' => 'an error']);
$i = 0;
printf($resolver->translate('Error: %i18n[main:string3]%'), ['message' => 'string3'], ['i' => $i]);

Documentation

Full HTML documentation generated from apigen on the classes, methods and properties within this library can be found within the doc folder.

Version History

Release 1.1.2 (Released May 7, 2016)

  • Modified doc blocks to work with apigen from phpdoc
  • Code now includes apigen generated documentation for you

Release 1.1.1 (Released April 19, 2016)

  • Fixed bugs with missing namespaces and catalog option validation

Release 1.1.0 (Released April 18, 2016)

  • Modified all interfaces to use Symfony's OptionsResolver component
  • Added type hints for all methods

Release 1.0.0 (Released April 14, 2016)

  • Initial release