first
This commit is contained in:
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Localization;
|
||||
|
||||
abstract class AbstractLocalization {
|
||||
|
||||
public function getLocale() {
|
||||
return 'en_US';
|
||||
}
|
||||
|
||||
abstract public function createMo();
|
||||
|
||||
abstract public function createNOOP_Translations();
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Nextend\Framework\Localization\Localization;
|
||||
|
||||
function n2_($text, $domain = 'nextend', $escape = true) {
|
||||
$translations = Localization::getTranslationsForDomain($domain);
|
||||
|
||||
if ($escape) {
|
||||
return esc_html($translations->translate($text));
|
||||
} else {
|
||||
return $translations->translate($text);
|
||||
}
|
||||
}
|
||||
|
||||
function n2_e($text, $domain = 'nextend') {
|
||||
echo esc_html(n2_($text, $domain, false));
|
||||
}
|
||||
|
||||
function n2_n($single, $plural, $number, $domain = 'nextend') {
|
||||
$translations = Localization::getTranslationsForDomain($domain);
|
||||
|
||||
return $translations->translate_plural($single, $plural, $number);
|
||||
}
|
||||
|
||||
function n2_en($single, $plural, $number, $domain = 'nextend') {
|
||||
echo esc_html(n2_n($single, $plural, $number, $domain));
|
||||
}
|
||||
|
||||
function n2_x($text, $context, $domain = 'nextend') {
|
||||
$translations = Localization::getTranslationsForDomain($domain);
|
||||
|
||||
return $translations->translate($text, $context);
|
||||
}
|
||||
|
||||
function n2_ex($text, $context, $domain = 'nextend') {
|
||||
echo esc_html(n2_x($text, $context, $domain));
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Localization;
|
||||
|
||||
use Nextend\Framework\Filesystem\Filesystem;
|
||||
use Nextend\Framework\Localization\Joomla\JoomlaLocalization;
|
||||
use Nextend\Framework\Localization\WordPress\WordPressLocalization;
|
||||
use Nextend\Framework\Pattern\SingletonTrait;
|
||||
use Nextend\Framework\Platform\Platform;
|
||||
use Nextend\Framework\Settings;
|
||||
|
||||
class Localization {
|
||||
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var AbstractLocalization
|
||||
*/
|
||||
private static $platformLocalization;
|
||||
|
||||
private static $l10n = array();
|
||||
|
||||
private static $js = array();
|
||||
|
||||
protected function init() {
|
||||
|
||||
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Functions.php';
|
||||
self::$platformLocalization = new WordPressLocalization();
|
||||
}
|
||||
|
||||
public static function getLocale() {
|
||||
return self::$platformLocalization->getLocale();
|
||||
}
|
||||
|
||||
private static function checkMoFile($path, $locale) {
|
||||
if (Filesystem::fileexists($path . '/' . $locale . '.mo')) return $locale . '.mo';
|
||||
|
||||
if (strpos($locale, '_')) {
|
||||
$nextLangStep = implode('_', explode('_', $locale, -1));
|
||||
|
||||
return self::checkMoFile($path, $nextLangStep);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
private static function loadTextDomain($domain, $mofile) {
|
||||
|
||||
$mo = self::$platformLocalization->createMo();
|
||||
if (!$mo->import_from_file($mofile)) return false;
|
||||
|
||||
if (isset(self::$l10n[$domain])) $mo->merge_with(self::$l10n[$domain]);
|
||||
self::$l10n[$domain] = &$mo;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function loadPluginTextDomain($path, $domain = 'nextend') {
|
||||
if (Platform::isAdmin() && Settings::get('force-english-backend')) {
|
||||
$locale = 'en_EN';
|
||||
} else {
|
||||
$locale = self::getLocale();
|
||||
}
|
||||
$mofile = self::checkMoFile($path, $locale);
|
||||
|
||||
if ($mofile && $loaded = self::loadTextDomain($domain, $path . '/' . $mofile)) {
|
||||
return $loaded;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getTranslationsForDomain($domain) {
|
||||
if (!isset(self::$l10n[$domain])) {
|
||||
self::$l10n[$domain] = self::$platformLocalization->createNOOP_Translations();
|
||||
}
|
||||
|
||||
return self::$l10n[$domain];
|
||||
}
|
||||
|
||||
public static function addJS($texts) {
|
||||
foreach ((array)$texts as $text) {
|
||||
self::$js[$text] = n2_($text);
|
||||
}
|
||||
}
|
||||
|
||||
public static function toJS() {
|
||||
if (count(self::$js)) {
|
||||
return '_N2._localization = ' . json_encode(self::$js) . ';';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Localization\WordPress;
|
||||
|
||||
use Mo;
|
||||
use Nextend\Framework\Localization\AbstractLocalization;
|
||||
use NOOP_Translations;
|
||||
use function get_locale;
|
||||
use function get_user_locale;
|
||||
use function is_admin;
|
||||
|
||||
class WordPressLocalization extends AbstractLocalization {
|
||||
|
||||
|
||||
public function getLocale() {
|
||||
|
||||
return is_admin() && function_exists('\\get_user_locale') ? get_user_locale() : get_locale();
|
||||
}
|
||||
|
||||
public function createMo() {
|
||||
|
||||
require_once ABSPATH . WPINC . '/pomo/mo.php';
|
||||
|
||||
return new MO();
|
||||
}
|
||||
|
||||
public function createNOOP_Translations() {
|
||||
|
||||
require_once ABSPATH . WPINC . '/pomo/mo.php';
|
||||
|
||||
return new NOOP_Translations();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user