1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-26 16:14:35 +02:00

Small optimization for for __('text') translation function when used in PHP 5.4.0 or newer and no textdomain specified.

This commit is contained in:
Ryan Cramer
2018-04-13 09:17:09 -04:00
parent 5884f05b9f
commit 566d60b152
2 changed files with 30 additions and 3 deletions

View File

@@ -5,7 +5,7 @@
* *
* Provide GetText like language translation functions to ProcessWire * Provide GetText like language translation functions to ProcessWire
* *
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer * ProcessWire 3.x, Copyright 2018 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
*/ */
@@ -13,6 +13,12 @@
/** /**
* Perform a language translation * Perform a language translation
* *
* ~~~~~~
* echo __('This is translatable text');
* echo __('Translatable with current file as textdomain', __FILE__);
* echo __('Translatable with other file as textdomain', '/site/templates/_init.php');
* ~~~~~~
*
* @param string $text Text for translation. * @param string $text Text for translation.
* @param string $textdomain Textdomain for the text, may be class name, filename, or something made up by you. If omitted, a debug backtrace will attempt to determine it automatically. * @param string $textdomain Textdomain for the text, may be class name, filename, or something made up by you. If omitted, a debug backtrace will attempt to determine it automatically.
* @param string $context Name of context - DO NOT USE with this function for translation as it won't be parsed for translation. Use only with the _x() function, which will be parsed. * @param string $context Name of context - DO NOT USE with this function for translation as it won't be parsed for translation. Use only with the _x() function, which will be parsed.
@@ -20,14 +26,23 @@
* *
*/ */
function __($text, $textdomain = null, $context = '') { function __($text, $textdomain = null, $context = '') {
static $useLimit = null;
if(!wire('languages')) return $text; if(!wire('languages')) return $text;
if(!$language = wire('user')->language) return $text; if(!$language = wire('user')->language) return $text;
/** @var Language $language */ /** @var Language $language */
if(!$language->id) return $text; if(!$language->id) return $text;
if($useLimit === null) {
$useLimit = version_compare(PHP_VERSION, '5.4.0') >= 0;
}
if(is_null($textdomain)) { if(is_null($textdomain)) {
if(defined('DEBUG_BACKTRACE_IGNORE_ARGS')) { if($useLimit) {
// PHP 5.4.0 or newer
$traces = @debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
} else if(defined('DEBUG_BACKTRACE_IGNORE_ARGS')) {
// PHP 5.3.6 or newer
$traces = @debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $traces = @debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
} else { } else {
// older PHP (deprecated)
$traces = @debug_backtrace(); $traces = @debug_backtrace();
} }
if(isset($traces[0]) && $traces[0]['file'] != __FILE__) { if(isset($traces[0]) && $traces[0]['file'] != __FILE__) {
@@ -57,6 +72,11 @@ function __($text, $textdomain = null, $context = '') {
* *
* Used when to text strings might be the same in English, but different in other languages. * Used when to text strings might be the same in English, but different in other languages.
* *
* ~~~~~
* echo _x('Click for more', 'button');
* echo _x('Click for more', 'text-link');
* ~~~~~
*
* @param string $text Text for translation. * @param string $text Text for translation.
* @param string $context Name of context * @param string $context Name of context
* @param string $textdomain Textdomain for the text, may be class name, filename, or something made up by you. If omitted, a debug backtrace will attempt to determine automatically. * @param string $textdomain Textdomain for the text, may be class name, filename, or something made up by you. If omitted, a debug backtrace will attempt to determine automatically.
@@ -70,6 +90,13 @@ function _x($text, $context, $textdomain = null) {
/** /**
* Perform a language translation with singular and plural versions * Perform a language translation with singular and plural versions
* *
* ~~~~~
* $items = array(...);
* $qty = count($items);
* echo _n('Found one item', 'Found multiple items', $qty);
* echo sprintf(_n('Found one item', 'Found %d items', $qty), $qty);
* ~~~~~
*
* @param string $textSingular Singular version of text (when there is 1 item) * @param string $textSingular Singular version of text (when there is 1 item)
* @param string $textPlural Plural version of text (when there are multiple items or 0 items) * @param string $textPlural Plural version of text (when there are multiple items or 0 items)
* @param int $count Quantity of items, should be 0 or more. * @param int $count Quantity of items, should be 0 or more.

View File

@@ -42,7 +42,7 @@ if(!defined("PROCESSWIRE")) die();
<?php if($config->debug && $adminTheme->isSuperuser && strpos($adminTheme->layout, 'sidenav') === false): ?> <?php if($config->debug && $adminTheme->isSuperuser && strpos($adminTheme->layout, 'sidenav') === false): ?>
<li> <li>
<a href='#' onclick="$('#debug_toggle').click(); return false;"> <a href='#' onclick="$('#debug_toggle').click(); return false;">
<?php echo $adminTheme->renderNavIcon('bug') . __('Debug'); ?> <?php echo $adminTheme->renderNavIcon('bug') . __('Debug', __FILE__); ?>
</a> </a>
</li> </li>
<?php endif; ?> <?php endif; ?>