Skip to content

Commit

Permalink
Merge branch '31-internationalization-translations'
Browse files Browse the repository at this point in the history
  • Loading branch information
kip9 committed Apr 1, 2014
2 parents e8522cf + 2e32942 commit de605ac
Show file tree
Hide file tree
Showing 40 changed files with 2,392 additions and 151 deletions.
7 changes: 4 additions & 3 deletions application/config/all/app.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
- Define application errors and error codes
- Define application errors and error codes
- This will keep the application lean and the formatErrors method can do easy lookups
- Feel free to add on
- After adding here, you need to update application/language/phrases.php, then regenerate .pot and update proper .po translations. Helper script to come
*/
$config['error_codes'] = array();

Expand All @@ -15,8 +16,8 @@
$config['error_codes'][5] = 'Mark could not be restored.';
$config['error_codes'][6] = 'Could not add mark.';
$config['error_codes'][7] = 'Could not delete mark.';
$config['error_codes'][8] = 'This mark does\'t have a valid URL.';
$config['error_codes'][9] = 'This mark does\'t have a valid title.';
$config['error_codes'][8] = 'This mark doesn\'t have a valid URL.';
$config['error_codes'][9] = 'This mark doesn\'t have a valid title.';

// Labels - Level 30 - 59
$config['error_codes'][30] = 'No `label_id` was found.';
Expand Down
10 changes: 10 additions & 0 deletions application/config/all/language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// Language to use
$config['default_language'] = 'english';

// Supported languages
$config['supported_languages'] = array(
'english' => 'en_US',
'polish' => 'pl_PL'
);
8 changes: 8 additions & 0 deletions application/config/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
|
*/

// Loads language translation files
$hook['pre_controller_method'][''] = array(
'class' => 'Unmark_Localization',
'function' => 'loadLanguage',
'filename' => 'Unmark_Localization.php',
'filepath' => 'hooks',
);


/* End of file hooks.php */
/* Location: ./application/config/hooks.php */
2 changes: 1 addition & 1 deletion application/controllers/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function upgrade()
show_error($this->migration->error_string());
exit;
}
exit('Upgraded. Please <a href="/">return home</a>.');
exit(sprintf(_('Upgraded. Please <a href="%s">return home</a>.'), '/'));

}

Expand Down
7 changes: 4 additions & 3 deletions application/controllers/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function index()
$user = $this->user->read("email = '" . $this->db_clean->email . "'", 1, 1);

if (! isset($user->user_id)) {
$this->data['message'] = 'The email address `' . $this->clean->email . '` was not found.';
$this->data['message'] = sprintf(_('The email address `%s` was not found.'), $this->clean->email);
}
elseif (! isset($user->active) || empty($user->active)) {
$this->data['message'] = 'Your account is no longer active. Please contact support.';
$this->data['message'] = _('Your account is no longer active. Please contact support.');
}
else {
// Check proper password
Expand All @@ -45,7 +45,8 @@ public function index()

// Check if passwords match
if ($match === false) {
$this->data['message'] = 'Your password is incorrect. Please try again.';

$this->data['message'] = _('Your password is incorrect. Please try again.');
}
else {
// At this point we are clear for takeoff
Expand Down
1 change: 1 addition & 0 deletions application/controllers/marks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Marks extends Plain_Controller

public function __construct()
{
$this->localized = true;
parent::__construct();
$this->redirectIfLoggedOut();
$this->load->model('users_to_marks_model', 'user_marks');
Expand Down
9 changes: 3 additions & 6 deletions application/controllers/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ public function index()
public function updateEmail()
{
if (! isset($this->db_clean->email) || ! isValid($this->db_clean->email, 'email')) {
$error_codes = $this->config->item('error_codes');
$this->data['message'] = $error_codes[604];
$this->data['message'] = reset(array_values(formatErrors(604)));
}
else {
// Check if email already exists
$total = $this->user->count("email = '" . $this->db_clean->email . "'");

if ($total > 0) {
$error_codes = $this->config->item('error_codes');
$this->data['message'] = $error_codes[603];
$this->data['message'] = reset(array_values(formatErrors(603)));
}
else {
$user = $this->user->update($this->user_id, array('email' => $this->db_clean->email));
Expand All @@ -64,8 +62,7 @@ public function updateEmail()
public function updatePassword()
{
if (! isset($this->clean->password) || ! isValid($this->clean->password, 'password')) {
$error_codes = $this->config->item('error_codes');
$this->data['message'] = $error_codes[602];
$this->data['message'] = reset(array_values(formatErrors(602)));
}
else {

Expand Down
7 changes: 7 additions & 0 deletions application/core/Plain_CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ function &get_instance()
*/
$EXT->_call_hook('post_controller_constructor');

/*
* ------------------------------------------------------
* Is there a "pre_controller_method" hook?
* ------------------------------------------------------
*/
$EXT->_call_hook('pre_controller_method');

/*
* ------------------------------------------------------
* Call the requested method
Expand Down
46 changes: 43 additions & 3 deletions application/core/Plain_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Plain_Controller extends CI_Controller
{

public $clean = null;
public $csrf_valid = false;
public $current_user = array();
Expand All @@ -19,6 +19,12 @@ class Plain_Controller extends CI_Controller
public $user_admin = false;
public $user_id = 0;
public $user_token = 0;
// Determines if controller tries to load language files
public $localized = true;
// User selected language (overriden in config)
public $selected_language = null;
// Supported languages list (overriden in config)
public $supported_languages = array('english' => 'en_US');

public function __construct()
{
Expand All @@ -30,7 +36,12 @@ public function __construct()

// Clean incoming variables in a variety of ways
$this->clean();


if($this->localized && $this->selected_language === null){
// Set list of supported languages for the app and pick selected
$this->getLanguageFromConfig();
}

// Get user token
$this->getUserInfo();

Expand Down Expand Up @@ -314,7 +325,7 @@ protected function getUserInfo()
$this->logged_in = (isset($this->session)) ? $this->session->userdata('logged_in') : false;
$this->current_user = (! empty($user_session)) ? $user_session : $this->current_user;
}

protected function isAdmin()
{
return $this->user_admin;
Expand Down Expand Up @@ -574,5 +585,34 @@ protected function view($view, $data=array())
$this->load->view('partials/debug', $data);
}
}

/**
* Determines language selection based on configuration file
*/
protected function getLanguageFromConfig(){
// Get languages
$this->load->config('all/language');
$languages = $this->config->item('supported_languages');
if(empty($languages)){
$languages = array('english'=>'en_US');
}
$this->supported_languages = $languages;
$languageList = array_values($this->supported_languages);
// Check if there is any choice
$langsCount = count($languageList);
if($langsCount == 0){
// No languages available
$this->localized = false;
$this->selected_language = null;
} else{
if($langsCount>1){
$lang = $this->config->item('default_language');
$this->selected_language = $languages[$lang];
} else if($langsCount == 1){
// No choice - return what's available
$this->selected_language = $languages[$languageList[0]];
}
}
}

}
91 changes: 91 additions & 0 deletions application/core/Plain_Lang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Plain_Lang extends CI_Lang {

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

/**
* Load a language file
*
* @access public
* @param mixed the name of the language file to be loaded. Can be an array
* @param string the language (english, etc.)
* @param bool return loaded array of translations
* @param bool add suffix to $langfile
* @param string alternative path to look for language file
* @return mixed
*/
function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
{
$langfile = str_replace('.php', '', $langfile);

if ($add_suffix == TRUE)
{
$langfile = str_replace('_lang.', '', $langfile).'_lang';
}

$langfile .= '.php';

if (in_array($langfile, $this->is_loaded, TRUE))
{
return;
}

$config =& get_config();

if ($idiom == '')
{
$deft_lang = ( ! isset($config['language'])) ? 'english' : $config['language'];
$idiom = ($deft_lang == '') ? 'english' : $deft_lang;
}

// Determine where the language file is and load it
if ($alt_path != '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
{
include($alt_path.'language/'.$idiom.'/'.$langfile);
}
else
{
$found = FALSE;
// Places to search for language files - system, application and custom folders in that order
// This way custom entries override application ones which override system ones
$lookupPaths = array();
array_push($lookupPaths, BASEPATH);
array_push($lookupPaths, APPPATH);
array_push($lookupPaths, CUSTOMPATH);
foreach ($lookupPaths as $package_path)
{
if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
{
include($package_path.'language/'.$idiom.'/'.$langfile);
$found = TRUE;
}
}

if ($found !== TRUE)
{
show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
}
}


if ( ! isset($lang))
{
log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
return;
}

if ($return == TRUE)
{
return $lang;
}

$this->is_loaded[] = $langfile;
$this->language = array_merge($this->language, $lang);
unset($lang);

log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
return TRUE;
}

}
22 changes: 16 additions & 6 deletions application/helpers/data_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,24 @@ function findStartFinish($start, $finish=null)
return array('start' => date('Y-m-d', $start), 'finish' => date('Y-m-d', $finish));
}

// Format any errors coming back to standardize them
function formatErrors($errors)
/**
* Format any errors coming back to standardize them
* @param int $errors Error code
* @param string $params Params passed to error message (if needed)
* @return array Array with error codes and messages
*/
function formatErrors($errors, $params = null)
{
if (is_numeric($errors)) {
$CI =& get_instance();
$error_codes = $CI->config->item('error_codes');
$errno = (array_key_exists($errors, $error_codes)) ? $errors : 0;
$message = (! empty($errno)) ? $error_codes[$errno] : 'Unknown Error';
if(array_key_exists($errors, $error_codes)){
$errno = $errors;
$message = empty($params) ? $error_codes[$errno] : call_user_func_array("sprintf", array_merge(array($error_codes[$errno]), $params));
} else{
$errno = 0;
$message = 'Unknown Error';
}
if ($errors >= 400 & $errors < 600) {
set_response_code($errors);
}
Expand Down Expand Up @@ -118,11 +128,11 @@ function generateTimeSpan($date)
foreach ($results as $type => $number) {
if (! empty($number)) {
$s = ($number == '1') ? '' : 's';
return $number . ' ' . $type . $s . ' ago';
return $number . ' ' . ngettext($type, $type.'s', $number) . ' ' . _('ago');
}
}

return 'Just Now';
return _('Just Now');
}

function getLastJsonError()
Expand Down
15 changes: 6 additions & 9 deletions application/helpers/view_helper.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
<?php defined("BASEPATH") or exit("No direct script access allowed");

// Checks the count and makes the string plural (well adds an s) if it's longer than 1 or 0.
function determinePlurality($num, $str, $show_num=true)
// Prints marks count in nice format
function printMarksCount($num)
{
if ($num == 0) { return "No ". $str . "s"; }
if ($show_num) {
return ($num > 1) ? $num . " " . $str . "s" : $num . " " . $str;
} else {
return ($num > 1) ? $str . "s" : $str;
if($num > 0) {
return sprintf(ngettext('%s mark', '%s marks', $num), $num);
} else {
return _('No marks');
}

}


// Checks current date with passed date and responsds with proper verbiage.
function formatExpires($date)
{
Expand Down
Loading

0 comments on commit de605ac

Please sign in to comment.