158 linhas
6,1 KiB
PHP
158 linhas
6,1 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Languages: the code is written in English and the translations live in /languages (gettext .po and .mo files).
|
|
*
|
|
* How the language is chosen:
|
|
* - Settings "Language" = English: the plugin texts stay in English, whatever the WordPress language.
|
|
* - Settings "Language" = Português: the plugin texts are in Portuguese, whatever the WordPress language.
|
|
* - Settings "Language" = Same as WordPress (default): WordPress loads the .mo of its language as usual
|
|
* (wp-content/languages/plugins first, then this plugin's /languages). Any pt_* language without its
|
|
* own file uses the European Portuguese file.
|
|
* Messages sent to customers use the language of each customer (see INTERWEB_MZ_DOMAINS_I18n::in()).
|
|
*/
|
|
class INTERWEB_MZ_DOMAINS_I18n
|
|
{
|
|
/** Portuguese catalogues already read (MO objects), by file. */
|
|
private static $catalogues = array();
|
|
|
|
public static function init()
|
|
{
|
|
add_action('init', array(__CLASS__, 'load'), 1);
|
|
add_filter('gettext_' . INTERWEB_MZ_DOMAINS_SLUG, array(__CLASS__, 'translate'), 10, 3);
|
|
add_filter('ngettext_' . INTERWEB_MZ_DOMAINS_SLUG, array(__CLASS__, 'translate_plural'), 10, 5);
|
|
add_filter('gettext_with_context_' . INTERWEB_MZ_DOMAINS_SLUG, array(__CLASS__, 'translate_context'), 10, 4);
|
|
add_filter('ngettext_with_context_' . INTERWEB_MZ_DOMAINS_SLUG, array(__CLASS__, 'translate_plural_context'), 10, 6);
|
|
}
|
|
|
|
public static function load()
|
|
{
|
|
load_plugin_textdomain(INTERWEB_MZ_DOMAINS_SLUG, false, dirname(plugin_basename(INTERWEB_MZ_DOMAINS_FILE)) . '/languages');
|
|
}
|
|
|
|
/** "auto", "pt" or "en", as saved in the settings. */
|
|
private static function choice()
|
|
{
|
|
return class_exists('INTERWEB_MZ_DOMAINS_Settings') ? INTERWEB_MZ_DOMAINS_Settings::get('ui_lang') : 'auto';
|
|
}
|
|
|
|
private static function reader_locale()
|
|
{
|
|
return function_exists('determine_locale') ? determine_locale() : get_locale();
|
|
}
|
|
|
|
/** "pt" or "en", for the person who is reading now (admin user or visitor). */
|
|
public static function lang()
|
|
{
|
|
$choice = self::choice();
|
|
if ($choice === 'pt' || $choice === 'en') {
|
|
return $choice;
|
|
}
|
|
return self::lang_of_locale(self::reader_locale());
|
|
}
|
|
|
|
public static function lang_of_locale($locale)
|
|
{
|
|
return (strpos((string) $locale, 'pt') === 0) ? 'pt' : 'en';
|
|
}
|
|
|
|
/**
|
|
* The Portuguese catalogue for a locale: the file of that pt_* locale when there is one
|
|
* (wp-content/languages/plugins first), otherwise this plugin's pt_PT file.
|
|
*
|
|
* @return MO|null
|
|
*/
|
|
private static function catalogue($locale)
|
|
{
|
|
$locale = preg_replace('/[^A-Za-z0-9_]/', '', (string) $locale);
|
|
$files = array();
|
|
if ($locale !== '' && self::lang_of_locale($locale) === 'pt') {
|
|
if (defined('WP_LANG_DIR')) {
|
|
$files[] = WP_LANG_DIR . '/plugins/' . INTERWEB_MZ_DOMAINS_SLUG . '-' . $locale . '.mo';
|
|
}
|
|
$files[] = INTERWEB_MZ_DOMAINS_DIR . 'languages/' . INTERWEB_MZ_DOMAINS_SLUG . '-' . $locale . '.mo';
|
|
}
|
|
$files[] = INTERWEB_MZ_DOMAINS_DIR . 'languages/' . INTERWEB_MZ_DOMAINS_SLUG . '-pt_PT.mo';
|
|
|
|
foreach ($files as $file) {
|
|
if (!array_key_exists($file, self::$catalogues)) {
|
|
self::$catalogues[$file] = false;
|
|
if (is_readable($file)) {
|
|
if (!class_exists('MO')) {
|
|
require_once ABSPATH . WPINC . '/pomo/mo.php';
|
|
}
|
|
$mo = new MO();
|
|
if ($mo->import_from_file($file)) {
|
|
self::$catalogues[$file] = $mo;
|
|
}
|
|
}
|
|
}
|
|
if (self::$catalogues[$file]) {
|
|
return self::$catalogues[$file];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* What the reader sees. $wp is what WordPress translated, $en the English text,
|
|
* $from_catalogue gets the text from a Portuguese catalogue.
|
|
*/
|
|
private static function pick($wp, $en, $from_catalogue)
|
|
{
|
|
if (self::lang() === 'en') {
|
|
// Chosen English: English. Same as WordPress: whatever WordPress loaded (other languages too).
|
|
return self::choice() === 'en' ? $en : $wp;
|
|
}
|
|
$locale = self::reader_locale();
|
|
// WordPress already loaded a Portuguese file for its own pt_* language: respect it.
|
|
if ($wp !== $en && self::lang_of_locale($locale) === 'pt') {
|
|
return $wp;
|
|
}
|
|
$mo = self::catalogue($locale);
|
|
return $mo ? call_user_func($from_catalogue, $mo) : $en;
|
|
}
|
|
|
|
public static function translate($translation, $text, $domain = '')
|
|
{
|
|
return self::pick($translation, $text, function ($mo) use ($text) {
|
|
return $mo->translate($text);
|
|
});
|
|
}
|
|
|
|
public static function translate_plural($translation, $single, $plural, $number, $domain = '')
|
|
{
|
|
$en = ($number == 1) ? $single : $plural;
|
|
return self::pick($translation, $en, function ($mo) use ($single, $plural, $number) {
|
|
return $mo->translate_plural($single, $plural, $number);
|
|
});
|
|
}
|
|
|
|
public static function translate_context($translation, $text, $context, $domain = '')
|
|
{
|
|
return self::pick($translation, $text, function ($mo) use ($text, $context) {
|
|
return $mo->translate($text, $context);
|
|
});
|
|
}
|
|
|
|
public static function translate_plural_context($translation, $single, $plural, $number, $context, $domain = '')
|
|
{
|
|
$en = ($number == 1) ? $single : $plural;
|
|
return self::pick($translation, $en, function ($mo) use ($single, $plural, $number, $context) {
|
|
return $mo->translate_plural($single, $plural, $number, $context);
|
|
});
|
|
}
|
|
|
|
/** Text in a given language, whatever the reader's language (used for messages sent to customers). */
|
|
public static function in($lang, $text)
|
|
{
|
|
if ($lang !== 'pt') {
|
|
return $text;
|
|
}
|
|
$mo = self::catalogue(get_locale());
|
|
return $mo ? $mo->translate($text) : $text;
|
|
}
|
|
}
|