Skip to content

Commit

Permalink
885-multiple-language-support
Browse files Browse the repository at this point in the history
* [x] Completed translating vue files
* [x] Completed translating .php files inside /HTTP
* [x] Completed translating .blade.php files
* [x] Updated /laravel-form-builder files to reflect translations
* [x] Fixed hover text, helptext and placeholder not translating after json->php mapping
* [x] Added helper function hot_swap() to translate keys from mapped json file
* [x] added add_additional to /lang/en/buttons.
* [x] Fixed XSS attack
  • Loading branch information
PG-Momik committed Jan 12, 2023
1 parent 9f95dce commit e112260
Show file tree
Hide file tree
Showing 320 changed files with 14,358 additions and 9,830 deletions.
132 changes: 113 additions & 19 deletions app/Helpers/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare(strict_types=1);

use App\IATI\Models\User\Role;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
Expand Down Expand Up @@ -367,7 +369,10 @@ function getCodeList($listName, $listType, bool $code = true): array
$data = [];

foreach ($codeList as $list) {
$data[$list['code']] = ($code) ? $list['code'] . (array_key_exists('name', $list) ? ' - ' . $list['name'] : '') : $list['name'];
$data[$list['code']] = ($code) ? $list['code'] . (array_key_exists(
'name',
$list
) ? ' - ' . $list['name'] : '') : $list['name'];
}

return $data;
Expand Down Expand Up @@ -400,12 +405,12 @@ function getCodeListArray($listName, $listType, bool $code = true): array

if (!function_exists('customDecryptString')) {
/**
* @param string $encryptedString
* @param string $encryptedString
* @param string|null $key
*
* @return bool|string|null
*/
function customDecryptString(string $encryptedString, string $key = null): bool|string|null
function customDecryptString(string $encryptedString, string $key = null): bool | string | null
{
$frontendEncryptor = new Encrypter($key ?? env('MIX_ENCRYPTION_KEY'), Config::get('app.cipher'));

Expand All @@ -415,12 +420,12 @@ function customDecryptString(string $encryptedString, string $key = null): bool|

if (!function_exists('customEncryptString')) {
/**
* @param string $string
* @param string $string
* @param string|null $key
*
* @return bool|string|null
*/
function customEncryptString(string $string, string $key = null): bool|string|null
function customEncryptString(string $string, string $key = null): bool | string | null
{
$frontendEncryptor = new Encrypter($key ?? env('MIX_ENCRYPTION_KEY'), Config::get('app.cipher'));

Expand All @@ -437,15 +442,26 @@ function customEncryptString(string $string, string $key = null): bool|string|nu
* @return bool|string|null
* @throws Exception
*/
function encryptString(string $string): bool|string|null
function encryptString(string $string): bool | string | null
{
$iv = random_bytes(16);
$salt = random_bytes(256);
$iterations = 999;
$encryptMethodLength = 256 / 4;
$hashKey = hash_pbkdf2('sha512', env('MIX_ENCRYPTION_KEY'), $salt, $iterations, $encryptMethodLength);
$hashKey = hash_pbkdf2(
'sha512',
env('MIX_ENCRYPTION_KEY'),
$salt,
$iterations,
$encryptMethodLength
);
$encryptedData = openssl_encrypt($string, 'AES-256-CBC', hex2bin($hashKey), OPENSSL_RAW_DATA, $iv);
$output = ['ciphertext' => base64_encode($encryptedData), 'iv' => bin2hex($iv), 'salt' => bin2hex($salt), 'iterations' => $iterations];
$output = [
'ciphertext' => base64_encode($encryptedData),
'iv' => bin2hex($iv),
'salt' => bin2hex($salt),
'iterations' => $iterations,
];

return base64_encode(json_encode($output, JSON_THROW_ON_ERROR));
}
Expand All @@ -461,7 +477,7 @@ function encryptString(string $string): bool|string|null
* @return bool|string|null
* @throws JsonException
*/
function decryptString(string $encryptedString, string $key): bool|string|null
function decryptString(string $encryptedString, string $key): bool | string | null
{
$json = json_decode(base64_decode($encryptedString), true, 512, JSON_THROW_ON_ERROR);

Expand Down Expand Up @@ -495,7 +511,7 @@ function decryptString(string $encryptedString, string $key): bool|string|null
* Return codeList array from json codeList.
*
* @param string $filePath
* @param bool $code
* @param bool $code
*
* @return array
* @throws JsonException
Expand Down Expand Up @@ -617,7 +633,9 @@ function getPeriodTypes(): array
*/
function generateToastData(): array
{
$toast['message'] = Session::exists('error') ? Session::get('error') : (Session::exists('success') ? Session::get('success') : '');
$toast['message'] = Session::exists('error') ? Session::get('error') : (Session::exists(
'success'
) ? Session::get('success') : '');
$toast['type'] = Session::exists('error') ? false : 'success';
Session::forget('success');
Session::forget('error');
Expand Down Expand Up @@ -670,7 +688,10 @@ function getTableConfig($module): array
{
$tableConfig = [
'activity' => ['orderBy' => ['updated_at'], 'direction' => ['asc', 'desc']],
'organisation' => ['orderBy' => ['updated_at', 'all_activities_count', 'name'], 'direction' => ['asc', 'desc']],
'organisation' => [
'orderBy' => ['updated_at', 'all_activities_count', 'name'],
'direction' => ['asc', 'desc'],
],
];

return $tableConfig[$module];
Expand All @@ -687,7 +708,12 @@ function getTableConfig($module): array
*/
function getDefaultLanguage($defaultValues): string
{
if (!empty($defaultValues) && array_key_exists('default_language', $defaultValues) && !empty($defaultValues['default_language'])) {
if (
!empty($defaultValues) && array_key_exists(
'default_language',
$defaultValues
) && !empty($defaultValues['default_language'])
) {
return $defaultValues['default_language'];
}

Expand Down Expand Up @@ -726,7 +752,23 @@ function ($value) {
*/
function getNonArrayElements(): array
{
return ['activity_status', 'activity_scope', 'default_flow_type', 'default_finance_type', 'default_tied_status', 'capital_spend', 'collaboration_type', 'identifier', 'org_id', 'default_field_values', 'updated_at', 'created_at', 'id', 'iati_identifier', 'element_status'];
return [
'activity_status',
'activity_scope',
'default_flow_type',
'default_finance_type',
'default_tied_status',
'capital_spend',
'collaboration_type',
'identifier',
'org_id',
'default_field_values',
'updated_at',
'created_at',
'id',
'iati_identifier',
'element_status',
];
}
}

Expand Down Expand Up @@ -781,7 +823,7 @@ function getEncodingType($file): string
}

return 'UTF-8';
} catch (\Exception $exception) {
} catch (Exception $exception) {
return 'UTF-8';
}
}
Expand Down Expand Up @@ -816,7 +858,7 @@ function trimInput($input): string
*
* @return false|string
*/
function dateFormat($format, $date): bool|string
function dateFormat($format, $date): bool | string
{
if (is_array($date) || is_bool($date)) {
return false;
Expand All @@ -829,7 +871,11 @@ function dateFormat($format, $date): bool|string

$dateArray = date_parse_from_format('Y-m-d', $date);

if (checkdate((int) $dateArray['month'], (int) $dateArray['day'], (int) $dateArray['year']) && (bool) strtotime($date)) {
if (
checkdate((int) $dateArray['month'], (int) $dateArray['day'], (int) $dateArray['year']) && (bool) strtotime(
$date
)
) {
return date($format, strtotime($date));
}

Expand All @@ -846,7 +892,7 @@ function dateFormat($format, $date): bool|string
*
* @return false|int
*/
function dateStrToTime($date): int|bool
function dateStrToTime($date): int | bool
{
if (is_array($date)) {
return false;
Expand All @@ -859,7 +905,11 @@ function dateStrToTime($date): int|bool

$dateArray = date_parse_from_format('Y-m-d', $date);

if (checkdate((int) $dateArray['month'], (int) $dateArray['day'], (int) $dateArray['year']) && (bool) strtotime($date)) {
if (
checkdate((int) $dateArray['month'], (int) $dateArray['day'], (int) $dateArray['year']) && (bool) strtotime(
$date
)
) {
return strtotime($date);
}

Expand Down Expand Up @@ -1066,3 +1116,47 @@ function is_variable_null($var): bool
return is_array($var) ? is_array_values_null($var) : is_null($var);
}
}

if (!function_exists('translate_this')) {
/**
* @param string $prefix_key
* @param string $suffix_key
*
* @return array|string|Translator|Application|null
*/
function translate_this(string $prefix_key, string $suffix_key = ''): array | string | Translator | Application | null
{
if (empty($suffix_key)) {
return trans('requests.' . $prefix_key);
}

return trans('requests.' . $prefix_key, ['suffix' => trans('requests.suffix.' . $suffix_key)]);
}
}

if (!function_exists('hot_swap')) {
/**
* @param $elements
*
* @return array|mixed
*/
function hot_swap($elements)
{
$swapable_keys = ['label', 'placeholder', 'hover_text', 'help_text'];
$result = [];
if (is_array($elements)) {
foreach ($elements as $key => $value) {
if (is_string($value) && in_array($key, $swapable_keys)) {
$translated = trans($value);
$result[$key] = $translated;
} else {
$result[$key] = hot_swap($value);
}
}
} else {
$result = $elements;
}

return $result;
}
}
Loading

0 comments on commit e112260

Please sign in to comment.