Welcome Guest, Not a member yet? Register   Sign In
A better language library
#1

[eluser]Negligence[/eluser]
I've decided that I should probably start sharing the libraries I've created that I find very useful. So, I'll start with my Language library. Here's a few reasons why it's better than the native version.

- Developing multi-lingual sites, the prospect of doing $this->lang->line(); for hundreds of language strings was daunting. With this library, one call gets all your language strings, or individual ones if you like.
- You can create a set of "common" language strings that you reuse throughout your application, so you aren't repeating strings in different files.
- You do not have to separate between different languages with folders. Instead, you put related language strings in a single file, and using arrays, you distinguish between the different language versions of a string.
- You can switch between languages on-the-fly.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* If you don't mind, keep this block of text up here if you use this library. Thanks!
*
* Author: Scott Winfield
* http://www.palidia.com */

class Lang
{
     private $defaultLang = 'en';
     private $language = 'en';
     private $positions = array('en' => 0, 'fr' => 1); // Position of the language string within the array
     private $varPrefix = 'lang_';
     private $data;
    
    
     /* Get all the language strings from a file */
     public function getKeys ( $file, $language = null )
     {
         if (isset($language))
         {
             $this->setLanguage($language);
         }    

         require(APPPATH . 'language/' . $file . '.php');
        
         $this->getLangStrings($lang);
         return $this->data;
     }
    
    
     /* Get all the commonly-used language strings */
     public function getCommonKeys()
     {
         require(APPPATH . 'language/Common.php');
        
         $this->getLangStrings($lang);
         return $this->data;
     }
    
    
     /* Get a specific key from a file */
     public function getKey ( $key, $file )
     {
         require(APPPATH . 'language/' . $file . '.php');
         return $this->getCorrectString($lang[$key]);
     }
    
    
     public function setLanguage ( $language )
     {
         $this->language = $language;
     }
    
    
     public function getLanguage()
     {
         return $this->language;
     }
    
    
     /* Walk through the $lang var and pull out the strings */
     private function getLangStrings ( $lang )
     {
          foreach ($lang AS $name => $strings)
         {
             $this->data[$this->varPrefix . $name] = $this->getCorrectString($strings);
         }        
     }
    
    
     /* Get the correct, language-sensitive string */
     private function getCorrectString ( $strings )
     {
         // Make sure you've got a valid string in the current language
         if (isset($strings[$this->positions[$this->language]]))
         {
             return $strings[$this->positions[$this->language]];    
         }
        
          // Otherwise, return the default language string        
         return $strings[$this->positions[$this->defaultLang]];
     }
}

?>

Usage:
Code:
// Load the library
$this->load->library('Lang');
Code:
// Getting all language strings
$strings = $this->lang->getKeys('filename', 'optional language');
Code:
// Get common language strings
$strings = $this->lang->getCommonKeys();
Code:
// Change the current language
$this->lang->setLanguage('en');


And your language file would look like this (under application/language/file.php):
Code:
$lang['email'] = array('English string', 'French String', 'Spanish String', 'etc');
$lang['phone'] = array('Phone', 'Phone', 'etc.');

Now you can have your translated strings side-by-side for easy editing and viewing. If you have any questions, let me know. Otherwise, enjoy Smile


Messages In This Thread
A better language library - by El Forum - 12-19-2007, 12:08 PM
A better language library - by El Forum - 12-20-2007, 01:01 AM
A better language library - by El Forum - 12-20-2007, 05:47 AM
A better language library - by El Forum - 12-20-2007, 06:41 AM
A better language library - by El Forum - 12-20-2007, 07:52 AM
A better language library - by El Forum - 12-20-2007, 08:25 AM
A better language library - by El Forum - 12-20-2007, 08:59 AM
A better language library - by El Forum - 12-20-2007, 09:20 AM
A better language library - by El Forum - 12-20-2007, 12:55 PM
A better language library - by El Forum - 12-20-2007, 01:10 PM
A better language library - by El Forum - 12-20-2007, 02:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB