Skip to content

Commit

Permalink
Sync to overtrue/pinyin 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 22, 2016
1 parent bc6c76e commit 16960a3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 135 deletions.
85 changes: 12 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,7 @@ Chinese to Pinyin translator for Laravel5 / Lumen based on [overtrue/pinyin](htt
## Install

```shell
composer require "overtrue/laravel-pinyin:dev-master"
```

or add the following line to your project's `composer.json`:

```json
"require": {
"overtrue/laravel-pinyin": "dev-master"
}
```
then

```shell
composer update
composer require "overtrue/laravel-pinyin:~3.0"
```

## For Laravel
Expand All @@ -33,14 +20,6 @@ Overtrue\LaravelPinyin\ServiceProvider::class,
...
```

### config file

you can publish the config file to `config/pinyin.php`:

```php
php artisan vendor:publish --provider="Overtrue\LaravelPinyin\ServiceProvider" --tag="config"
```

## For Lumen

Add the following line to `bootstrap/app.php` after `// $app->withEloquent();`
Expand All @@ -53,73 +32,33 @@ $app->register(Overtrue\LaravelPinyin\ServiceProvider::class);
...
```

## Configuration

| .env | config/pinyin.php | default | description |
| --- | --- | --- | --- |
| `PINYIN_DELIMITER` | delimiter | `" "` | Symbol for stitching each pinyin. `'-' => dài-zhe-xī-wàng-qù-lǔ-xíng` |
| PINYIN_ACCENT | accent | `true` | Output with tone symbol. |
| PINYIN_ONLY_CHINESE | only_chinese | `true` | Leaving only the Chinese characters. |
| PINYIN_UPPERCASE | uppercase | `false` | Output uppercase(letter) |


## Usage

you can get the instance of `Overtrue\Pinyin\Pinyin` from app container:

```php

$pinyin = App::make('pinyin');
echo $pinyin->trans('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo
```

or use facade `Pinyin`:

add the following line to the section `aliases` of you project's `config/app.php` file:

```php
'Pinyin' => 'Overtrue\Pinyin\Pinyin',
```

then

```php
echo Pinyin::trans('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo

echo Pinyin::letter('带着希望去旅行,比到达终点更美好');
// d z x w q l x b d d z d g m h

echo Pinyin::parse('带着希望去旅行,比到达终点更美好');
// array(
// 'src' => '带着希望去旅行,比到达终点更美好',
// 'pinyin' => 'dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo',
// 'letter' => 'd z x w q l x b d d z d g m h',
// );
$pinyin = app('pinyin');
echo $pinyin->sentence('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǔ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo
```

There are more convenient functions:

| function | method |
| ------------- | --------------------------------------------------- |
| `pinyin()` | `Pinyin::trans()` |
| `letter()` | `Pinyin::letter()` |
| `pinyin_and_letter` | `Pinyin::parse()` |
| `pinyin()` | `app('pinyin')->convert()` |
| `pinyin_abbr()` | `app('pinyin')->abbr()` |
| `pinyin_permlink` | `app('pinyin')->permlink()` |
| `pinyin_sentence` | `app('pinyin')->sentence()` |

```php
echo pinyin('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo

echo letter('带着希望去旅行,比到达终点更美好');
// d z x w q l x b d d z d g m h
// ["dai", "zhe", "xi", "wang", "qu", "lu", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]

echo pinyin_and_letter('带着希望去旅行,比到达终点更美好');
// array(
// 'src' => '带着希望去旅行,比到达终点更美好',
// 'pinyin' => 'dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo',
// 'letter' => 'd z x w q l x b d d z d g m h',
// );
echo pinyin_abbr('带着希望去旅行');
// dzxwqlx
...
```

About `overtrue/pinyin` specific configuration and use, refer to: [overtrue/pinyin](https://github.com/overtrue/pinyin)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"overtrue"
],
"require": {
"overtrue/pinyin": ">=2.5.0"
"overtrue/pinyin": "~3.0"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 2 additions & 12 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ class ServiceProvider extends LaravelServiceProvider
*/
public function boot()
{
if (function_exists('config_path')) {
$this->publishes([
__DIR__ . '/config.php' => config_path('pinyin.php'),
], 'config');
}
//
}

/**
Expand All @@ -28,15 +24,9 @@ public function boot()
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/config.php', 'pinyin'
);

Pinyin::settings(config('pinyin', []));

$this->app->singleton('pinyin', function($app)
{
return Pinyin::getInstance();
return new Pinyin();
});
}
}
38 changes: 0 additions & 38 deletions src/config.php

This file was deleted.

39 changes: 28 additions & 11 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,64 @@
* Get the Pinyin of given string.
*
* @param string $string
* @param array $setting
* @param string $option
*
* @return string
*/
function pinyin($string, $setting = [])
function pinyin($string, $option = PINYIN_NONE)
{
return Pinyin::trans($string, $setting);
return app('pinyin')->convert($string, $option);
}
} else {
Log::warning('There exist multiple function "pinyin".');
}

if (! function_exists('letter')) {
if (! function_exists('pinyin_abbr')) {
/**
* Get the fist letters of given string.
*
* @param string $string
* @param array $setting
* @param string $delimiter
*
* @return string
*/
function letter($string, $setting = [])
function pinyin_abbr($string, $delimiter = '')
{
return Pinyin::letter($string, $setting);
return app('pinyin')->abbr($string, $delimiter);
}
} else {
Log::warning('There exist multiple function "letter".');
}

if (! function_exists('pinyin_and_letter')) {
if (! function_exists('pinyin_permlink')) {
/**
* Get the fist pinyin and letters of given string.
*
* @param string $string
* @param array $setting
* @param string $delimiter
*
* @return string
*/
function pinyin_and_letter($string, $setting = [])
function pinyin_permlink($string, $delimiter = '-')
{
return Pinyin::parse($string, $setting);
return app('pinyin')->permlink($string, $delimiter);
}
} else {
Log::warning('There exist multiple function "pinyin_and_letter".');
}

if (! function_exists('pinyin_sentence')) {
/**
* Get the fist pinyin and letters of given string.
*
* @param string $string
* @param string $tone
*
* @return string
*/
function pinyin_sentence($string, $tone = false)
{
return app('pinyin')->sentence($string, $tone);
}
} else {
Log::warning('There exist multiple function "pinyin_and_letter".');
Expand Down

0 comments on commit 16960a3

Please sign in to comment.