Skip to content

Instantly share code, notes, and snippets.

@ashour
Last active February 12, 2020 11:59
Show Gist options
  • Save ashour/b36a45a905248a88868dacf261983f8b to your computer and use it in GitHub Desktop.
Save ashour/b36a45a905248a88868dacf261983f8b to your computer and use it in GitHub Desktop.
<?php
class I18n
{
public const DEFAULT_LANG = 'en';
private static $allMessages;
private static $supportedLangs;
/**
* @return string code for current locale
*/
public static function lang()
{
return $_REQUEST['lang'] ?? static::DEFAULT_LANG;
}
/**
* @return 'rtl'|'ltr' layout direction of current locale
*/
public static function dir()
{
return static::lang() == 'ar' ? 'rtl' : 'ltr';
}
/**
* @return array all supported locales in the format `['en' => 'English', ...]`
*/
public static function supportedLangs()
{
if (static::$supportedLangs == null) {
require_once 'languages.php';
static::$supportedLangs = $languages;
}
return static::$supportedLangs;
}
/**
* Retrieve message translated for current locale
*
* @param string $key of message in translation messages file
* @param array $replacements key-value pairs for interpolation
* @return string
*/
public static function __($key, $replacements = [])
{
$message = static::messages()[$key] ?? $key;
if (count($replacements) > 0) {
foreach ($replacements as $key => $value) {
$message = str_replace('{' . $key . '}', $value, $message);
}
}
return $message;
}
/**
* @return bool whether a messages exists in the current locale with the
* given key
*/
public static function hasKey($key)
{
return isset(static::messages()[$key]);
}
/**
* @return array all messages for all locales
*/
private static function allMessages()
{
if (static::$allMessages == null) {
require_once 'messages.php';
static::$allMessages = $messages;
}
return static::$allMessages;
}
/**
* @return array messages for current locale
*/
private static function messages()
{
return static::allMessages()[static::lang()];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment