includes/lang.php
author Dan
Sat, 27 Oct 2007 13:29:17 -0400
changeset 205 c4542792db2b
child 209 8a00247d1dee
permissions -rw-r--r--
Localization low-level framework added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     1
<?php
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     2
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     3
/*
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     4
 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     5
 * Version 1.1.1
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     6
 * Copyright (C) 2006-2007 Dan Fuhry
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     7
 *
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     8
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     9
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    10
 *
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    12
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    13
 */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    14
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    15
/**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    16
 * Language class - processes, stores, and retrieves language strings.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    17
 * @package Enano
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    18
 * @subpackage Localization
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    19
 * @copyright 2007 Dan Fuhry
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    20
 * @license GNU General Public License
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    21
 */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    22
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    23
class Language
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    24
{
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    25
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    26
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    27
   * The numerical ID of the loaded language.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    28
   * @var int
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    29
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    30
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    31
  var $lang_id;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    32
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    33
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    34
   * The ISO-639-3 code for the loaded language. This should be grabbed directly from the database.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    35
   * @var string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    36
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    37
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    38
  var $lang_code;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    39
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    40
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    41
   * Will be an object that holds an instance of the class configured with the site's default language. Only instanciated when needed.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    42
   * @var object
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    43
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    44
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    45
  var $default;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    46
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    47
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    48
   * The list of loaded strings.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    49
   * @var array
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    50
   * @access private
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    51
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    52
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    53
  var $strings = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    54
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    55
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    56
   * Constructor.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    57
   * @param int|string Language ID or code to load.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    58
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    59
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    60
  function __construct($lang)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    61
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    62
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    63
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    64
    if ( defined('IN_ENANO_INSTALL') )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    65
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    66
      // special case for the Enano installer: it will load its own strings from a JSON file and just use this API for fetching and templatizing them.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    67
      $this->lang_id   = LANG_DEFAULT;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    68
      $this->lang_code = 'neutral';
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    69
      return true;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    70
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    71
    if ( is_string($lang) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    72
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    73
      $sql_col = 'lang_code="' . $db->escape($lang) . '"';
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    74
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    75
    else if ( is_int($lang) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    76
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    77
      $sql_col = 'lang_id=' . $lang . '';
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    78
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    79
    else
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    80
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    81
      $db->_die('lang.php - attempting to pass invalid value to constructor');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    82
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    83
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    84
    $lang_default = ( $x = getConfig('default_language') ) ? intval($x) : 1;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    85
    $q = $db->sql_query("SELECT lang_id, lang_code, ( lang_id = $lang_default ) AS is_default FROM " . table_prefix . "language WHERE $sql_col OR lang_id = $lang_default ORDER BY is_default DESC LIMIT 1;");
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    86
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    87
    if ( !$q )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    88
      $db->_die('lang.php - main select query');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    89
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    90
    if ( $db->numrows() < 1 )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    91
      $db->_die('lang.php - There are no languages installed');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    92
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    93
    $row = $db->fetchrow();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    94
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    95
    $this->lang_id   = intval( $row['lang_id'] );
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    96
    $this->lang_code = $row['lang_code'];
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    97
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    98
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    99
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   100
   * PHP 4 constructor.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   101
   * @param int|string Language ID or code to load.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   102
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   103
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   104
  function Language($lang)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   105
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   106
    $this->__construct($lang);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   107
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   108
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   109
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   110
   * Fetches language strings from the database, or a cache file if it's available.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   111
   * @param bool If true (default), allows the cache to be used.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   112
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   113
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   114
  function fetch($allow_cache = true)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   115
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   116
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   117
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   118
    $lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   119
    // Attempt to load the strings from a cache file
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   120
    if ( file_exists($lang_file) && $allow_cache )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   121
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   122
      // Yay! found it
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   123
      $this->load_cache_file($lang_file);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   124
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   125
    else
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   126
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   127
      // No cache file - select and retrieve from the database
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   128
      $q = $db->sql_unbuffered_query("SELECT string_category, string_name, string_content FROM " . table_prefix . "language_strings WHERE lang_id = {$this->lang_id};");
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   129
      if ( !$q )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   130
        $db->_die('lang.php - selecting language string data');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   131
      if ( $row = $db->fetchrow() )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   132
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   133
        $strings = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   134
        do
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   135
        {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   136
          $cat =& $row['string_category'];
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   137
          if ( !is_array($strings[$cat]) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   138
          {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   139
            $strings[$cat] = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   140
          }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   141
          $strings[$cat][ $row['string_name'] ] = $row['string_content'];
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   142
        }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   143
        while ( $row = $db->fetchrow() );
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   144
        // all done fetching
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   145
        $this->merge($strings);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   146
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   147
      else
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   148
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   149
        $db->_die('lang.php - No strings for language ' . $this->lang_code);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   150
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   151
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   152
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   153
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   154
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   155
   * Loads a file from the disk cache (treated as PHP) and merges it into RAM.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   156
   * @param string File to load
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   157
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   158
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   159
  function load_cache_file($file)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   160
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   161
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   162
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   163
    // We're using eval() here because it makes handling scope easier.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   164
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   165
    if ( !file_exists($file) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   166
      $db->_die('lang.php - requested cache file doesn\'t exist');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   167
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   168
    $contents = file_get_contents($file);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   169
    $contents = preg_replace('/([\s]*)<\?php/', '', $contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   170
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   171
    @eval($contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   172
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   173
    if ( !isset($lang_cache) || ( isset($lang_cache) && !is_array($lang_cache) ) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   174
      $db->_die('lang.php - the cache file is invalid (didn\'t set $lang_cache as an array)');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   175
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   176
    $this->merge($lang_cache);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   177
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   178
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   179
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   180
   * Merges a standard language assoc array ($arr[cat][stringid]) with the master in RAM.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   181
   * @param array
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   182
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   183
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   184
  function merge($strings)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   185
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   186
    // This is stupidly simple.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   187
    foreach ( $strings as $cat_id => $contents )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   188
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   189
      if ( !is_array($this->strings[$cat_id]) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   190
        $this->strings[$cat_id] = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   191
      foreach ( $contents as $string_id => $string )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   192
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   193
        $this->strings[$cat_id][$string_id] = $string;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   194
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   195
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   196
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   197
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   198
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   199
   * Imports a JSON-format language file into the database and merges with current strings.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   200
   * @param string Path to the JSON file to load
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   201
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   202
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   203
  function import($file)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   204
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   205
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   206
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   207
    if ( !file_exists($file) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   208
      $db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   209
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   210
    $contents = trim(@file_get_contents($file));
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   211
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   212
    if ( empty($contents) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   213
      $db->_die('lang.php - can\'t load the contents of the language file');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   214
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   215
    // Trim off all text before and after the starting and ending braces
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   216
    $contents = preg_replace('/^([^{]+)\{/', '{', $contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   217
    $contents = preg_replace('/\}([^}]+)$/', '}', $contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   218
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   219
    $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   220
    $langdata = $json->decode($contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   221
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   222
    if ( !is_array($langdata) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   223
      $db->_die('lang.php - invalid language file');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   224
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   225
    if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   226
      $db->_die('lang.php - language file does not contain the proper items');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   227
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   228
    $insert_list = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   229
    $delete_list = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   230
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   231
    foreach ( $langdata['categories'] as $category )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   232
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   233
      if ( isset($langdata['strings'][$category]) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   234
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   235
        foreach ( $langdata['strings'][$category] as $string_name => $string_value )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   236
        {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   237
          $string_name = $db->escape($string_name);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   238
          $string_value = $db->escape($string_value);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   239
          $category_name = $db->escape($category);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   240
          $insert_list[] = "({$this->lang_id}, '$category_name', '$string_name', '$string_value')";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   241
          $delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   242
        }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   243
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   244
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   245
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   246
    $delete_list = implode(" OR\n  ", $delete_list);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   247
    $sql = "DELETE FROM " . table_prefix . "language_strings WHERE $delete_list;";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   248
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   249
    // Free some memory
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   250
    unset($delete_list);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   251
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   252
    // Run the query
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   253
    $q = $db->sql_query($sql);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   254
    if ( !$q )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   255
      $db->_die('lang.php - couldn\'t kill off them old strings');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   256
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   257
    $insert_list = implode(",\n  ", $insert_list);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   258
    $sql = "INSERT INTO " . table_prefix . "language_strings(lang_id, string_category, string_name, string_content) VALUES\n  $insert_list;";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   259
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   260
    // Free some memory
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   261
    unset($insert_list);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   262
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   263
    // Run the query
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   264
    $q = $db->sql_query($sql);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   265
    if ( !$q )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   266
      $db->_die('lang.php - couldn\'t insert strings in import()');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   267
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   268
    // YAY! done!
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   269
    // This will regenerate the cache file if possible.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   270
    $this->regen_caches();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   271
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   272
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   273
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   274
   * Refetches the strings and writes out the cache file.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   275
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   276
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   277
  function regen_caches()
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   278
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   279
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   280
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   281
    $lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   282
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   283
    // Refresh the strings in RAM to the latest copies in the DB
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   284
    $this->fetch(false);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   285
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   286
    $handle = @fopen($lang_file, 'w');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   287
    if ( !$handle )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   288
      // Couldn't open the file. Silently fail and let the strings come from the database.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   289
      return false;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   290
      
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   291
    // The file's open, that means we should be good.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   292
    fwrite($handle, '<?php
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   293
// This file was generated automatically by Enano. You should not edit this file because any changes you make
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   294
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   295
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   296
$lang_cache = ');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   297
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   298
    $exported = $this->var_export_string($this->strings);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   299
    if ( empty($exported) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   300
      // Ehh, that's not good
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   301
      $db->_die('lang.php - var_export_string() failed');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   302
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   303
    fwrite($handle, $exported . '; ?>');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   304
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   305
    // Done =)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   306
    fclose($handle);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   307
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   308
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   309
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   310
   * Calls var_export() on whatever, and returns the function's output.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   311
   * @param mixed Whatever you want var_exported. Usually an array.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   312
   * @return string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   313
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   314
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   315
  function var_export_string($val)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   316
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   317
    ob_start();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   318
    var_export($val);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   319
    $contents = ob_get_contents();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   320
    ob_end_clean();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   321
    return $contents;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   322
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   323
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   324
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   325
   * Fetches a language string from the cache in RAM. If it isn't there, it will call fetch() again and then try. If it still can't find it, it will ask for the string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   326
   * in the default language. If even then the string can't be found, this function will return what was passed to it.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   327
   *
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   328
   * This will also templatize strings. If a string contains variables in the format %foo%, you may specify the second parameter as an associative array in the format
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   329
   * of 'foo' => 'foo substitute'.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   330
   *
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   331
   * @param string ID of the string to fetch. This will always be in the format of category_stringid.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   332
   * @param array Optional. Associative array of substitutions.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   333
   * @return string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   334
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   335
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   336
  function get($string_id, $substitutions = false)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   337
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   338
    // Extract the category and string ID
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   339
    $category = substr($string_id, 0, ( strpos($string_id, '_') ));
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   340
    $string_name = substr($string_id, ( strpos($string_id, '_') + 1 ));
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   341
    $found = false;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   342
    if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   343
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   344
      $found = true;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   345
      $string = $this->strings[$category][$string_name];
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   346
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   347
    if ( !$found )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   348
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   349
      // Ehh, the string wasn't found. Rerun fetch() and try again.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   350
      $this->fetch();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   351
      if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   352
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   353
        $found = true;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   354
        $string = $this->strings[$category][$string_name];
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   355
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   356
      if ( !$found )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   357
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   358
        // STILL not found. Check the default language.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   359
        $lang_default = ( $x = getConfig('default_language') ) ? intval($x) : $this->lang_id;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   360
        if ( $lang_default != $this->lang_id )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   361
        {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   362
          if ( !is_object($this->default) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   363
            $this->default = new Language($lang_default);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   364
          return $this->default->get($string_id, $substitutions);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   365
        }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   366
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   367
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   368
    if ( !$found )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   369
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   370
      // Alright, it's nowhere. Return the input, grumble grumble...
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   371
      return $string_id;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   372
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   373
    // Found it!
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   374
    // Perform substitutions.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   375
    if ( !is_array($substitutions) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   376
      $substitutions = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   377
    return $this->substitute($string, $substitutions);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   378
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   379
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   380
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   381
   * Processes substitutions.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   382
   * @param string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   383
   * @param array
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   384
   * @return string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   385
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   386
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   387
  function substitute($string, $subs)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   388
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   389
    preg_match_all('/%this\.([a-z0-9_]+)%/', $string, $matches);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   390
    if ( count($matches[0]) > 0 )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   391
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   392
      foreach ( $matches[1] as $i => $string_id )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   393
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   394
        $result = $this->get($string_id);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   395
        $string = str_replace($matches[0][$i], $result, $string);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   396
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   397
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   398
    foreach ( $subs as $key => $value )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   399
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   400
      $string = str_replace("%$key%", $value, $string);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   401
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   402
    return $string;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   403
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   404
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   405
} // class Language
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   406
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   407
?>