create translation handler
This commit is contained in:
59
app/Http/Controllers/TranslationController.php
Normal file
59
app/Http/Controllers/TranslationController.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
class TranslationController extends Controller
|
||||
{
|
||||
/**
|
||||
* @param string $locale
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function get(string $locale = 'id')
|
||||
{
|
||||
app()->setLocale($locale);
|
||||
|
||||
return $this->all() ?? '{}';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$path = lang_path(app()->getLocale() . '.json');
|
||||
|
||||
if (File::exists($path)) {
|
||||
return json_decode(File::get($path), true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param string $locale
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function register(Request $request, string $locale = 'id')
|
||||
{
|
||||
$request->validate([
|
||||
'text' => 'required|string',
|
||||
]);
|
||||
|
||||
app()->setLocale($locale);
|
||||
|
||||
$all = $this->all() ?? [];
|
||||
|
||||
if (!array_key_exists($request->text, $all)) {
|
||||
$all[$request->text] = $request->text;
|
||||
}
|
||||
|
||||
$path = lang_path($locale . '.json');
|
||||
|
||||
return File::put($path, json_encode(
|
||||
$all, JSON_PRETTY_PRINT
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -34,8 +34,31 @@ const can = (abilities) => {
|
||||
}
|
||||
}
|
||||
|
||||
window.translations = {}
|
||||
window.locale = localStorage.getItem('locale') || 'id'
|
||||
|
||||
const translation = () => axios.get(route('api.translation.get', window.locale))
|
||||
.then(response => response.data)
|
||||
.then(response => window.translations = response)
|
||||
|
||||
translation()
|
||||
|
||||
window.can = can
|
||||
window.__ = (text, replacements = {}) => {
|
||||
if (typeof text === 'string') {
|
||||
if (window.translations.hasOwnProperty(text)) {
|
||||
text = window.translations[text]
|
||||
} else {
|
||||
axios.post(route(
|
||||
'api.translation.register', window.locale
|
||||
), { text }).then(translation)
|
||||
}
|
||||
|
||||
for (const key in replacements) {
|
||||
text = text.replace(new RegExp(`:${key}`, 'g'), replacements[key])
|
||||
}
|
||||
}
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
|
||||
@@ -28,3 +28,8 @@ Route::prefix('/v1')->name('api.v1.')->middleware(['auth:sanctum'])->group(funct
|
||||
|
||||
Route::get('/user', fn () => request()->user());
|
||||
});
|
||||
|
||||
Route::prefix('/translation/{locale?}')->name('api.translation.')->controller(App\Http\Controllers\TranslationController::class)->group(function () {
|
||||
Route::get('/', 'get')->name('get');
|
||||
Route::post('/', 'register')->name('register');
|
||||
});
|
||||
Reference in New Issue
Block a user