Application Integration
In This Article
Stand-Alone
All filters, validators, view-helpers and the translator of zend-i18n can also be used stand-alone.
Translator
Setup
Create a file for the translation messages. For example languages/de_DE.php
:
return [
'car' => 'Auto',
'train' => 'Zug',
];
Create the translator instance and add the translation file.
$translator = new Zend\I18n\Translator\Translator();
$translator->addTranslationFile(
Zend\I18n\Translator\Loader\PhpArray::class,
__DIR__ . '/languages/de_DE.php',
'default',
'de_DE'
);
Translate Messages
$translator->setLocale('de_DE');
echo $translator->translate('car'); // Auto
echo $translator->translate('train'); // Zug
Filters
Each filter can be used directly.
$filter = new Zend\I18n\Filter\Alnum();
echo $filter->filter("This is (my) content: 123"); // "Thisismycontent123"
Using Filter Plugin Manager
Register all standard filters of zend-i18n in the filter-plugin-manager:
$filterManager = new Zend\Filter\FilterPluginManager(
new Zend\ServiceManager\ServiceManager()
);
$filterManager->configure(
(new Zend\I18n\ConfigProvider())->getFilterConfig()
);
Get a filter:
/** @var Zend\I18n\Filter\Alnum $filter */
$filter = $filterManager->get(Zend\I18n\Filter\Alnum::class);
Validators
Each validator can be used directly.
$validator = new Zend\I18n\Validator\Alnum();
if ($validator->isValid('Abcd12')) {
// value contains only allowed chars
} else {
// false
}
Using Validator Plugin Manager
Register all standard validators of zend-i18n in the validator-plugin-manager:
$validatorManager = new Zend\Validator\ValidatorPluginManager(
new Zend\ServiceManager\ServiceManager()
);
$validatorManager->configure(
(new Zend\I18n\ConfigProvider())->getValidatorConfig()
);
Get a validator:
/** @var Zend\I18n\Validator\Alnum $validator */
$validator = $validatorManager->get(Zend\I18n\Validator\Alnum::class);
View Helpers
Setup zend-view
Create the renderer:
$renderer = new Zend\View\Renderer\PhpRenderer();
Register all standard view-helpers of zend-i18n in the helper-plugin-manager:
$renderer->getHelperPluginManager()->configure(
(new Zend\I18n\ConfigProvider())->getViewHelperConfig()
);
Using Helper
echo $renderer->currencyFormat(1234.56, 'USD', null, 'en_US'); // "$1,234.56"
Found a mistake or want to contribute to the documentation? Edit this page on GitHub!