Welcome Guest, Not a member yet? Register   Sign In
Multilingual random quotes HMVC module
#1

[eluser]Altazar[/eluser]
I was searching for an answer, but after I wrote the question, I've found the solution and I'll share it here. I'm using HMVC and have made a module "quotes" which had only this controller and nothing else:
Code:
class Quotes extends MX_Controller {

public function __construct()
{
  parent::__construct();
}

public function index(){
   $quotes[] = "If one does not know to which port one is sailing, no wind is favorable. - <i>Lucius Annaeus Seneca</i>";
   $quotes[] = "Not all who wander are lost. - <i>JRR Tolkien</i>";
   $quotes[] = "Red sky at night, sailor's delight. Red sky in morning, sailor's warning. - <i>author unknown</i>";
   $random_number = rand(0,count($quotes)-1);
   echo $quotes[$random_number];
}
}
I've included it in pages like other modules:
Code:
echo '<blockquote class="quotes">';
echo Modules::run('quotes');
echo '</blockquote>';
Now I wanted to make it multilingual using i18n library, so I've placed translations in "language" folder inside the same module. This is english quotes_lang.php:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$lang['quotes.01']   = "If one does not know to which port one is sailing, no wind is favorable. - <i>Lucius Annaeus Seneca</i>";
$lang['quotes.02']   = "Not all who wander are lost. - <i>JRR Tolkien</i>";
$lang['quotes.03']   = "Red sky at night, sailor's delight. Red sky in morning, sailor's warning. - <i>author unknown</i>";

/* End of file quotes_lang.php */
/* Location: ./application/modules/quotes/language/english/quotes_lang.php */
And this is the change in controller above:
Code:
$this->lang->load('quotes');
$quotes[] = lang('quotes.01');
$quotes[] = lang('quotes.02');
$quotes[] = lang('quotes.03');
I hope someone finds it useful. Good luck! Smile




Theme © iAndrew 2016 - Forum software by © MyBB