PHP code example of sunrise / translator

1. Go to this page and download the library: Download sunrise/translator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sunrise / translator example snippets


// Contents of this example translation file (located at /translations/sr.php):

return [
    'Hello, {username}!' => 'Zdravo, {username}!',
];

use Sunrise\Translator\TranslatorManager;
use Sunrise\Translator\Translator\DirectoryTranslator;

$translator = new TranslatorManager(
    translators: [
        new DirectoryTranslator(
            domain: 'app',
            directory: '/translations',
        ),
    ],
);

// Result: Zdravo, Marko!
$translator->translate(domain: 'app', locale: 'sr', template: 'Hello, {username}!', placeholders: ['{username}' => 'Marko']);

use DI\ContainerBuilder;
use Sunrise\Translator\TranslatorManagerInterface;

$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinition(__DIR__ . '/../vendor/sunrise/translator/resources/definitions/translator_manager.php');

$container = $containerBuilder->build();

// See above for usage examples.
$translator = $container->get(TranslatorManagerInterface::class);