2017 © Pedro Peláez
 

library transliteration

image

that0n3guy/transliteration

  • Wednesday, October 26, 2016
  • by that0n3guy
  • Repository
  • 2 Watchers
  • 7 Stars
  • 19,474 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 8 Versions
  • 4 % Grown

The README.md

Transliteration - Laravel 4, 5, 6, 7, 8, 9 & 10 text cleaning Package

Transliteration provides one-way string transliteration (romanization) and cleans text by replacing unwanted characters., (*1)

...it takes Unicode text and tries to represent it in US-ASCII characters (universally displayable, unaccented characters) by attempting to transliterate the pronunciation expressed by the text in some other writing system to Roman letters., (*2)

This adapts the module from https://drupal.org/project/transliteration for use with Laravel., (*3)

Features

  • Transliterate text to US-ASCII characters

Why use this?

  • I use this for filename on uploads. See this image:

*, (*4)

Quick start

Install the package via Composer:, (*5)

composer require that0n3guy/transliteration

Depending on your version of Laravel, you should install a different version of the package., (*6)

Laravel Version Package Version
10.0 ^2.0
9.0 ^2.0
8.0 ^2.0
7.0 ^2.0
6.0 ^2.0
5.0 ^2.0
4.0 ^1.0

In your config/app.php add 'That0n3guy\Transliteration\TransliterationServiceProvider' to the end of the $providers array, (*7)

'providers' => array(

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'That0n3guy\Transliteration\TransliterationServiceProvider',

),

How to use

Simply call the Transliteration class:, (*8)

Route::get('/test', function(){
  echo Transliteration::clean_filename('test& ® is true');
});

This would return test_r_is_true, (*9)

Set a language

You can optionally set a Optional ISO 639 language code. Do it like so:, (*10)

Route::get('/test', function(){
  echo Transliteration::clean_filename('testing Japanese 日本語', 'jpn');
});

This would return testing_Japanese_Ri_Ben_Yu_., (*11)

How to use to rename file uploads (sanitize them)

This is an old example, but still relevant. It uses Cabinet which I don't really recommend using anymore since there are better options., (*12)

Add something like:, (*13)

// if using transliteration
if (class_exists( 'That0n3guy\Transliteration\Transliteration' )) {
  $file->fileSystemName = Transliteration::clean_filename($file->getClientOriginalName());  // You can see I am cleaning the filename
}

To your Upload controller. For example. I added it to my UploadController.php and my store() method looks like so:, (*14)

/**
 * 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 ) {
      ...

Example how to use with octobercms

  • Create a plugin, for this example I'll call it that0n3guy.drivers. You can see documentation here: https://octobercms.com/docs/console/scaffolding#scaffold-create-plugin, (*15)

  • Add a bootPackages method to your Plugin.php as per the instructions here (copy/paste it straight from that page): https://luketowers.ca/blog/how-to-use-laravel-packages-in-october-cms-plugins/, (*16)

  • Add that0n3guy/transliteration to your plugins composer.json file:, (*17)

    "require": {
        "that0n3guy/transliteration": "^2.0"
    }

Create a config file in your plugins config folder (create this folder) just like https://luketowers.ca/blog/how-to-use-laravel-packages-in-october-cms-plugins/. File structure example:, (*18)

that0n3guy
    drivers
        config
            config.php
        Plugin.php

The config file should contain:, (*19)

<?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;

Now you can use transliteration facade anywhere in your octobercms php code (like in the docs above)., (*20)

The Versions

26/10 2016

dev-master

9999999-dev

  Sources   Download

The Requires

 

by Peter O

26/10 2016

dev-readmeUpdate

dev-readmeUpdate

  Sources   Download

The Requires

 

by Peter O

23/12 2015

2.0.x-dev

2.0.9999999.9999999-dev

  Sources   Download

The Requires

 

by Peter O

23/12 2015

2.0.1

2.0.1.0

  Sources   Download

The Requires

 

by Peter O

27/10 2015

dev-readmeChanges

dev-readmeChanges

  Sources   Download

The Requires

 

by Peter O

12/02 2015

2.0.0

2.0.0.0

  Sources   Download

The Requires

 

by Peter O

26/06 2014

1.0.1

1.0.1.0

  Sources   Download

The Requires

 

by Peter O

14/04 2014

1.0.0

1.0.0.0

  Sources   Download

The Requires

 

by Peter O