diff --git a/.travis.yml b/.travis.yml index 9aa6ec2ef..652872484 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/composer.json b/composer.json index b0ea97f4f..576daa95b 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Helper/Functions.php b/src/Helper/Functions.php index 8e6dbb4a9..ab8bc62db 100644 --- a/src/Helper/Functions.php +++ b/src/Helper/Functions.php @@ -1,5 +1,6 @@ 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); } } @@ -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 @@ -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], '.'); } @@ -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); } }