zvermafia/transliteration-laravel

Integration of the zvermafia/transliteration with Laravel

v0.1.3 2020-03-25 16:47 UTC

This package is auto-updated.

Last update: 2020-03-25 16:47:57 UTC


README

Latest Version on Packagist Software License Total Downloads

Integration of the zvermafia/transliteration with Laravel.

Navigation by sections

Install

Via Composer

$ composer require zvermafia/transliteration-laravel

Setup

There are two options for your choice, alif and lotin. With the option alif will be used the alif.uz's API. And with the option lotin will be used the lotin.uz's API.
Default option is alif. But if you want to change this to the lotin option then you simply need to add a TRANSLITERATOR key with the value lotin into the .env file.

TRANSLITERATOR=lotin

But if you want to extend this package with your own realization then you need to publish package's configuration file and update it with your option.

To publish the configuration file simply execute the php artisan vendor:publish command in the project root folder and select the Zvermafia\TransliterationLaravel\TransliteratorServiceProvider as a provider.

Usage

You can use a LaraTransliterator facade but I'll show you another way in the example below. Let's assume we have a PoorWorkerController with a translit method in which we want transliterate a requested text (with POST method) and return the result as JSON.

<?php

namespace App\Http\Controllers;

// use LaraTransliterator; // but we won't use the facade in this example
use Zvermafia\Transliteration\Interfaces\TransliteratorInterface;

class PoorWorkerController
{
    public function translit(Request $request, TransliteratorInterface $transliterator)
    {
        $result = $transliterator->setText($request->input('text'))
            ->toCyrillic()
            ->translit()
            ->getResult();

        return response()->json(compact('result'));
    }
}

For AJAX requests you can use a ready-made controller! Firstly you must define a route for the controller:

// in your routes file
Route::post('translit', '\Zvermafia\TransliterationLaravel\Controllers\TransliteratorController');

For AJAX request demonstration we'll use jQuery library:

$.ajax({
    url: '/translit',
    type: 'POST',
    data: {
        // _token: $('meta[name="csrf-token"]').attr('content'),
        text: 'Salom, dunyo!',
        to_cyrillic: 1
    },
    dataType: 'JSON'
})
.done(function (response) {
    if (response.code === 200) {
        alert(response.result);
    } else {
        alert('Something went wrong... Check the console for logs, please.');
        console.log(response);
    }
})
.fail(function (response) {
    console.log('Whoops, something went wrong... Check the response below:');
    console.log(response);
});
// A response will be like below:
{
    "code": 200,
    "text": "Salom, dunyo!",
    "to_cyrillic": 1,
    "result": "Салом, дунё!"
} 

// Or if an error occurs the response code will be different from 200
// and response will be like below:
{
    "code": 200,
    "message": "Transliterated text",
} 

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email mohirjon@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.