that0n3guy / transliteration
Transliteration 提供单向字符串转写(罗马化)并通过替换不需要的字符来清洗文本。
Requires
- php: >=5.3.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
This package is not auto-updated.
Last update: 2024-09-16 15:44:28 UTC
README
Transliteration 提供单向字符串转写(罗马化)并通过替换不需要的字符来清洗文本。
...它接受 Unicode 文本并尝试通过将文本中某些其他书写系统表示的发音转写为罗马字母来以 US-ASCII 字符(通用可显示、无重音字符)表示它。
此模块是从 https://drupal.org/project/transliteration 修改为与 Laravel 一起使用的。
功能
- 将文本转写为 US-ASCII 字符
为什么要使用这个?
- 我使用它来上传文件名。请看这张图片
- 我使用它与 Andrew Elkins 的 Cabinet 一起
快速入门
通过 Composer 安装包
composer require that0n3guy/transliteration
根据您的 Laravel 版本,您应该安装不同版本的包。
在您的 config/app.php
中将 'That0n3guy\Transliteration\TransliterationServiceProvider'
添加到 $providers
数组的末尾
'providers' => array( 'Illuminate\Foundation\Providers\ArtisanServiceProvider', 'Illuminate\Auth\AuthServiceProvider', ... 'That0n3guy\Transliteration\TransliterationServiceProvider', ),
如何使用
只需调用 Transliteration 类
Route::get('/test', function(){ echo Transliteration::clean_filename('test& ® is true'); });
这将返回 test_r_is_true
设置语言
您可以可选地设置一个 可选的 ISO 639 语言代码。这样做
Route::get('/test', function(){ echo Transliteration::clean_filename('testing Japanese 日本語', 'jpn'); });
这将返回 testing_Japanese_Ri_Ben_Yu_
。
如何使用来重命名文件上传(清理它们)
这是一个旧例子,但仍相关。它使用 Cabinet,我现在不推荐使用,因为有更好的选择。
在您的上传控制器中添加类似的内容
// if using transliteration if (class_exists( 'That0n3guy\Transliteration\Transliteration' )) { $file->fileSystemName = Transliteration::clean_filename($file->getClientOriginalName()); // You can see I am cleaning the filename }
例如,我在我的 UploadController.php 中添加了它,我的 store() 方法看起来像这样
/** * Stores new upload * */ public function store() { $file = Input::file('file'); // if using transliteration if (class_exists( 'That0n3guy\Transliteration\Transliteration' )) { $file->fileSystemName = Transliteration::clean_filename($file->getClientOriginalName()); } $upload = new Upload; try { $upload->process($file); } catch(Exception $exception){ // Something went wrong. Log it. Log::error($exception); $error = array( 'name' => $file->getClientOriginalName(), 'size' => $file->getSize(), 'error' => $exception->getMessage(), ); // Return error return Response::json($error, 400); } // If it now has an id, it should have been successful. if ( $upload->id ) { ...
如何与 octobercms 一起使用示例
-
创建一个插件,例如,我将它命名为
that0n3guy.drivers
。您可以在以下位置查看文档:https://octobercms.com/docs/console/scaffolding#scaffold-create-plugin -
按照以下说明在您的
Plugin.php
中添加一个 bootPackages 方法(直接从该页面复制/粘贴):https://luketowers.ca/blog/how-to-use-laravel-packages-in-october-cms-plugins/ -
将
that0n3guy/transliteration
添加到您的插件 composer.json 文件
"require": {
"that0n3guy/transliteration": "^2.0"
}
在您的插件配置文件夹中创建一个配置文件(创建此文件夹)就像 https://luketowers.ca/blog/how-to-use-laravel-packages-in-october-cms-plugins/。文件结构示例
that0n3guy
drivers
config
config.php
Plugin.php
配置文件应包含
<?php
use Illuminate\Support\Facades\Config;
$config = [
// This contains the Laravel Packages that you want this plugin to utilize listed under their package identifiers
'packages' => [
'that0n3guy/transliteration' => [
'providers' => [
'\That0n3guy\Transliteration\TransliterationServiceProvider',
],
'aliases' => [
'Transliteration' => '\That0n3guy\Transliteration\Facades\Transliteration',
],
],
],
];
return $config;
现在您可以在您的 octobercms PHP 代码的任何地方使用转写外观(如上面的文档所示)。