Multilingual set up of codeigniter - codeigniter

I'm using this tutorial http://sumonbd.wordpress.com/2009/09/16/develop-multilingual-site-using-codeigniter-i18n-library/ for multilingual in codeigniter.
I've follow the instruction listed correctly. It does not gave me any error, but I couldn't say that it is working properly.
when I run through mysite.com/en/about and mysite.com/fr/about it always give me the default language which is english. I'm wondering if there is any configuration that I need to set to be able to work properly.
I'm thinking about this in config.php
$config['language']
and
this in autoload.php
$autoload['language']
Do I have to configure those? or any other configuration to work the multilingual properly.

After analyzing the code of CI and the codes in the blog I've come up to this solution.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends CI_Controller {
function index()
{
// you might want to just autoload these two helpers
$this->load->helper('language');
$this->load->helper('url');
$this->getLang();
$this->load->view('about');
}
function getLang(){
$url = $_SERVER['REQUEST_URI'];
$lang = explode("/", $url);
if($lang[2] == 'en'){
// load language file
return $loadLang = $this->lang->load('english','english');
}
else if($lang[2] == 'fr'){
// load language file
return $loadLang = $this->lang->load('french','french');
}
else{
// load language file
return $loadLang = $this->lang->load('english','english');
}
return false;
}
}
I've created the function getLang() in which I load the language file.
$this->lang->load('language_file','language_folder');

Sorry, you don't provide enough information about what you have done. But I'll try to answer.
I've try the updated tutorial, and it's works like a charm.
First, you point to an out to date tutorial. This is the source of your link with an update: source
Second, a file prefixed with MY_ (default configuration) is a core classes ( see Core Classes ) not a library and should be placed on application/core directory.
Third, in your view file, try to avoid using php short tag (<?=$variabel;?>) because not all server configuration enable php short tag. Use normal echo tag instead <?php echo $var; ?>. Longer, but worth it as you write the standard pattern.
As I said before, if you follow the updated tutorial your script should work as mine. I hope this help. Sorry for my English, correct me if I'm wrong.

i think you should have a look at URI-Language-Identifier i use it for my multilingual projects, and its working as it should.

Related

Joomla - call function model within the model

In a public function of my model I call
$user_type=$this->get_user_type();
In the same model I have
private function get_user_type()
{
$user_type='asd';
$asd_groups = (int)$config->get('asd_groups');
$ver_groups = (int)$config->get('ver_groups');
jimport( 'joomla.user.helper' );
$user_groups=JUserHelper::getUserGroups($user->id);
if(in_array($asd_groups,$user_groups)){
$user_type='asd';
}
if(in_array($ver_groups,$user_groups)){
$user_type='ver';
}
return $user_type;
}
The site give me a white page, if I comment the calling line "$this->get_user_type();" then it works...
I really don't understand what is wrong here.
There is not enough information or code here to help you… for example where is $config coming from and what is it? What version of Joomla is this on?
If $config is not defined as a global then that may be the source of the problem depending on your PHP setup.
Things you can do to help yourself find the problem, in Joomla's Global Configuration.
Set Error Messages to "Development" in Joomla (you are using a development site and not a live website right?)
Turn on Joomla's DEBUG mode
Then update your question with details of error messages, Joomla version and where this code is running (you say your model) and where $config is coming from.
Oh sure!
I have missed the two configuration variable when i moved the code from inside a function in a dedicated function.
I copied these two lines on the first row of the function and now it works!
$config = JComponentHelper::getParams(S_APP_NAME);
$user = JFactory::getUser ();

CodeIgniter pagintation - appending the page number to "page" string

What I am trying to achieve is urls outputting via pagination like this:
http://www.mysite.com/users/page5
or
http://www.mysite.com/users/page-5
At present, it will be using the URI segments like this:
http://www.mysite.com/users/page/5
I can modify the routes.php config file to route the path if the first two URLs are used. So, that's not the issue.
What I am having trouble with is, how do I initialize the settings for the pagination, so that the $this->pagination->create_links() will create a pagination with items having links like in the first or the second format?
Let me know if you need more explanation or examples regarding this. I'm not much good in explaining things. :)
Thank you
This functionality already exists in the in-development version of CodeIgniter 3.0. You can view the Pagination class as it sits here.
To use this library, you can either A) use all of CI 3.0 (it's pretty stable), or B) extend (or, more realistically, replace) the Pagination library by creating application/libraries/MY_Pagination.php and filling it with the contents of the link above. (Full disclosure: it's been a while since I tinkered with CI, so I don't know if anything has changed since that may result in errors with any of this answer.)
To use the feature you want, specify your base URL minus the page-X segment, set that you want to use page numbers instead of offset in your URI segment, and then specify a prefix.
$config['base_url'] = site_url('users');
$config['use_page_numbers'] = true;
$config['prefix'] = 'page-';
Make sure to include your other obvious items as well, such as per_page, etc.
To change the functionality of the Pagination library, you can extend the library and override the create_links() function.
Create a file named MY_Pagination.php in application/libraries/
The file should have the following structure, so you can change or add additional functionality to CI's native Pagination library. (It is bad practice to directly change the Pagination library in the system directory.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Pagination extends CI_Pagination {
public function __construct()
{
parent::__construct();
}
}
You'll then need to add the create_links() function to your MY_Pagination class, allowing you to override its default functionality. Below is an explanation of what you could change to achieve your desired output (you may want to add flexibility, by adding a parameter to the function, but this is the simplest change that I could think of.)
function create_links()
{
// You can copy the exact functionality of this function from:
// system/libraries/Pagination.php
// The line you want to change is:
// $this->base_url = rtrim($this->base_url, '/') .'/';
// Changing to this: $this->base_url = rtrim($this->base_url, '/') .'';
// Will create links in this format: ../page5
// Or changing to this: $this->base_url = rtrim($this->base_url, '/') .'-';
// Will create links in this format: ../page-5
}

how to debug opencart project ? for example putting break points step into code etc?

I'm new one to opencart.is there any debug tools available for opencart ? .i don't know control flow of opencart execution.so i want to put break points,step into code,see variable values. please give any reference to that .thanks in advance.
I wrote a super simple little function for the loader class that I use 100 times a day. It really helps and you can call it from just about anywhere.
OPEN:
system/engine/loader.php
Right before the closing brace for the class add this method:
// adding testing method
public function test ($items, $quit = true) {
echo "<pre>";
print_r ($items);
echo "</pre>";
if ($quit):
exit;
endif;
}
Now anytime after the Controller is instantiated you can call:
$this->load->test($results);
OR:
$this->load->test($results, false);
if you're in a loop and don't want the script to exit.
Obviously substitute $results for whatever array or variable you want to test.
It's been a huge help to me.
You can of course add this via vqmod if you don't want to modify the core.
You are right. Opencart is very simple system.
In addition you can use xDebug - very useful tool.
Also, read system/logs/error.txt
error_reporting(E_ALL); // very helpful
die(print_r($_POST, true)); // print all POST data and break the code
you can use https://github.com/mithereal/opencart_inline_debuggers and just d($var); in the source where var is a varible or object

Magento Tables and how they work with the database

I been trying to understand Magento and I read many things on the wiki, but I couldn't figure out How does Magento works with database tables? because I didn't see any SQL
I would reccomend read over this blog post from Alan Storm:
http://alanstorm.com/magento_models_orm
He explains quite abit about the Magento ORM system, and in my opinion that entire site is a great resource for any Magneto developer.
If you watch your MySQl log, the calls made by magento can sometimes be 500 lines long or longer ... These calls are dynamically constructed using XML files. The best way to manipulate Magento data manually is to use MAGE:: calls or use a direct database connection by using:
$read = $resource->getConnection('core_read');
$sql = "select * from [YOUR_TABLE] where 1 limit 1";
$result = $read->query($sql);
It's either that or calls that look like:
$value = 'some value';
$item->setData('some_key', $value);
$item->save();
Magento is object oriented, so those are the most commonly accepted and used ways to retrieve/set data in Magento. I hope that helps.
Read chapter 5 onwards from the knowledge base.
You are not really asking a question so no one can help on the specifics, I always find that you learn best by doing, I find the best way to mess around with magento is to create a test.php file in shell/ with the following: (for example)
<?php
require('abstract.php');
class Test extends Mage_Shell_Abstract
{
function run(){ //call your functions here
echo 'running ..';
$this->database();
}
function database() { //you can create as many functions as you like
$entityId = '4449'; //product id
$product=Mage::getModel("catalog/product")->load($entityId);
var_dump($product->getAttributeText('size'));
}
}
$test = new Test();
$test -> run();
Then you can run from console:
php test.php
and it returns in my example
running ..string(11) "Extra Large"
Hope this helps you, next time be more specific.

Global variables in CodeIgniter view

I am following the Nettuts tutorial to implement Basecamp-style subdomains with CodeIgniter.
Based on the calling subdomain, the subdomain table in the database returns an extension corresponding to that subdomain. So say for stackoverflow.mywebsite.com, it will return the extension sf, and the image folder and CSS file used all over the website will change on the basis of this extension; for example, images_sf, style_sf.css, etc.
Now, what is the best way to fetch this extension anywhere in M, V, or C ?
Options:
Cookies
Dynamically setting CI config variable
Set a variable for this in MY_Controller and access that variable via $this-> anywhere.
Send that variable from each controller to models, helpers, views or libraries.
Did I miss any other options? Also, which one will be best assuming this will be heavily used all over the code?
Thanks
Personally, I would create a base controller for each site that you extend, and just use $this->load->vars($data); to load the information you need globally set.
In the constructor of your specific base Controller just load the data into the views globally like this.
$data->some_var = "some value";
$this->load->vars($data);
And then in all your views loaded by this controller (or base controller) you can utilize the variable $some_var directly in the view.
Your best bet is probably option 3, to put it in the constructor of your base contoller, probably MY_controller or whatever you are extending Controller (now CI_Controller with the CI2.0 official release)
However if all you're doing is getting an extension, there might not be a reason to have a database table, since you could just keep it in a config file
I'd do something like this... in MY_Controller.php (This is CI 2.0 syntax)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
$this->load->config('sub_prefix');
$pre_arr = $this->config->item('prefixes');
/* Check to make sure the subdomain name is in the config array */
$this->prefix = isset($pre_arr[$subdomain_name]) ? $pre_arr[$subdomain_name] : '';
}
Then in the config file (sub_prefix.php)
<?php if (! defined('BASEPATH')) exit('No direct script access');
$config['prefixes'] = array('subdomain1' => 'sub1',
'stackoverflow' => 'sf');
That way you don't have to run an extra query everytime the page loads for something that is relatively static...if you got to the point where there would be a lot more information you would need other than a prefix, then it would make sense to do it with the database
You can now just use $this->prefix in any of the views, controllers, or models you will be using... the best way to do it for something that is heavily use all over your application.

Resources