Skip to content

Commit

Permalink
[FEAT][TRA] add tranlate class module
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Dec 29, 2021
1 parent 006f52f commit 3665bd1
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 42 deletions.
7 changes: 6 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion .idea/wepesi_validation.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
"Wepesi\\app\\":"src/"
}
},
"require": {}
"require": {
"php": ">=7.4"
}
}
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

require_once "./vendor/autoload.php";
include('./shared/global.php');
include('./test/index.php');
4 changes: 4 additions & 0 deletions lang/en/language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$language=[
// "hello"=>"hello"
];
10 changes: 10 additions & 0 deletions lang/fr/language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$language=[
"`%s` should have minimum of `%s` caracters"=>"`%s` doit avoir un minimum de `%s` caracteres",
"`%s` should have maximum of `%s` caracters"=>"`%s` doit avoir un maximum de `%s` caracteres",
"`%s` this should be an email"=>"`%s` doit etre un email",
"`%s` this should be a link(url)"=>"`%s` doit avoir un lien(url) ",
"`%s` should match %s"=>"`%s` doit correpondre a `%s`",
"`%s` is required"=>"`%s` est obligatoire",
"`%s` should be a string"=>"`%s` doit est une chaine de caractere",
];
Empty file.
20 changes: 0 additions & 20 deletions nbproject/project.properties

This file was deleted.

10 changes: 0 additions & 10 deletions nbproject/project.xml

This file was deleted.

5 changes: 5 additions & 0 deletions shared/global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
const LANG = "fr";

define('WEB_ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_NAME']));
define('ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']));
21 changes: 21 additions & 0 deletions src/Escape.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php


namespace Wepesi\app;

class i18n
{
static function translate(string $message,array $data=[]):string{
$file="./lang/".LANG."/language.php";
if(!is_file($file) && !file_exists($file)){
$file="./lang/en/language.php";
}
include($file);
$message_key=$message;
if(count($data)>0){
$key_value=!isset($language[$message])?null:$language[$message];
$message_key=$key_value!=null?vsprintf($key_value,$data):vsprintf($message,$data);
}
return $message_key;
}
}
14 changes: 7 additions & 7 deletions src/VString.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function min(int $rule_values=0){
if (strlen($this->string_value) < $min) {
$message=[
"type"=>"string.min",
"message"=> "`{$this->string_item}` should have minimum of `{$min}` caracters",
"message"=> i18n::translate("`%s` should have minimum of `%s` caracters",[$this->string_item,$min]),
"label"=>$this->string_item,
"limit"=>$min
];
Expand All @@ -62,7 +62,7 @@ function max(int $rule_values=1){
if (strlen($this->string_value) > $max) {
$message = [
"type" => "string.max",
"message" => "`{$this->string_item}` should have maximum of `{$max}` caracters",
"message" => i18n::translate("`%s` should have maximum of `%s` caracters",[$this->string_item,$max]),
"label" => $this->string_item,
"limit" => $max
];
Expand All @@ -78,7 +78,7 @@ function email(){
if (!filter_var($this->string_value, FILTER_VALIDATE_EMAIL)) {
$message = [
"type" => "string.email",
"message" => "`{$this->string_item}` this should be an email",
"message" => i18n::translate("`%s` this should be an email",[$this->string_item]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -93,7 +93,7 @@ function url(){
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->string_value)) {
$message = [
"type" => "string.url",
"message" => "`{$this->string_item}` this shoudl be a link(url)",
"message" => i18n::translate("`{$this->string_item}` this should be a link(url)"),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -110,7 +110,7 @@ function match(string $key_tomatch){
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])) {
$message = [
"type" => "string.match",
"message" => "`{$this->string_item}` should match {$key_tomatch}",
"message" => i18n::translate("`%s` should match %s",[$this->string_item,$key_tomatch]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -122,7 +122,7 @@ function required(){
if (empty($required_value)) {
$message = [
"type"=> "any.required",
"message" => "`{$this->string_item}` is required",
"message" => i18n::translate("`%s` is required",[$this->string_item]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -149,7 +149,7 @@ private function checkExist(string $itemKey=null){
}else if(!preg_match($regex,$this->source_data[$item_to_check]) || strlen(trim($this->source_data[$item_to_check]))==0){
$message=[
"type" => "string.unknow",
"message" => "`{$item_to_check}` shoud be a s tring",
"message" => i18n::translate("`%s` should be a string",[$item_to_check]),
"label" => $item_to_check,
];
$this->addError($message);
Expand Down
4 changes: 2 additions & 2 deletions test/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"date_created"=>"2021-05-23"
];
$valid=new Validate($source);
// include "./test/string.php";
include "./test/string.php";
// include "./test/number.php";
// include "./test/boolean.php";
include "./test/date.php";
// include "./test/date.php";

0 comments on commit 3665bd1

Please sign in to comment.