1. Go to this page and download the library: Download tobento/app-translation 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/ */
tobento / app-translation example snippets
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app');
// You might add trans directory:
$app->dirs()
->dir($app->dir('app').'trans', 'trans', group: 'trans', priority: 100);
// if not set, the same dir is specified by the boot as default!
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
// Run the app
$app->run();
use Tobento\App\AppFactory;
use Tobento\Service\Translation\TranslatorInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app');
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
$app->booting();
$translator = $app->get(TranslatorInterface::class);
$translated = $translator->trans(
message: 'Hi :name',
parameters: [':name' => 'John'],
locale: 'de'
);
// or using the app macro:
$translated = $app->trans(
message: 'Hi :name',
parameters: [':name' => 'John'],
locale: 'de'
);
// Run the app
$app->run();
use Tobento\Service\Translation\TranslatorInterface;
class SomeService
{
public function __construct(
protected TranslatorInterface $translator,
) {}
}
use Tobento\App\Boot;
use Tobento\App\Translation\Boot\Translation;
class AnyServiceBoot extends Boot
{
public const BOOT = [
// you may ensure the view boot.
Translation::class,
];
public function boot(Translation $translation)
{
$translated = $translation->trans(
message: 'Hi :name',
parameters: [':name' => 'John'],
locale: 'de'
);
}
}
use function Tobento\App\Translation\{trans};
$translated = trans(
message: 'Hi :name',
parameters: [':name' => 'John'],
locale: 'de'
);
use Tobento\App\AppFactory;
use Tobento\Service\Translation\TranslatorInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app');
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
$app->booting();
$translator = $app->get(TranslatorInterface::class);
// set the default locale:
$translator->setLocale('de');
// set the locale fallbacks:
$translator->setLocaleFallbacks(['de' => 'en']);
// set the locale mapping:
$translator->setLocaleMapping(['de' => 'de-CH']);
// or using the app on method:
$app->on(TranslatorInterface::class, function(TranslatorInterface $translator) {
$translator->setLocale('de');
$translator->setLocaleFallbacks(['de' => 'en']);
$translator->setLocaleMapping(['de' => 'de-CH']);
});
// Run the app
$app->run();
use Tobento\App\AppFactory;
use Tobento\Service\Translation\MissingTranslationHandlerInterface;
use Tobento\Service\Translation\MissingTranslationHandler;
use Psr\Log\LoggerInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app');
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
$app->booting();
$app->set(MissingTranslationHandlerInterface::class, function()) {
return new MissingTranslationHandler($logger); // LoggerInterface, any PSR-3 logger
});
// Run the app
$app->run();
use Tobento\Service\Migration\MigrationInterface;
use Tobento\Service\Migration\ActionsInterface;
use Tobento\Service\Migration\Actions;
use Tobento\Service\Migration\Action\FilesCopy;
use Tobento\Service\Migration\Action\FilesDelete;
use Tobento\Service\Dir\DirsInterface;
class TranslationFiles implements MigrationInterface
{
protected array $files;
/**
* Create a new TranslationFiles.
*
* @param DirsInterface $dirs
*/
public function __construct(
protected DirsInterface $dirs,
) {
$transDir = realpath(__DIR__.'/../../').'/resources/trans/';
$this->files = [
$this->dirs->get('trans').'en/' => [
$transDir.'en/en.json',
$transDir.'en/shop.json',
],
$this->dirs->get('trans').'de/' => [
$transDir.'de/de.json',
$transDir.'de/shop.json',
],
];
}
/**
* Return a description of the migration.
*
* @return string
*/
public function description(): string
{
return 'Translation files.';
}
/**
* Return the actions to be processed on install.
*
* @return ActionsInterface
*/
public function install(): ActionsInterface
{
return new Actions(
new FilesCopy($this->files),
);
}
/**
* Return the actions to be processed on uninstall.
*
* @return ActionsInterface
*/
public function uninstall(): ActionsInterface
{
return new Actions(
new FilesDelete($this->files),
);
}
}
use Tobento\App\Boot;
use Tobento\App\Migration\Boot\Migration;
class TranslationFilesMigration extends Boot
{
public const BOOT = [
// you may ensure the migration boot.
Migration::class,
];
public function boot(Migration $migration)
{
// Install migrations
$migration->install(TranslationFiles::class);
}
}
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app');
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
$app->boot(TranslationFilesMigration::class);
//...
// Run the app
$app->run();
use Tobento\App\AppFactory;
use Tobento\Service\Translation\TranslatorInterface;
use Tobento\Service\Translation\Resource;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app');
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
// using the app on method:
$app->on(TranslatorInterface::class, function(TranslatorInterface $translator) {
$translator->resources()->add(new Resource('*', 'de', [
'Hello World' => 'Hallo Welt',
]));
$translator->resources()->add(new Resource('shop', 'de', [
'Cart' => 'Warenkorb',
]));
});
// Run the app
$app->run();
use Tobento\App\AppFactory;
use Tobento\Service\Translation\TranslatorInterface;
use Tobento\Service\Translation\Resource;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app');
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
// using the app on method:
$app->on(TranslatorInterface::class, function(TranslatorInterface $translator) {
$translator->resources()->add(new Resource(
name: 'shop',
locale: 'de',
translations: ['Hello World' => 'Hallo Welt'],
priority: 200, // set a higher the default added
));
});
// Run the app
$app->run();
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app');
// Add trans directory with higher priority:
$this->app->dirs()->dir(
dir: $this->app->dir('app').'trans-custom/',
// do not use 'trans' as name for migration purposes
name: 'trans.custom',
group: 'trans',
// add higher priority as default trans dir:
priority: 300,
);
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
// Run the app
$app->run();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.