Skip to content

Commit

Permalink
Merge pull request #598 from phalcon/2.0.x
Browse files Browse the repository at this point in the history
2.0.13
  • Loading branch information
sergeyklay committed May 21, 2016
2 parents 3652e64 + ab0bbc5 commit 6559062
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ env:
- TEST_DB_NAME="incubator_tests"
- TEST_DB_CHARSET="utf8"
matrix:
- PHALCON_VERSION="2.0.13"
- PHALCON_VERSION="2.0.12"
- PHALCON_VERSION="2.0.11"
- PHALCON_VERSION="2.0.10"
Expand Down
31 changes: 24 additions & 7 deletions Library/Phalcon/Translate/Adapter/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class Database extends Base implements AdapterInterface, \ArrayAccess
*/
protected $stmtSelect;

/**
* Use ICU MessageFormatter to parse message
*
* @var boolean
*/
protected $useIcuMessageFormatter = false;

/**
* Class constructor.
*
Expand All @@ -63,6 +70,14 @@ public function __construct(array $options)
throw new Exception("Parameter 'language' is required");
}

if (isset($options['useIcuMessageFormatter'])) {
if (!class_exists('\MessageFormatter')) {
throw new Exception('"MessageFormatter" class is required');
}

$this->useIcuMessageFormatter = (boolean) $options['useIcuMessageFormatter'];
}

$this->stmtSelect = sprintf(
'SELECT value FROM %s WHERE language = :language AND key_name = :key_name',
$options['table']
Expand All @@ -85,19 +100,21 @@ public function __construct(array $options)
*/
public function query($translateKey, $placeholders = null)
{
$options = $this->options;

$options = $this->options;
$translation = $options['db']->fetchOne(
$this->stmtSelect,
Db::FETCH_ASSOC,
['language' => $options['language'], 'key_name' => $translateKey]
);
$value = empty($translation['value']) ? $translateKey : $translation['value'];

$value = empty($translation['value']) ? $translateKey : $translation['value'];

if (is_array($placeholders)) {
foreach ($placeholders as $k => $v) {
$value = str_replace('%' . $k . '%', $v, $value);
if (is_array($placeholders) && !empty($placeholders)) {
if (true === $this->useIcuMessageFormatter) {
$value = \MessageFormatter::formatMessage($options['language'], $value, $placeholders);
} else {
foreach ($placeholders as $placeHolderKey => $placeHolderValue) {
$value = str_replace('%' . $placeHolderKey . '%', $placeHolderValue, $value);
}
}
}

Expand Down
20 changes: 17 additions & 3 deletions Library/Phalcon/Translate/Adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ class IndexController extends \Phalcon\Mvc\Controller
protected function _getTranslation()
{
return new Phalcon\Translate\Adapter\Database([
'db' => $this->di->get('db'), // Here we're getting the database from DI
'table' => 'translations', // The table that is storing the translations
'language' => $this->request->getBestLanguage() // Now we're getting the best language for the user
'db' => $this->di->get('db'), // Here we're getting the database from DI
'table' => 'translations', // The table that is storing the translations
'language' => $this->request->getBestLanguage(), // Now we're getting the best language for the user
'useIcuMessageFormatter' => true, // Optional, if need formatting message using ICU MessageFormatter
]);
}

Expand All @@ -52,6 +53,11 @@ CREATE TABLE `translations` (
)
```

Optional create unique index
```sql
CREATE UNIQUE INDEX translations_language_key_name_unique_index ON translations (language, key_name);
```

The columns are self-described, but pay attention to `language` — it's a column that stores the language
that the user is using, that can be `en`, `en-us` or `en-US`.
Now it's your responsibility to decide which pattern you want to use.
Expand Down Expand Up @@ -92,6 +98,14 @@ Or, if you wish you can use [Volt][2]:
<h1>{{ expression._("IndexPage_Hello_World") }}</h1>
```

ICU MessageFormatter Example
```php
// Example plural message with key 'cats'
// Peter has {nbCats, plural, =0{no cat} =1{a cat} other{# cats}}

$this->_getTranslation()->_('cats', ['nbCats' => rand(0, 10)]);
```

## ResourceBundle

This adapter uses ResourceBundle as translation frontend.
Expand Down

0 comments on commit 6559062

Please sign in to comment.