Skip to content

Commit b2a997e

Browse files
committed
0.1.2
0 parents  commit b2a997e

File tree

4 files changed

+570
-0
lines changed

4 files changed

+570
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Grupo de PHP da Zona da Mata
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "phpzm/message",
3+
"description": "Package message",
4+
"minimum-stability": "dev",
5+
"keywords": [
6+
"php",
7+
"framework",
8+
"api",
9+
"simples"
10+
],
11+
"homepage": "https://github.com/phpzm/message",
12+
"license": "MIT",
13+
"version": "0.1.2",
14+
"type": "package",
15+
"authors": [
16+
{"name": "William", "email": "[email protected]"},
17+
{"name": "Ezio", "email": "[email protected]"}
18+
],
19+
"require": {
20+
"php": ">=7.0",
21+
"phpmailer/phpmailer": "^5.2"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Simples\\Message\\": "src/"
26+
}
27+
}
28+
}

src/Message/Lang.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Simples\Message;
4+
5+
use Simples\Core\Helper\File;
6+
use Simples\Core\Kernel\App;
7+
8+
/**
9+
* @method static string validation($i18, array $parameters = [])
10+
* @method static string auth($i18, array $parameters = [])
11+
*
12+
* Class Lang
13+
* @package Simples\Core\Kernel
14+
*/
15+
abstract class Lang
16+
{
17+
/**
18+
* @param $default
19+
* @param string $fallback
20+
*/
21+
public static function locale($default, $fallback = '')
22+
{
23+
App::options('lang', ['default' => $default, 'fallback' => $fallback]);
24+
}
25+
26+
/**
27+
* @param $name
28+
* @param $arguments
29+
* @return string
30+
*/
31+
public static function __callStatic($name, $arguments)
32+
{
33+
if (isset($arguments[1])) {
34+
return self::lang($name, $arguments[0], $arguments[1]);
35+
}
36+
return self::lang($name, $arguments[0]);
37+
}
38+
39+
40+
/**
41+
* @param $scope
42+
* @param $path
43+
* @param array $parameters
44+
* @return string
45+
*/
46+
public static function lang($scope, $path, array $parameters = [])
47+
{
48+
$i18n = "Lang '{$scope}.{$path}' not found";
49+
$languages = App::options('lang');
50+
$filename = static::filename($scope, $languages['default'], $languages['fallback']);
51+
52+
if ($filename) {
53+
/** @noinspection PhpIncludeInspection */
54+
$phrases = include $filename;
55+
56+
$i18n = search($phrases, $path);
57+
if (gettype($i18n) === TYPE_STRING) {
58+
return self::replace($i18n, $parameters);
59+
}
60+
}
61+
return $i18n;
62+
}
63+
64+
/**
65+
* @param $i18n
66+
* @param $parameters
67+
* @return mixed
68+
*/
69+
public static function replace($i18n, $parameters)
70+
{
71+
foreach ($parameters as $key => $value) {
72+
$i18n = str_replace('{' . $key . '}', parse($value), $i18n);
73+
}
74+
return $i18n;
75+
}
76+
77+
/**
78+
* @param string $scope
79+
* @param string $default
80+
* @param string $fallback
81+
* @return string
82+
*/
83+
private static function filename(string $scope, string $default, string $fallback): string
84+
{
85+
$filename = resources("locales/{$default}/{$scope}.php");
86+
if (!File::exists($filename)) {
87+
$filename = resources("locales/{$fallback}/{$scope}.php");
88+
}
89+
if (File::exists($filename)) {
90+
return $filename;
91+
}
92+
return '';
93+
}
94+
}

0 commit comments

Comments
 (0)