Files
safekat/ci4/app/Controllers/Language.php

58 lines
1.6 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
class Language extends BaseController
{
public function index()
{
$session = session();
$locale = $this->request->getLocale();
$session->remove('lang');
$session->set('lang', $locale);
$url = previous_url();
return redirect()->to($url);
}
// Function to get the translation of the language file from a AJAX request
public function getTranslation()
{
$translationFile = $this->request->getPost('translationFile');
$data = [];
if(is_array($translationFile)){
foreach($translationFile as $file){
$locale = $this->request->getPost('locale');
$path = "Language/{$locale}/$file.php";
$lang = require APPPATH.$path;
$data[$file] = $lang;
}
return json_encode($data);
}
else{
$locale = $this->request->getPost('locale');
$path = "Language/{$locale}/$translationFile.php";
$lang = require APPPATH.$path;
return json_encode($lang);
}
}
public function file(string $file)
{
$locale = $this->request->getLocale(); // es, en, fr…
$path = APPPATH."Language/{$locale}/{$file}.php";
if (! is_file($path)) {
return $this->response->setStatusCode(404);
}
/** @var array $lines */
$lines = require $path; // el array que devuelve tu lang-file
return $this->response->setJSON($lines); // Content-Type: application/json
}
}