Skip to content
This repository has been archived by the owner on Nov 25, 2022. It is now read-only.

Commit

Permalink
Added support for php-gettext.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkader committed May 26, 2018
1 parent 2011fe4 commit 6fe9c63
Show file tree
Hide file tree
Showing 13 changed files with 1,893 additions and 74 deletions.
12 changes: 10 additions & 2 deletions application/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* In case you want to use Gettext extension instead of CodeIgniter PHP array
* for languages lines, set this to "true".
* @since 2.1.0
*/
defined('USE_GETTEXT') OR define('USE_GETTEXT', false);

/**
* We make sure to load our Skeleton "bootstrap.php" file.
* NOTE: Keep this line at the top of this file.
* NOTE: Keep this line at the top of this file and right
* below "USE_GETTEXT" definition.
* @since 2.1.0
*/
require_once(KBPATH.'bootstrap.php');
Expand Down Expand Up @@ -35,4 +43,4 @@
*
* And away we go...
*/
require_once BASEPATH.'core/CodeIgniter.php';
require_once(BASEPATH.'core/CodeIgniter.php');
11 changes: 11 additions & 0 deletions content/language/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>

<p>Directory access is forbidden.</p>

</body>
</html>
12 changes: 12 additions & 0 deletions skeleton/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,15 @@ function _csk_reserved_module($module = null)
return (empty($modules)) ? false : in_array($module, $modules);
}
}

// ------------------------------------------------------------------------

/**
* See if the user decided to use PHP-Gettext instead of PHP Array
* for languages lines.
* @since 2.1.0
*/
if (defined('USE_GETTEXT') && true === USE_GETTEXT)
{
require_once(KBPATH.'third_party/bkader/gettext/class-gettext.php');
}
171 changes: 124 additions & 47 deletions skeleton/core/KB_Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class KB_Lang extends CI_Lang
*/
protected $fallback = 'english';

/**
* Flag to check whether we should use PHP-Gettext.
* @since 2.1.0
*/
public $gettext = false;

/**
* Class constructor.
* @return void
Expand All @@ -70,6 +76,14 @@ public function __construct()

$this->config =& load_class('Config', 'core');
$this->router =& load_class('Router', 'core');

/**
* In order to use PHP-Gettext, the following must be true:
* 1. USE_GETTEXT constant is defined and set to TRUE.
* 2. "gettext_instance" function exists.
*/
$this->gettext = (defined('USE_GETTEXT') && true === USE_GETTEXT);
$this->gettext = function_exists('gettext_instance');
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -265,6 +279,15 @@ public function load($langfile, $idiom = '', $return = false, $add_suffix = true
*/
public function line($line, $index = '')
{
/**
* See if we are are using PHP-Gettext.
* @since 2.1.0
*/
if ($this->gettext && false === stripos($line, 'csk_'))
{
return __($line, $index); // $index is used as the text domain.
}

/**
* Because some lines start with CSK_ (uppercased), we make sure
* to format the $line before we proceed.
Expand Down Expand Up @@ -293,18 +316,11 @@ public function line($line, $index = '')
log_message('error', 'Could not find the language line "'.$line.'"');

/**
* We use inflector helper to avoid the FIXME thing on production.
* @since 2.0.0
* Both "inflect" and "FIXME" use were dropped. We use the $line
* as it is, as the line to return.
* @since 2.1.0
*/
if ('production' === ENVIRONMENT)
{
function_exists('humanize') OR get_instance()->load->helper('inflector');
$value = humanize($line);
}
else
{
$value = "FIXME('{$line}')";
}
$value = $line;
}

return $value;
Expand All @@ -328,6 +344,16 @@ function_exists('humanize') OR get_instance()->load->helper('inflector');
*/
public function nline(string $singular, string $plural, int $number, $index = '')
{
/**
* See if we are are using PHP-Gettext.
* @since 2.1.0
*/
if ($this->gettext && false === stripos($line, 'csk_'))
{
// $index is used as the text domain.
return _n($singular, $plural, $number, $index);
}

return ($number == 1)
? $this->line($singular, $index)
: $this->line($plural, $index);
Expand All @@ -343,59 +369,95 @@ public function nline(string $singular, string $plural, int $number, $index = ''
*/
public function lang()
{
// Make the method remember the language.
static $lang;

// Not yet cached?
if (empty($lang))
{
$lang = $this->languages($this->config->item('language'));
// "current_language" item should have been set by Kbcore library.
$lang = $this->config->item('current_language');

// In case it wasn't set, we use a backup a plan.
if ( ! $lang)
{
/**
* The language folder should have been stored in SESSION, in
* case it was not, we use default language.
*/
$folder = isset($_SESSION['language'])
? $_SESSION['language']
: $this->config->item('language');

$lang = $this->languages($folder);

// If the language was found, we use it.
if (isset($lang[$folder]))
{
$lang = $lang[$folder];
}
// Otherwise, we use English as a backup plan.
else
{
$lang = array(
'name' => 'English',
'name_en' => 'English',
'folder' => 'english',
'locale' => 'en-US',
'gettext' => 'en_US',
'direction' => 'ltr',
'code' => 'en',
'flag' => 'us',
);
}
}
}

// Collect function arguments.
$args = func_get_args();
// The language was not found?
if ( ! $lang)
{
return false;
}

// Empty? Return the current language.
if (empty($args))
// Not arguments passed? Return the language folder.
if (empty($args = func_get_args()))
{
return $lang['folder'];
}

// The language is already cached? Use it. Otherwise load it.
// Prepare the the final returned result.
$return = $lang;

// Not found ?
if ( ! $return)
{
return FALSE;
}

// Get rid of nasty array.
(is_array($args[0])) && $args = $args[0];
$args_count = count($args);

// In case of a boolean, we return all language details.
if (is_bool($args[0]) && $args[0] === true)
if ($args_count == 1 && $args[0] === true)
{
return $return;
}

// In case of a single item and found, return it.
if (count($args) === 1 && isset($return[$args[0]]))
if ($args_count === 1 && isset($return[$args[0]]))
{
return $return[$args[0]];
}

$_return = array();

// Loop through language details and get only what's requested.
foreach ($args as $arg)
// Multiple arguments?
if ($args_count >= 2)
{
if (isset($return[$arg]))
$_return = array();

foreach ($args as $arg)
{
$_return[$arg] = $return[$arg];
isset($return[$arg]) && $_return[$arg] = $return[$arg];
}

empty($_return) OR $return = $_return;
}

// Return the result if any.
return ($_return) ? $_return : $return;
return $return;
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -433,39 +495,54 @@ public function languages($langs = null)
}
}

$return = $languages;

// No argument? return all languages.
if (null === $langs)
{
return $languages;
return $return;
}

// Format our requested languages.
( ! is_array($langs)) && $langs = array_map('trim', explode(',', $langs));

// A single language is requested? Return it.
if (count($langs) == 1 && isset($languages[$langs[0]]))
if ( ! empty($langs))
{
return $languages[$langs[0]];
}

// Build requested languages array.
$_languages = array();
foreach ($langs as $lang)
{
if (isset($languages[$lang]))
// Build requested languages array.
$_languages = array();
foreach ($langs as $lang)
{
$_languages[$lang] = $languages[$lang];
if (isset($languages[$lang]))
{
$_languages[$lang] = $languages[$lang];
}
}

empty($_languages) OR $return = $_languages;
}

// If found any, return them. Otherwise, we return the full array.
return empty($_languages) ? $languages : $_languages;
return $return;
}

}

// ------------------------------------------------------------------------

if ( ! function_exists('langinfo'))
{
/**
* Returns the name or details about the language currently in use.
* @param mixed $key what to retrieve.
* @return mixed
*/
function langinfo($key = null)
{
return get_instance()->lang->lang($key);
}
}

// ------------------------------------------------------------------------

if ( ! function_exists('lang'))
{
/**
Expand Down

0 comments on commit 6fe9c63

Please sign in to comment.