Skip to content

Commit 3665bd1

Browse files
committed
[FEAT][TRA] add tranlate class module
1 parent 006f52f commit 3665bd1

File tree

13 files changed

+65
-42
lines changed

13 files changed

+65
-42
lines changed

.idea/php.xml

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/wepesi_validation.iml

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@
1818
"Wepesi\\app\\":"src/"
1919
}
2020
},
21-
"require": {}
21+
"require": {
22+
"php": ">=7.4"
23+
}
2224
}

index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
22

33
require_once "./vendor/autoload.php";
4+
include('./shared/global.php');
45
include('./test/index.php');

lang/en/language.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
$language=[
3+
// "hello"=>"hello"
4+
];

lang/fr/language.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
$language=[
3+
"`%s` should have minimum of `%s` caracters"=>"`%s` doit avoir un minimum de `%s` caracteres",
4+
"`%s` should have maximum of `%s` caracters"=>"`%s` doit avoir un maximum de `%s` caracteres",
5+
"`%s` this should be an email"=>"`%s` doit etre un email",
6+
"`%s` this should be a link(url)"=>"`%s` doit avoir un lien(url) ",
7+
"`%s` should match %s"=>"`%s` doit correpondre a `%s`",
8+
"`%s` is required"=>"`%s` est obligatoire",
9+
"`%s` should be a string"=>"`%s` doit est une chaine de caractere",
10+
];

nbproject/configs/wepesi_validation.properties

Whitespace-only changes.

nbproject/project.properties

Lines changed: 0 additions & 20 deletions
This file was deleted.

nbproject/project.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

shared/global.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
const LANG = "fr";
3+
4+
define('WEB_ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_NAME']));
5+
define('ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']));

src/Escape.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace Wepesi\app;
5+
6+
class i18n
7+
{
8+
static function translate(string $message,array $data=[]):string{
9+
$file="./lang/".LANG."/language.php";
10+
if(!is_file($file) && !file_exists($file)){
11+
$file="./lang/en/language.php";
12+
}
13+
include($file);
14+
$message_key=$message;
15+
if(count($data)>0){
16+
$key_value=!isset($language[$message])?null:$language[$message];
17+
$message_key=$key_value!=null?vsprintf($key_value,$data):vsprintf($message,$data);
18+
}
19+
return $message_key;
20+
}
21+
}

src/VString.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function min(int $rule_values=0){
4343
if (strlen($this->string_value) < $min) {
4444
$message=[
4545
"type"=>"string.min",
46-
"message"=> "`{$this->string_item}` should have minimum of `{$min}` caracters",
46+
"message"=> i18n::translate("`%s` should have minimum of `%s` caracters",[$this->string_item,$min]),
4747
"label"=>$this->string_item,
4848
"limit"=>$min
4949
];
@@ -62,7 +62,7 @@ function max(int $rule_values=1){
6262
if (strlen($this->string_value) > $max) {
6363
$message = [
6464
"type" => "string.max",
65-
"message" => "`{$this->string_item}` should have maximum of `{$max}` caracters",
65+
"message" => i18n::translate("`%s` should have maximum of `%s` caracters",[$this->string_item,$max]),
6666
"label" => $this->string_item,
6767
"limit" => $max
6868
];
@@ -78,7 +78,7 @@ function email(){
7878
if (!filter_var($this->string_value, FILTER_VALIDATE_EMAIL)) {
7979
$message = [
8080
"type" => "string.email",
81-
"message" => "`{$this->string_item}` this should be an email",
81+
"message" => i18n::translate("`%s` this should be an email",[$this->string_item]),
8282
"label" => $this->string_item,
8383
];
8484
$this->addError($message);
@@ -93,7 +93,7 @@ function url(){
9393
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->string_value)) {
9494
$message = [
9595
"type" => "string.url",
96-
"message" => "`{$this->string_item}` this shoudl be a link(url)",
96+
"message" => i18n::translate("`{$this->string_item}` this should be a link(url)"),
9797
"label" => $this->string_item,
9898
];
9999
$this->addError($message);
@@ -110,7 +110,7 @@ function match(string $key_tomatch){
110110
if (isset($this->source_data[$key_tomatch]) && (strlen($this->string_value)!= strlen($this->source_data[$key_tomatch])) && ($this->string_value!=$this->source_data[$key_tomatch])) {
111111
$message = [
112112
"type" => "string.match",
113-
"message" => "`{$this->string_item}` should match {$key_tomatch}",
113+
"message" => i18n::translate("`%s` should match %s",[$this->string_item,$key_tomatch]),
114114
"label" => $this->string_item,
115115
];
116116
$this->addError($message);
@@ -122,7 +122,7 @@ function required(){
122122
if (empty($required_value)) {
123123
$message = [
124124
"type"=> "any.required",
125-
"message" => "`{$this->string_item}` is required",
125+
"message" => i18n::translate("`%s` is required",[$this->string_item]),
126126
"label" => $this->string_item,
127127
];
128128
$this->addError($message);
@@ -149,7 +149,7 @@ private function checkExist(string $itemKey=null){
149149
}else if(!preg_match($regex,$this->source_data[$item_to_check]) || strlen(trim($this->source_data[$item_to_check]))==0){
150150
$message=[
151151
"type" => "string.unknow",
152-
"message" => "`{$item_to_check}` shoud be a s tring",
152+
"message" => i18n::translate("`%s` should be a string",[$item_to_check]),
153153
"label" => $item_to_check,
154154
];
155155
$this->addError($message);

test/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"date_created"=>"2021-05-23"
1717
];
1818
$valid=new Validate($source);
19-
// include "./test/string.php";
19+
include "./test/string.php";
2020
// include "./test/number.php";
2121
// include "./test/boolean.php";
22-
include "./test/date.php";
22+
// include "./test/date.php";

0 commit comments

Comments
 (0)