1
<?php
2
/*
3
  Belokan Micro Framework
4
  Licensed under GNU GPL
5
  http://gitorious.org/belokan
6
  ---
7
  Belokan/Local.php:
8
  Localization system
9
*/
10
11
class Belokan_Local {
12
  
13
  static private
14
    $_instance,
15
    $_langs,
16
    $_data,
17
    $_logBelokan;
18
  
19
  public static function getInstance ()
20
  {
21
    if (self::$_instance === null)
22
      self::$_instance = new self;
23
    return self::$_instance;
24
  }
25
  
26
  public function __construct ()
27
  {
28
    $this->_langs = array();
29
    $this->_data = array();
30
    $this->_logBelokan = Belokan_Config::getInstance()->get('log', 'belokan');
31
  }
32
  
33
  public function loadLanguage ($lang, $source, $sourceType='file')
34
  {
35
    $log = Belokan_Log::getInstance();
36
    
37
    if (strtolower($sourceType) == 'file') {
38
      if (file_exists($source)) {
39
        $this->_data[$lang] = include $source;
40
        return true;
41
      } else {
42
        $source = Belokan_Config::getInstance()->get('dir', 'langs').$source;
43
        if (file_exists($source)) {
44
          $this->_data[$lang] = include $source;
45
          return true;
46
        } else {
47
          if ($this->_logBelokan)
48
            $log->add('Belokan', 'Source file for language '.$lang.' not found ('.$source.').');
49
          return false;
50
        }
51
      }
52
    } else if (strtolower($sourceType) == 'sqlite') {
53
      $base = new Belokan_Db_SQLite($source);
54
      $base->query('SELECT * FROM `lang_'.$lang.'`;')->execute();
55
      $rows = $base->getRawResults();
56
      if (sqlite_num_rows($rows)) {
57
        while ($row = sqlite_fetch_array($rows))
58
          $this->_data[$lang][$row['key']] = $row['value'];
59
        return true;
60
      } else {
61
        if ($this->_logBelokan)
62
          $log->add('Belokan', 'Retrieve an empty rowset for language '.$lang.' from the SQLite database.');
63
        return false;
64
      }
65
    } else if (strtolower($sourceType) == 'mysql') {
66
      $base = new Belokan_Db_MySQL($source);
67
      $base->query('SELECT * FROM `lang_'.$lang.'`;')->execute();
68
      $rows = $base->getRawResults();
69
      if (mysql_num_rows($rows) > 0) {
70
        while ($row = mysql_fetch_array($rows))
71
          $this->_data[$lang][$row['key']] = $row['value'];
72
        return true;
73
      } else {
74
        if ($this->_logBelokan)
75
          $log->add('Belokan', 'Retrieve an empty rowset for language '.$lang.' from the MySQL database.');
76
        return false;
77
      }
78
    }
79
  }
80
81
  public function isLoaded ($lang)
82
  {
83
    return isset($this->_data[$lang]);
84
  }
85
  
86
  public function get ($lang, $id)
87
  {
88
    if (!isset($this->_data[$lang][$id]))
89
      return null;
90
    return $this->_data[$lang][$id];
91
  }
92
  
93
}