Browse Source

Merge pull request #321 from toitzi/feature/i18n

Fix i18n
Tobias Oitzinger 6 years ago
parent
commit
6a46fcd371

+ 5 - 15
modules/kohana/views/kohana/error.php

@@ -1,14 +1,4 @@
 <?php
-//when exceptions where thrown we where getting a ErrorException [ Fatal Error ]: Call to undefined function __()
-//since i18n was not loaded yet. nasty but works...
-if (!function_exists('__'))
-{
-    function __($message,$variables = NULL)
-    {
-        return is_array($variables) ? strtr($message, $variables):$message;
-    }
-}
-
 // Unique error identifier
 $error_id = uniqid('error');
 
@@ -69,11 +59,11 @@ $error_id = uniqid('error');
 						<?php if ($step['file']): $source_id = $error_id.'source'.$i; ?>
                             <a href="#<?php echo $source_id ?>" onclick="return koggle('<?php echo $source_id ?>')"><?php echo Debug::path($step['file']) ?> [ <?php echo $step['line'] ?> ]</a>
                         <?php else: ?>
-                            {<?php echo __('PHP internal call') ?>}
+                            {<?php echo I18n::get('PHP internal call') ?>}
                         <?php endif ?>
 					</span>
                         &raquo;
-                        <?php echo $step['function'] ?>(<?php if ($step['args']): $args_id = $error_id.'args'.$i; ?><a href="#<?php echo $args_id ?>" onclick="return koggle('<?php echo $args_id ?>')"><?php echo __('arguments') ?></a><?php endif ?>)
+                        <?php echo $step['function'] ?>(<?php if ($step['args']): $args_id = $error_id.'args'.$i; ?><a href="#<?php echo $args_id ?>" onclick="return koggle('<?php echo $args_id ?>')"><?php echo I18n::get('arguments') ?></a><?php endif ?>)
                     </p>
                     <?php if (isset($args_id)): ?>
                         <div id="<?php echo $args_id ?>" class="collapsed">
@@ -95,10 +85,10 @@ $error_id = uniqid('error');
             <?php endforeach ?>
         </ol>
     </div>
-    <h2><a href="#<?php echo $env_id = $error_id.'environment' ?>" onclick="return koggle('<?php echo $env_id ?>')"><?php echo __('Environment') ?></a></h2>
+    <h2><a href="#<?php echo $env_id = $error_id.'environment' ?>" onclick="return koggle('<?php echo $env_id ?>')"><?php echo I18n::get('Environment') ?></a></h2>
     <div id="<?php echo $env_id ?>" class="content collapsed">
         <?php $included = get_included_files() ?>
-        <h3><a href="#<?php echo $env_id = $error_id.'environment_included' ?>" onclick="return koggle('<?php echo $env_id ?>')"><?php echo __('Included files') ?></a> (<?php echo count($included) ?>)</h3>
+        <h3><a href="#<?php echo $env_id = $error_id.'environment_included' ?>" onclick="return koggle('<?php echo $env_id ?>')"><?php echo I18n::get('Included files') ?></a> (<?php echo count($included) ?>)</h3>
         <div id="<?php echo $env_id ?>" class="collapsed">
             <table cellspacing="0">
                 <?php foreach ($included as $file): ?>
@@ -109,7 +99,7 @@ $error_id = uniqid('error');
             </table>
         </div>
         <?php $included = get_loaded_extensions() ?>
-        <h3><a href="#<?php echo $env_id = $error_id.'environment_loaded' ?>" onclick="return koggle('<?php echo $env_id ?>')"><?php echo __('Loaded extensions') ?></a> (<?php echo count($included) ?>)</h3>
+        <h3><a href="#<?php echo $env_id = $error_id.'environment_loaded' ?>" onclick="return koggle('<?php echo $env_id ?>')"><?php echo I18n::get('Loaded extensions') ?></a> (<?php echo count($included) ?>)</h3>
         <div id="<?php echo $env_id ?>" class="collapsed">
             <table cellspacing="0">
                 <?php foreach ($included as $file): ?>

+ 9 - 9
modules/pagination/views/pagination/basic.php

@@ -1,15 +1,15 @@
 <p class="pagination">
 
 	<?php if ($first_page !== FALSE): ?>
-		<a href="<?php echo HTML::chars($page->url($first_page)) ?>" rel="first"><?php echo __('First') ?></a>
+		<a href="<?php echo HTML::chars($page->url($first_page)) ?>" rel="first"><?php echo I18n::get('First') ?></a>
 	<?php else: ?>
-		<?php echo __('First') ?>
+		<?php echo I18n::get('First') ?>
 	<?php endif ?>
 
 	<?php if ($previous_page !== FALSE): ?>
-		<a href="<?php echo HTML::chars($page->url($previous_page)) ?>" rel="prev"><?php echo __('Previous') ?></a>
+		<a href="<?php echo HTML::chars($page->url($previous_page)) ?>" rel="prev"><?php echo I18n::get('Previous') ?></a>
 	<?php else: ?>
-		<?php echo __('Previous') ?>
+		<?php echo I18n::get('Previous') ?>
 	<?php endif ?>
 
 	<?php for ($i = 1; $i <= $total_pages; $i++): ?>
@@ -23,15 +23,15 @@
 	<?php endfor ?>
 
 	<?php if ($next_page !== FALSE): ?>
-		<a href="<?php echo HTML::chars($page->url($next_page)) ?>" rel="next"><?php echo __('Next') ?></a>
+		<a href="<?php echo HTML::chars($page->url($next_page)) ?>" rel="next"><?php echo I18n::get('Next') ?></a>
 	<?php else: ?>
-		<?php echo __('Next') ?>
+		<?php echo I18n::get('Next') ?>
 	<?php endif ?>
 
 	<?php if ($last_page !== FALSE): ?>
-		<a href="<?php echo HTML::chars($page->url($last_page)) ?>" rel="last"><?php echo __('Last') ?></a>
+		<a href="<?php echo HTML::chars($page->url($last_page)) ?>" rel="last"><?php echo I18n::get('Last') ?></a>
 	<?php else: ?>
-		<?php echo __('Last') ?>
+		<?php echo I18n::get('Last') ?>
 	<?php endif ?>
 
-</p><!-- .pagination -->
+</p><!-- .pagination -->

+ 9 - 9
modules/pagination/views/pagination/floating.php

@@ -58,15 +58,15 @@ for ($i = $n7; $i <= $n8; ++$i)
 <p class="pagination">
 
 	<?php if ($first_page !== FALSE): ?>
-		<a href="<?php echo HTML::chars($page->url($first_page)) ?>" rel="first"><?php echo __('First') ?></a>
+		<a href="<?php echo HTML::chars($page->url($first_page)) ?>" rel="first"><?php echo I18n::get('First') ?></a>
 	<?php else: ?>
-		<?php echo __('First') ?>
+		<?php echo I18n::get('First') ?>
 	<?php endif ?>
 
 	<?php if ($previous_page !== FALSE): ?>
-		<a href="<?php echo HTML::chars($page->url($previous_page)) ?>" rel="prev"><?php echo __('Previous') ?></a>
+		<a href="<?php echo HTML::chars($page->url($previous_page)) ?>" rel="prev"><?php echo I18n::get('Previous') ?></a>
 	<?php else: ?>
-		<?php echo __('Previous') ?>
+		<?php echo I18n::get('Previous') ?>
 	<?php endif ?>
 
 	<?php foreach ($links as $number => $content): ?>
@@ -80,15 +80,15 @@ for ($i = $n7; $i <= $n8; ++$i)
 	<?php endforeach ?>
 
 	<?php if ($next_page !== FALSE): ?>
-		<a href="<?php echo HTML::chars($page->url($next_page)) ?>" rel="next"><?php echo __('Next') ?></a>
+		<a href="<?php echo HTML::chars($page->url($next_page)) ?>" rel="next"><?php echo I18n::get('Next') ?></a>
 	<?php else: ?>
-		<?php echo __('Next') ?>
+		<?php echo I18n::get('Next') ?>
 	<?php endif ?>
 
 	<?php if ($last_page !== FALSE): ?>
-		<a href="<?php echo HTML::chars($page->url($last_page)) ?>" rel="last"><?php echo __('Last') ?></a>
+		<a href="<?php echo HTML::chars($page->url($last_page)) ?>" rel="last"><?php echo I18n::get('Last') ?></a>
 	<?php else: ?>
-		<?php echo __('Last') ?>
+		<?php echo I18n::get('Last') ?>
 	<?php endif ?>
 
-</p><!-- .pagination -->
+</p><!-- .pagination -->

+ 1 - 1
modules/userguide/views/userguide/api/class.php

@@ -107,7 +107,7 @@ Class is not declared in a file, it is probably an internal <?php echo html::anc
 <dd><?php echo $prop->description ?></dd>
 <dd><?php echo $prop->value ?></dd>
 <?php if ($prop->default !== $prop->value): ?>
-<dd><small><?php echo __('Default value:') ?></small><br/><?php echo $prop->default ?></dd>
+<dd><small><?php echo I18n::get('Default value:') ?></small><br/><?php echo $prop->default ?></dd>
 <?php endif ?>
 <?php endforeach ?>
 </dl>

+ 3 - 0
system/classes/Error/Exception.php

@@ -0,0 +1,3 @@
+<?php
+
+class Error_Exception extends KO7_Error_Exception {}

+ 5 - 5
system/classes/KO7/Core.php

@@ -952,10 +952,10 @@ class KO7_Core {
 	}
 
 	/**
-	 * PHP error handler, converts all errors into ErrorExceptions. This handler
+	 * PHP error handler, converts all errors into Error_Exceptions. This handler
 	 * respects error_reporting settings.
 	 *
-	 * @throws  ErrorException
+	 * @throws  Error_Exception
 	 * @return  TRUE
 	 */
 	public static function error_handler($code, $error, $file = NULL, $line = NULL)
@@ -963,8 +963,8 @@ class KO7_Core {
 		if (error_reporting() & $code)
 		{
 			// This error is not suppressed by current error reporting settings
-			// Convert the error into an ErrorException
-			throw new ErrorException($error, $code, 0, $file, $line);
+			// Convert the error into an Error_Exception
+			throw new Error_Exception($error, NULL, $code, 0, $file, $line);
 		}
 
 		// Do not execute the PHP error handler
@@ -1005,7 +1005,7 @@ class KO7_Core {
 			ob_get_level() AND ob_clean();
 
 			// Fake an exception for nice debugging
-			KO7_Exception::handler(new ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']));
+			KO7_Exception::handler(new Error_Exception($error['message'], NULL, $error['type'], 0, $error['file'], $error['line']));
 
 			// Shutdown now to avoid a "death loop"
 			exit(1);

+ 31 - 0
system/classes/KO7/Error/Exception.php

@@ -0,0 +1,31 @@
+<?php
+/**
+ * KO7 Error Exception Class
+ */
+class KO7_Error_Exception extends ErrorException {
+
+	/**
+	 * Creates a new translated exception.
+	 *
+	 *     throw new KO7_Error_Exception('Something went terrible wrong, :user',
+	 *         array(':user' => $user));
+	 *
+	 * @param   string          $message    Error message
+	 * @param   array           $variables  Translation variables
+	 * @param   int             $code       The error code
+	 * @param   int             $severity   The severity level of the exception.
+	 * @param   string			$file 		The filename where the exception is thrown.
+	 * @param   int 			$line		The line number where the exception is thrown.
+	 * @param   Throwable       $previous   Previous throwable
+	 * @return  void
+	 */
+	public function __construct(string $message = '', array $variables = NULL, int $code = 0, int $severity = 1, string $file = __FILE__, int $line = __LINE__, $previous = NULL)
+	{
+		// Set the message
+		$message = I18n::get([$message, $variables]);
+
+		// Pass the message and integer code to the parent
+		parent::__construct($message, $code, $severity, $file, $line, $previous);
+	}
+
+}

+ 80 - 51
system/classes/KO7/I18n.php

@@ -2,36 +2,45 @@
 /**
  * Internationalization (i18n) class. Provides language loading and translation
  * methods without dependencies on [gettext](http://php.net/gettext).
- *
  * Typically this class would never be used directly, but used via the __()
  * function, which loads the message and replaces parameters:
  *
- *     // Display a translated message
+ *     // Display a translated message in APPATH
  *     echo __('Hello, world');
  *
- *     // With parameter replacement
- *     echo __('Hello, :user', array(':user' => $username));
+ *	   // Display a translated message in SYSPATH and MODPATH
+ * 	   echo I18n::get('Hello, world');
+ *
+ *     // With parameter replacement in APPATH
+ *     echo __('Hello, :user', [':user' => $username]);
+ *
+ *     // With parameter replacement in SYSPATH nad MODPATH
+ * 	   echo I18n::get(['Hello, :user', [':user' => $username]]);
  *
  * @package    KO7
  * @category   Base
- * @author     Kohana Team
- * @copyright  (c) Kohana Team
+ * @author     Koseven Team
+ * @copyright  (c) 2008 - 2016 Kohana Team
+ * @copyright  (c) since  2018 Koseven Team
  * @license    https://koseven.ga/LICENSE.md
  */
 class KO7_I18n {
 
 	/**
-	 * @var  string   target language: en-us, es-es, zh-cn, etc
+	 * Target language: en-us, es-es, zh-cn, etc
+	 * @var  string
 	 */
 	public static $lang = 'en-us';
 
 	/**
-	 * @var  string  source language: en-us, es-es, zh-cn, etc
+	 * Source language: en-us, es-es, zh-cb, etc
+	 * @var string
 	 */
 	public static $source = 'en-us';
 
 	/**
-	 * @var  array  cache of loaded languages
+	 * Cache of loaded languages
+	 * @var array
 	 */
 	protected static $_cache = [];
 
@@ -44,15 +53,15 @@ class KO7_I18n {
 	 *     // Change the current language to Spanish
 	 *     I18n::lang('es-es');
 	 *
-	 * @param   string  $lang   new language setting
+	 * @param   string  $lang  New target language
+	 *
 	 * @return  string
 	 * @since   3.0.2
 	 */
-	public static function lang($lang = NULL)
+	public static function lang(string $lang = NULL) : string
 	{
-		if ($lang)
+		if ($lang && $lang !== I18n::$lang)
 		{
-			// Normalize the language
 			I18n::$lang = strtolower(str_replace([' ', '_'], '-', $lang));
 		}
 
@@ -65,35 +74,64 @@ class KO7_I18n {
 	 *
 	 *     $hello = I18n::get('Hello friends, my name is :name');
 	 *
-	 * @param   string  $string text to translate
-	 * @param   string  $lang   target language
+	 * @param   string|array  $string Text to translate or array [text, values]
+	 * @param   string  	  $lang   Target Language
+	 * @param   string		  $source Source Language
 	 * @return  string
 	 */
-	public static function get($string, $lang = NULL)
+	public static function get($string, string $lang = NULL, string $source = NULL)
 	{
+		$values = [];
+
+		// Check if $string is array [text, values]
+		if (Arr::is_array($string))
+		{
+			if (isset($string[1]) && Arr::is_array($string[1]))
+			{
+				$values = $string[1];
+			}
+			$string = $string[0];
+		}
+
+		// Set Target Language if not set
 		if ( ! $lang)
 		{
 			// Use the global target language
 			$lang = I18n::$lang;
 		}
 
-		// Load the translation table for this language
-		$table = I18n::load($lang);
+		// Set source Language if not set
+		if ( ! $source)
+		{
+			// Use the global source language
+			$source = I18n::$source;
+		}
+
+		// Load Table only if Source language does not match target language
+		if ($source !== $lang)
+		{
+			// Load the translation table for this language
+			$table = I18n::load($lang);
 
-		// Return the translated string if it exists
-		return isset($table[$string]) ? $table[$string] : $string;
+			// Return the translated string if it exists
+			$string = $table[$string] ?? $string;
+		}
+
+		return empty($values) ? $string : strtr($string, $values);
 	}
 
 	/**
 	 * Returns the translation table for a given language.
 	 *
-	 *     // Get all defined Spanish messages
-	 *     $messages = I18n::load('es-es');
+	 *     // Get all defined English messages.
+	 *     // This will look for translation inside 'i18n/en/us.php'
+	 * 	   // and overwrite the ones in 'i18n/en.php'
+	 *     $messages = I18n::load('en-us');
 	 *
 	 * @param   string  $lang   language to load
 	 * @return  array
 	 */
-	public static function load($lang)
+	public static function load(string $lang) : array
 	{
 		if (isset(I18n::$_cache[$lang]))
 		{
@@ -101,39 +139,35 @@ class KO7_I18n {
 		}
 
 		// New translation table
-		$table = [];
+		$table = [[]];
 
 		// Split the language: language, region, locale, etc
 		$parts = explode('-', $lang);
 
-		do
+		// Loop through Paths
+		foreach ([$parts[0], implode(DIRECTORY_SEPARATOR, $parts)] as $path)
 		{
-			// Create a path for this set of parts
-			$path = implode(DIRECTORY_SEPARATOR, $parts);
+			// Load files
+			$files = KO7::find_file('i18n', $path);
 
-			if ($files = KO7::find_file('i18n', $path, NULL, TRUE))
+			// Loop through files
+			if ( ! empty($files))
 			{
-				$t = [];
+				$t = [[]];
 				foreach ($files as $file)
 				{
 					// Merge the language strings into the sub table
-					$t = array_merge($t, KO7::load($file));
+					$t[] = KO7::load($file);
 				}
-
-				// Append the sub table, preventing less specific language
-				// files from overloading more specific files
-				$table += $t;
+				$table[] = $t;
 			}
-
-			// Remove the last part
-			array_pop($parts);
 		}
-		while ($parts);
+
+		$table = array_merge(...array_merge(...$table));
 
 		// Cache the translation table locally
 		return I18n::$_cache[$lang] = $table;
 	}
-
 }
 
 if ( ! function_exists('__'))
@@ -147,20 +181,15 @@ if ( ! function_exists('__'))
 	 * [!!] The target language is defined by [I18n::$lang].
 	 *
 	 * @uses    I18n::get
-	 * @param   string  $string text to translate
-	 * @param   array   $values values to replace in the translated text
-	 * @param   string  $lang   source language
+	 *
+	 * @param   string  $string  Text to translate
+	 * @param   array   $values  Values to replace in the translated text
+	 * @param   string  $lang    Source language
+	 *
 	 * @return  string
 	 */
-	function __($string, array $values = NULL, $lang = 'en-us')
+	function __(string $string, array $values = NULL, $lang = NULL)
 	{
-		if ($lang !== I18n::$lang)
-		{
-			// The message and target languages are different
-			// Get the translation for this message
-			$string = I18n::get($string);
-		}
-
-		return empty($values) ? $string : strtr($string, $values);
+		return I18n::get($values ? [$string, $values] : $string, NULL, $lang);
 	}
 }

+ 1 - 1
system/classes/KO7/KO7/Exception.php

@@ -50,7 +50,7 @@ class KO7_KO7_Exception extends Exception {
     public function __construct($message = "", array $variables = NULL, $code = 0, Throwable $previous = NULL)
     {
         // Set the message
-        $message = __($message, $variables);
+        $message = I18n::get([$message, $variables]);
 
         // Pass the message and integer code to the parent
         parent::__construct($message, (int) $code, $previous);

+ 1 - 1
system/classes/KO7/Upload.php

@@ -215,7 +215,7 @@ class KO7_Upload {
 				// Get the width and height from the uploaded image
 				list($width, $height) = getimagesize($file['tmp_name']);
 			}
-			catch (ErrorException $e)
+			catch (Error_Exception $e)
 			{
 				// Ignore read errors
 			}

Some files were not shown because too many files changed in this diff