Skip to content

Commit

Permalink
[ENH] refactor application translation base module
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Sep 22, 2024
1 parent eb7af87 commit 1430730
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
28 changes: 16 additions & 12 deletions config/function.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (c) 2023. Wepesi.
* Copyright (c) 2023-2024. Wepesi Dev Framework
*/

if (! function_exists('getSubDirectories')) {
Expand Down Expand Up @@ -132,15 +132,19 @@ function dumper($ex)
}
}

if (! function_exists('url')) {
if (! function_exists('tra')) {
/**
* get a formatted application url route path
* @param string $path
* translate your text
* @param string $text
* @param array|string|null $value
* @return string
*/
function url(string $path): string
function tra(string $text, array|string $value = null): string
{
return WEB_ROOT . ltrim($path, '/');
$default_language = $_ENV['LANG'] ?? \Wepesi\Core\Session::get('lang');
$i18n = new \Wepesi\Core\i18n($default_language);
$translate_value = !is_array($value) ? [$value] : $value;
return $i18n->translate($text, $translate_value);
}
}

Expand All @@ -151,7 +155,7 @@ function url(string $path): string
* @param bool $create if the file does not exist, create it
* @return bool
*/
function fileExists(string $filename, bool $create): bool
function fileExists(string $filename, bool $create= false): bool
{
if (! is_file($filename) && !file_exists($filename)){
if ($create) {
Expand All @@ -165,12 +169,12 @@ function fileExists(string $filename, bool $create): bool

if (! function_exists('directoryExists')) {
/**
* Validate if the file exists, and in some case create it
* @param string $filename Path to the file or directory. to check files on network shares.
* @param bool $create if the file does not exist, create it
* Validate if the directory exists, and in some case create it
* @param string $filename directory Path to check files on network shares.
* @param bool $create if the directory does not exist, create it
* @return bool
*/
function directoryExists(string $filename, bool $create): bool
function directoryExists(string $filename, bool $create = false): bool
{
if (! file_exists($filename)){
if ($create) {
Expand All @@ -180,4 +184,4 @@ function directoryExists(string $filename, bool $create): bool
}
return true;
}
}
}
4 changes: 4 additions & 0 deletions config/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function serverDomain(): object
* Project General built-in Functions
*/
require_once $ROOT_DIR . '/config/function.php';
/**
*
*/
require_once $ROOT_DIR . '/config/tra.php';

/**
* Project autoload register
Expand Down
28 changes: 28 additions & 0 deletions config/tra.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/
/**
* needs a description
* @param string $content
* @return mixed|string
*/function tr($content)
{
$args = func_get_args();
return tra($content, '', false, array_slice($args, 1));
}

/**
* translate your text
* @param string $message
* @param string|array $value
* @return string
*/
function tra(string $message, $value = null): string
{
$i18n = new \Wepesi\Core\i18n(\Wepesi\Core\Session::get('lang'));
$translate_value = !is_array($value) ? [$value] : $value;
return $i18n->translate($message, $translate_value);
}

function tra_js(){}

0 comments on commit 1430730

Please sign in to comment.