diff --git a/docs/en/intl-formatter.md b/docs/en/intl-formatter.md index 19b002a..eb3d9a7 100644 --- a/docs/en/intl-formatter.md +++ b/docs/en/intl-formatter.md @@ -13,10 +13,6 @@ In case you use [`yiisoft/config`](http://github.com/yiisoft/config), you will g the following DI container configuration is necessary: ```php -translate('Test string: {str}', ['str' => 'string data'], 'moduleId ### Example of usage without `yiisoft/translator` package ```php - /** @var \Yiisoft\Translator\IntlMessageFormatter $formatter */ $pattern = 'Total {count, number} {count, plural, one{item} other{items}}.'; $params = ['count' => 1]; @@ -67,5 +62,5 @@ echo $formatter->format($pattern, $params, $locale); // output: Alexander is male and he loves Yii! ``` -To get a list of options available for locale you're using - see +To get a list of options available for locale you're using see [https://intl.rmcreative.ru/](https://intl.rmcreative.ru/) diff --git a/docs/en/simple-formatter.md b/docs/en/simple-formatter.md index 9ecece7..2a25d64 100644 --- a/docs/en/simple-formatter.md +++ b/docs/en/simple-formatter.md @@ -2,14 +2,10 @@ ## Configuration -In case of using [yiisoft/config](http://github.com/yiisoft/config), the configuration is added automatically. If not, +In case of using [yiisoft/config](http://github.com/yiisoft/config), the configuration is added automatically. If not, add the following mapping: ```php -translate'; + +$messages = $extractor->extract($defaultCategory, $translatorCall); +// Result is same as from `extract` function of ContentParser (see below). +``` + +## Adicionando arquivos com extensão diferente de `.php` ou ignorando alguns diretórios + +```php +$path = '/path/to/your/project'; +$only = ['**.php', '**.php7']; +$except = ['**/brokenSamples/*']; +$extractor = new \Yiisoft\Translator\Extractor\TranslationExtractor($path, $only, $except); +``` + +Para obter mais informações sobre os parâmetros `only` e `except` [veja yiisoft/files](https://github.com/yiisoft/files). + +## Obtendo uma lista de problemas ao extrair mensagens + +Caso você tenha parâmetros complicados, como retornos de chamada, constantes etc, o extrator pode pular algumas linhas: + +```php +/** @var \Yiisoft\Translator\Extractor\TranslationExtractor $extractor */ +$defaultCategory = 'defaultCategoryName'; +$messages = $extractor->extract($defaultCategory); + +if ($extractor->hasSkippedLines()) { + $skippedLines = $extractor->getSkippedLines(); + /** + * Will be returned array looks like: + * [ + * '/path/to/fileName' => [ + * [ + * int $numberOfLine, + * string $incorrectLine, + * ], + * ]; + */ +} +``` + +## Analisando conteúdo diretamente + +O extrator usa `ContentParser` internamente, que é aplicado a cada arquivo. Você pode querer aplicá-lo a um único arquivo +também: + +```php +/** + * Default category for messages without translator call category set. + * For example, $translator->translate('SimpleText'); + * Optional. By default this value equals empty string. + */ +$defaultCategory = 'defaultCategoryName'; +/** + * + * Translator method call signature. + * Optional. By default using default call signature `->translate`. + */ +$translatorCall = '::translate'; + +$parser = new \Yiisoft\Translator\Extractor\ContentParser($defaultCategory, $translatorCall); + +$fileContent = file_get_contents('some_file.php'); +$messages = $parser->extract($fileContent); +``` + +`$messages` conterá o seguinte array: + +```php +[ + 'defaultCategoryName' => [ + 'messageId1', + 'messageId2', + ], + 'categoryName' => [ + 'messageId3', + 'messageId4', + ], +] +``` diff --git a/docs/pt-BR/intl-formatter.md b/docs/pt-BR/intl-formatter.md new file mode 100644 index 0000000..597055e --- /dev/null +++ b/docs/pt-BR/intl-formatter.md @@ -0,0 +1,66 @@ +# Formatador `intl` + +O formatador de mensagens `intl` utiliza recursos de formatação de mensagens de extensão intl do PHP. + +## Requisitos + +- `intl` Extensão PHP 1.0.2 ou superior. +- Biblioteca `ICU` 49.0 ou superior. + +## Configuração + +Caso use [`yiisoft/config`](http://github.com/yiisoft/config), você obterá a configuração automaticamente. Se não, +a seguinte configuração do contêiner DI é necessária: + +```php +use Yiisoft\Translator\MessageFormatterInterface; +use Yiisoft\Translator\IntlMessageFormatter; + +return [ + MessageFormatterInterface::class => IntlMessageFormatter::class, +]; +``` + +## Uso geral + +### Exemplo de uso com `yiisoft/translator` + +```php +/** @var \Yiisoft\Translator\Translator $translator **/ + +$categoryName = 'moduleId'; +$pathToModuleTranslations = './module/messages/'; +$additionalCategorySource = new Yiisoft\Translator\CategorySource( + $categoryName, + new \Yiisoft\Translator\Message\Php\MessageSource($pathToModuleTranslations), + new \Yiisoft\Translator\IntlMessageFormatter() +); +$translator->addCategorySources($additionalCategorySource); + +$translator->translate('Test string: {str}', ['str' => 'string data'], 'moduleId', 'en'); +// output: Test string: string data +``` + +### Exemplo de uso sem o pacote `yiisoft/translator` + +```php +/** @var \Yiisoft\Translator\IntlMessageFormatter $formatter */ +$pattern = 'Total {count, number} {count, plural, one{item} other{items}}.'; +$params = ['count' => 1]; +$locale = 'en'; +echo $formatter->format($pattern, $params, $locale); +// output: Total 1 item. + +$pattern = '{gender, select, female{Уважаемая} other{Уважаемый}} {firstname}'; +$params = ['gender' => null, 'firstname' => 'Vadim']; +echo $formatter->format($pattern, $params, 'ru'); +// output: Уважаемый Vadim + +$pattern = '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'; +$params = ['name' => 'Alexander', 'gender' => 'male']; +echo $formatter->format($pattern, $params, $locale); +// output: Alexander is male and he loves Yii! +``` + +Para obter uma lista de opções disponíveis para a localidade que você está usando consulte +[https://intl.rmcreative.ru/](https://intl.rmcreative.ru/) diff --git a/docs/pt-BR/simple-formatter.md b/docs/pt-BR/simple-formatter.md new file mode 100644 index 0000000..2274668 --- /dev/null +++ b/docs/pt-BR/simple-formatter.md @@ -0,0 +1,49 @@ +# Formatador Simples + +## Configuração + +No caso de usar [yiisoft/config](http://github.com/yiisoft/config), a configuração é adicionada automaticamente. Se não, +adicione o seguinte mapeamento: + +```php +use Yiisoft\Translator\MessageFormatterInterface; +use Yiisoft\Translator\SimpleMessageFormatter; + +return [ + MessageFormatterInterface::class => SimpleMessageFormatter::class, +]; +``` + +## Usando com `Translator` + +```php +/** @var \Yiisoft\Translator\Translator $translator **/ + +$categoryName = 'moduleId'; +$pathToModuleTranslations = './module/messages/'; +$additionalCategory = new Yiisoft\Translator\CategorySource( + $categoryName, + new \Yiisoft\Translator\Message\Php\MessageSource($pathToModuleTranslations), + new \Yiisoft\Translator\SimpleMessageFormatter() +); +$translator->addCategorySources($additionalCategory); + +$translator->translate('Test string: {str}', ['str' => 'string data'], 'moduleId', 'en'); +// output: Test string: string data +``` + +## Usando sem `Translator` + +```php +/** @var \Yiisoft\Translator\SimpleMessageFormatter $formatter */ +$pattern = 'Test number: {number}'; +$params = ['number' => 5]; +$locale = 'en'; +echo $formatter->format($pattern, $params, $locale); +// output: Test number: 5 + +$pattern = 'Test string: {str}'; +$params = ['str' => 'string data']; +echo $formatter->format($pattern, $params, $locale); +// output: Test string: string data +```