diff --git a/composer.json b/composer.json index f70e86fc..104445f9 100755 --- a/composer.json +++ b/composer.json @@ -24,8 +24,6 @@ "ext-mbstring": "*", "ext-fileinfo": "*", - "flextype-components/i18n" : "1.3.0", - "glowy/session": "^3.0.0", "glowy/filesystem": "^3.0.0", "glowy/arrays": "^4.0.0", diff --git a/src/flextype/core/I18n.php b/src/flextype/core/I18n.php new file mode 100644 index 00000000..cfe0ec2b --- /dev/null +++ b/src/flextype/core/I18n.php @@ -0,0 +1,79 @@ + 'Login', 'auth_password' => 'Password']); + * + * New translation keys for `en_US` locale + * I18n::add(['auth_login' => 'Login', 'auth_password' => 'Password'], 'en_US'); + * + * @param string $translates Translation keys and values to add + * @param string $locale Locale + * @return void + */ + public static function add(array $translates, string $locale = null) : void + { + $locale = ($locale === null) ? I18n::$locale : $locale; + + if (isset(I18n::$dictionary[$locale])) { + I18n::$dictionary[$locale] += $translates; + } else { + I18n::$dictionary[$locale] = $translates; + } + } + + /** + * Returns translation of a string. If no translation exists, the original + * string will be returned. No parameters are replaced. + * + * Get translated string for `auth_login` for default locale + * $translated_string = I18n::find('auth_login'); + * + * @param string $translate Translate to find + * @param array $values Values to replace in the translated text + * @param string $locale Locale + * @return string + */ + public static function find(string $translate, array $values = [], string $locale = null) : string + { + $locale = ($locale === null) ? I18n::$locale : $locale; + + // Search current string to translate in the Dictionary + if (isset(I18n::$dictionary[$locale][$translate])) { + $translate = I18n::$dictionary[$locale][$translate]; + $translate = empty($values) ? $translate : strtr($translate, $values); + } else { + $translate = $translate; + } + + // Return translation of a string + return $translate; + } +} \ No newline at end of file diff --git a/src/flextype/helpers/helpers.php b/src/flextype/helpers/helpers.php index 441cf71b..91cb0441 100644 --- a/src/flextype/helpers/helpers.php +++ b/src/flextype/helpers/helpers.php @@ -2,6 +2,7 @@ declare(strict_types=1); +require_once __DIR__ . '/i18n.php'; require_once __DIR__ . '/services.php'; require_once __DIR__ . '/arrays.php'; require_once __DIR__ . '/finder.php'; diff --git a/src/flextype/helpers/i18n.php b/src/flextype/helpers/i18n.php new file mode 100644 index 00000000..51e8090e --- /dev/null +++ b/src/flextype/helpers/i18n.php @@ -0,0 +1,32 @@ + $username], 'en_US'); + * + * @param string $translate Translate to find + * @param array $values Values to replace in the translated text + * @param string $locale Locale + * @return string + */ + function __(string $translate, array $values = [], string $locale = null) : string + { + return I18n::find($translate, $values, $locale); + } +} \ No newline at end of file