How to Create Codeigniter Language Files from Database

How to create Codeigniter language files from database?

You are on the right track. You’ll want to create a language file on the fly (e.g. whenever you update the language contents of your database)

1st: the database layout

Create a table lang_token with columns id, category, description, lang, token and populate its fields like this:

    CREATE TABLE IF NOT EXISTS `lang_token` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` text NOT NULL,
`description` text NOT NULL,
`lang` text NOT NULL,
`token` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `lang_token` (`id`, `category`, `description`, `lang`, `token`)
VALUES
(1, 'error', 'noMail', 'english', 'You must submit a valid email address'),
(2, 'error', 'noUser', 'english', 'You must submit a username');

2nd: About CodeIgniter language files

CodeIgniter will look first in your application/language directory, Each language should be stored in its own folder. Make sure you have your English or German, etc. subdirectories created e.g. application/language/english

3rd: Controller function to create language file on the fly

About The Codeigniter language files:
It's a good practice to use a common prefix (category) for all messages in a given file to avoid collisions with similarly named items in other files
There structure is like: $lang['category_description'] = “token”;

    function updatelangfile($my_lang){
$this->db->where('lang',$my_lang);
$query=$this->db->get('lang_token');

$lang=array();
$langstr="<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
*
* Created: 2014-05-31 by Vickel
*
* Description: ".$my_lang." language file for general views
*
*/"."\n\n\n";

foreach ($query->result() as $row){
//$lang['error_csrf'] = 'This form post did not pass our security checks.';
$langstr.= "\$lang['".$row->category."_".$row->description."'] = \"$row->token\";"."\n";
}
write_file('./application/language/'.$my_lang.'/general_lang.php', $langstr);

}

Final notes:

  1. Whenever you change your database, you’ll call the function updatelangfile(‘english’)
  2. Don’t forget to load the file helper and language class in the constructor of the controller where updatelangfile() is located:

    function __construct(){
    parent::__construct();
    $this->load->helper('file');
    $this->lang->load('general', 'english');
    }

CodeIgniter - Store and read Language from database.

You’ll want to create a language file on the fly (e.g. whenever you update the language contents of your database)

1st: the database layout

Create a table lang_token with columns id, category, description, lang, token and populate its fields like this:

    CREATE TABLE IF NOT EXISTS `lang_token` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` text NOT NULL,
`description` text NOT NULL,
`lang` text NOT NULL,
`token` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `lang_token` (`id`, `category`, `description`, `lang`, `token`)
VALUES
(1, 'list', 'add', 'english', 'add to list'),
(2, 'list', 'remove', 'english', 'remove from list');

2nd: About CodeIgniter language files

CodeIgniter will look first in your application/language directory, Each language should be stored in its own folder. Make sure you have your English or German, etc. subdirectories created e.g. application/language/english

3rd: Controller function to create language file on the fly

About The Codeigniter language files:
It's a good practice to use a common prefix (category) for all messages in a given file to avoid collisions with similarly named items in other files
There structure is like: $lang['category_description'] = “token”;

    function updatelangfile($my_lang){
$this->db2->where('lang',$my_lang);
$query=$this->db2->get('lang_token');

$lang=array();
$langstr="<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
*
* Created: 2014-05-31 by Vickel
*
* Description: ".$my_lang." language file for general views
*
*/"."\n\n\n";

foreach ($query->result() as $row){

$langstr.= "\$lang['".$row->category."_".$row->description."'] = \"$row->token\";"."\n";
}
write_file('./application/language/'.$my_lang.'/general_lang.php', $langstr);

}

Final notes:

  1. Whenever you change your database, you’ll call the function updatelangfile(‘english’)
  2. Don’t forget to load the file helper and language class in the constructor of the controller where updatelangfile() is located:

    function __construct(){
    parent::__construct();
    $this->load->helper('file');
    $this->lang->load('general', 'english');
    }

Codeigniter : Multilanguage using database and language files

A bit late, but Language Class Extended DB is a very good solution.

How to call language from database in codeigniter and call with foreach from language library message?

//controller

function function_name(){
$data=array();
$this->load->model('model_name');
$data['languageArray']=$this->model_name->get_language();
//load view file
$this->load->view('folder/file_name',$data);
}

//model

function get_language(){
return $this->db->get('table_name')->result_array();
}

//view

<select class="form-control" name="language_id" id ="language_id">
<option value="">Select Language</option>
<?php foreach ($languageArray as $language) { ?>
<option value="<?php echo $language['id'] ?>">
<?php echo $language['lang'] ?></option>
<?php } ?>
</select>

the best way to make codeigniter website multi-language. calling from lang arrays depends on lang session?

Have you seen CodeIgniter's Language library?

The Language Class provides functions
to retrieve language files and lines
of text for purposes of internationalization.

In your CodeIgniter system folder you'll
find one called language containing sets
of language files. You can create your
own language files as needed in order
to display error and other messages in
other languages.

Language files are typically stored in
your system/language directory. Alternately
you can create a folder called language
inside your application folder and store
them there. CodeIgniter will look first
in your application/language directory.
If the directory does not exist or the
specified language is not located there
CI will instead look in your global
system/language folder.

In your case...

  • you need to create a polish_lang.php and english_lang.php inside application/language/polish
  • then create your keys inside that file (e.g. $lang['hello'] = "Witaj";
  • then load it in your controller like $this->lang->load('polish_lang', 'polish');
  • then fetch the line like $this->lang->line('hello'); Just store the return value of this function in a variable so you can use it in your view.

Repeat the steps for the english language and all other languages you need.

CodeIgniter: Language file editor?

I found this:

http://blog.codebusters.pl/en/entry/codeigniter-frontend-language-files-editor

Hope this helps
:)



Related Topics



Leave a reply



Submit