Skip to content

Commit

Permalink
Squashed 'src/i18n/' changes from 3081cf1..bf58b9d
Browse files Browse the repository at this point in the history
bf58b9d Upstream some components travis ci config (#161)
f0450aa 修复Json Validator会失效的BUG (#153)
303b1de optimization translate, do not throw exception (#138)
f4b7470  add coWrite(); use swoft/swoole-ide-helper (#83)

git-subtree-dir: src/i18n
git-subtree-split: bf58b9d520e5b0eddab11318d5b5c12f3070f208
  • Loading branch information
swoft-bot committed Oct 6, 2018
1 parent 8c279ef commit 0c8e5f0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ language: php
php:
- 7.0
- 7.1
- 7.2

install:
- wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz -O hiredis.tar.gz && mkdir -p hiredis && tar -xf hiredis.tar.gz -C hiredis --strip-components=1 && cd hiredis && sudo make -j$(nproc) && sudo make install && sudo ldconfig && cd ..
- pecl install -f swoole-2.0.12
- echo 'no' | pecl install -f redis
- wget https://github.com/swoole/swoole-src/archive/v4.0.2.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure --enable-async-redis && make -j$(nproc) && make install && cd -
- echo "extension = swoole.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

before_script:
- phpenv config-rm xdebug.ini
- composer update

script: composer test
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"repositories": [
{
"type": "composer",
"url": "https://packagist.phpcomposer.com"
"url": "https://packagist.laravel-china.org"
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/Helper/Functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
if (! function_exists('translate')) {

if (!function_exists('translate')) {
/**
* I18n translate
*
Expand Down
68 changes: 58 additions & 10 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class Translator
*/
public $languageDir = '@resources/languages/';

/**
* All loaded languages
*
* @var []string
*/
private $languages = [];

/**
* Translation messages
*
Expand Down Expand Up @@ -67,19 +74,26 @@ public function init()
protected function loadLanguages(string $sourcePath)
{
if ($this->loaded === false) {
$languages = [];
$iterator = new \RecursiveDirectoryIterator($sourcePath);
$files = new \RecursiveIteratorIterator($iterator);

foreach ($files as $file) {
// Only load php file
// TODO add .mo .po support
if (pathinfo($file, PATHINFO_EXTENSION) !== 'php') {
continue;
}

$messages = str_replace([$sourcePath, '.php'], '', $file);
list($language, $category) = explode('/', $messages);

$languages[$language] = 1;
$this->messages[$language][$category] = require $file;
}

$this->loaded = true;
$this->languages = \array_keys($languages);
}
}

Expand All @@ -95,18 +109,50 @@ protected function loadLanguages(string $sourcePath)
public function translate(string $key, array $params, string $locale = null): string
{
$realKey = $this->getRealKey($key, $locale);
if (!ArrayHelper::has($this->messages, $realKey)) {
$exceptionMessage = sprintf('Translate error, key %s does not exist', $realKey);
throw new \InvalidArgumentException($exceptionMessage);
}
$message = ArrayHelper::get($this->messages, $realKey);

// not exist, return key
if (!\is_string($message)) {
throw new \InvalidArgumentException(sprintf('Message type error, possibly incorrectly key'));
return $key;
}

// no params
if (!$params) {
return $message;
}

return $this->formatMessage($message, $params);
}

/**
* get message data by key
* @return mixed
*/
public function get(string $key, string $locale = null)
{
$realKey = $this->getRealKey($key, $locale);

return ArrayHelper::get($this->messages, $realKey);
}

/**
* get messages
* @return array
*/
public function getMessages(): array
{
return $this->messages;
}

/**
* get languages
* @return array
*/
public function getLanguages(): array
{
return $this->languages;
}

/**
* @param string $key
* @param string|null $locale
Expand All @@ -115,10 +161,11 @@ public function translate(string $key, array $params, string $locale = null): st
*/
private function getRealKey(string $key, string $locale = null): string
{
if ($locale === null) {
if (!$locale) {
$locale = $this->defaultLanguage;
}
if (strpos($key, '.') === false) {

if (\strpos($key, '.') === false) {
$key = implode([$this->defualtCategory, $key], '.');
}

Expand All @@ -134,8 +181,9 @@ private function getRealKey(string $key, string $locale = null): string
*/
private function formatMessage(string $message, array $params): string
{
$params = array_values($params);
array_unshift($params, $message);
return sprintf(...$params);
$params = \array_values($params);
\array_unshift($params, $message);

return \sprintf(...$params);
}
}

0 comments on commit 0c8e5f0

Please sign in to comment.