Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] refactor application translation base module #189

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
bim-g marked this conversation as resolved.
Show resolved Hide resolved
$translate_value = !is_array($value) ? [$value] : $value;
return $i18n->translate($message, $translate_value);
}

function tra_js(){}
Loading