b2evolution PHP Cross Reference Blogging Systems

Source: /inc/locales/model/_translation.funcs.php - 343 lines - 10116 bytes - Summary - Text - Print

Description: This file implements functions for handling translation. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**

   3   * This file implements functions for handling translation.

   4   *

   5   * This file is part of the evoCore framework - {@link http://evocore.net/}

   6   * See also {@link http://sourceforge.net/projects/evocms/}.

   7   *

   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}

   9   *

  10   * @package evocore

  11   *

  12   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  13   * @author fplanque: Francois PLANQUE.

  14   *

  15   * @version $Id: _translation.funcs.php 985 2012-03-05 21:59:17Z sam2kb $

  16   */
  17  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  18  
  19  
  20  /**

  21   * Update a table T_i18n_original_string from the file messages.pot

  22   *

  23   * @param boolean TRUE - if process is OK

  24   */
  25  function translation_update_table_pot()
  26  {
  27      global $DB, $locales_path;
  28  
  29      // Reset all previous strings

  30      $DB->query( 'UPDATE T_i18n_original_string SET iost_inpotfile = 0' );
  31  
  32      $status = '-';
  33  
  34      $lines = file( $locales_path.'messages.pot' );
  35      $lines[] = '';    // Adds a blank line at the end in order to ensure complete handling of the file

  36  
  37      foreach( $lines as $line )
  38      {
  39          if( trim( $line ) == '' )
  40          {    // Blank line, go back to base status:
  41              if( $status == 't' && !empty( $msgid ) )
  42              {    // ** End of an original text ** :
  43                  translation_update_table_pot_row( trim( $msgid ) );
  44              }
  45              $msgid = '';
  46              $msgstr = '';
  47              $status = '-';
  48          }
  49          elseif( ( $status == '-' ) && preg_match( '#^msgid "(.*)"#', $line, $matches ) )
  50          {    // Encountered an original text
  51              $status = 'o';
  52              $msgid = $matches[1];
  53          }
  54          elseif( ( $status == 'o' ) && preg_match( '#^msgstr "(.*)"#', $line, $matches ) )
  55          {    // Encountered a translated text
  56              $status = 't';
  57              $msgstr = $matches[1];
  58          }
  59          elseif( preg_match( '#^"(.*)"#', $line, $matches ) )
  60          {    // Encountered a followup line
  61              if( $status == 'o' )
  62                  $msgid .= $matches[1];
  63              elseif( $status == 't' )
  64                  $msgstr .= $matches[1];
  65          }
  66      }
  67  
  68      return true;
  69  }
  70  
  71  
  72  /**

  73   * Update/Insert a string from .POT file into the table T_i18n_original_string

  74   *

  75   * @param string Original string

  76   */
  77  function translation_update_table_pot_row( $string )
  78  {
  79      global $DB;
  80  
  81      // Get original string ID

  82      $SQL = new SQL();
  83      $SQL->SELECT( 'iost_ID' );
  84      $SQL->FROM( 'T_i18n_original_string' );
  85      $SQL->WHERE( 'iost_string = '.$DB->quote( $string ) );
  86      $original_string_ID = $DB->get_var( $SQL->get() );
  87  
  88      if( $original_string_ID )
  89      {    // Update already existing string
  90          $DB->query( 'UPDATE T_i18n_original_string SET iost_inpotfile = 1 WHERE iost_ID = '.$DB->quote( $original_string_ID ) );
  91      }
  92      else
  93      {    // Insert new string
  94          $DB->query( 'INSERT INTO T_i18n_original_string ( iost_string, iost_inpotfile ) VALUES ( '.$DB->quote( $string ).', 1 )' );
  95      }
  96  }
  97  
  98  
  99  /**

 100   * Update a table T_i18n_translated_string from the file messages.pot

 101   *

 102   * @param string Locale

 103   * @param boolean TRUE - if process is OK

 104   */
 105  function translation_update_table_po( $locale )
 106  {
 107      global $DB, $locales_path, $locales;
 108  
 109      $po_file_name = $locales_path.$locales[$locale]['messages'].'/LC_MESSAGES/messages.po';
 110  
 111      // Reset all previous strings

 112      $DB->query( 'UPDATE T_i18n_translated_string SET itst_inpofile = 0 WHERE itst_locale = '.$DB->quote( $locale ) );
 113  
 114      $status = '-';
 115  
 116      if( !file_exists( $po_file_name ) )
 117      {    // No locale file, Exit here
 118          global $Messages;
 119          $Messages->add( T_('No found .PO file'), 'error' );
 120          return false;
 121      }
 122  
 123      $lines = file( $po_file_name );
 124      $lines[] = '';    // Adds a blank line at the end in order to ensure complete handling of the file

 125  
 126      foreach( $lines as $line )
 127      {
 128          if( trim( $line ) == '' )
 129          {    // Blank line, go back to base status:
 130              if( $status == 't' && !empty( $msgstr ) )
 131              {    // ** End of an original text ** :
 132                  translation_update_table_po_row( $locale, trim( $msgid ), trim( $msgstr ) );
 133              }
 134              $msgid = '';
 135              $msgstr = '';
 136              $status = '-';
 137          }
 138          elseif( ( $status == '-' ) && preg_match( '#^msgid "(.*)"#', $line, $matches ) )
 139          {    // Encountered an original text
 140              $status = 'o';
 141              $msgid = $matches[1];
 142          }
 143          elseif( ( $status == 'o' ) && preg_match( '#^msgstr "(.*)"#', $line, $matches ) )
 144          {    // Encountered a translated text
 145              $status = 't';
 146              $msgstr = $matches[1];
 147          }
 148          elseif( preg_match( '#^"(.*)"#', $line, $matches ) )
 149          {    // Encountered a followup line
 150              if( $status == 'o' )
 151                  $msgid .= $matches[1];
 152              elseif( $status == 't' )
 153                  $msgstr .= $matches[1];
 154          }
 155      }
 156  
 157      return true;
 158  }
 159  
 160  
 161  /**

 162   * Update/Insert a string from .PO file into the table T_i18n_translated_string

 163   *

 164   * @param string Locale

 165   * @param string Original string

 166   * @param string Translated string

 167   */
 168  function translation_update_table_po_row( $locale, $original_string, $translated_string )
 169  {
 170      global $DB;
 171  
 172      // Get original string ID

 173      $SQL = new SQL();
 174      $SQL->SELECT( 'iost_ID' );
 175      $SQL->FROM( 'T_i18n_original_string' );
 176      $SQL->WHERE( 'iost_string = '.$DB->quote( $original_string ) );
 177      $original_string_ID = $DB->get_var( $SQL->get() );
 178  
 179      if( !$original_string_ID )
 180      {    // No original string, Exit here
 181          return;
 182      }
 183  
 184      // Get translated string

 185      $SQL = new SQL();
 186      $SQL->SELECT( 'itst_ID' );
 187      $SQL->FROM( 'T_i18n_translated_string' );
 188      $SQL->WHERE( 'itst_standard = '.$DB->quote( $translated_string ) );
 189      $SQL->WHERE_and( 'itst_iost_ID = '.$DB->quote( $original_string_ID ) );
 190      $translated_string_ID = $DB->get_var( $SQL->get() );
 191  
 192      if( $translated_string_ID )
 193      {    // Update already existing string
 194          $DB->query( 'UPDATE T_i18n_translated_string SET itst_inpofile = 1 WHERE itst_ID = '.$DB->quote( $translated_string_ID ) );
 195      }
 196      else
 197      {    // Insert new string
 198          $DB->query( 'INSERT INTO T_i18n_translated_string ( itst_iost_ID, itst_locale, itst_standard, itst_inpofile ) VALUES ( '.$DB->quote( $original_string_ID ).', '.$DB->quote( $locale ).', '.$DB->quote( $translated_string ).', 1 )' );
 199      }
 200  }
 201  
 202  
 203  /**

 204   * Generate .PO file

 205   *

 206   * @param string Locale

 207   */
 208  function translation_generate_po_file( $locale )
 209  {
 210      global $DB, $locales_path, $locales;
 211  
 212      $po_folder_name = $locales_path.$locales[$locale]['messages'].'/LC_MESSAGES/';
 213      $po_file_name = $po_folder_name.'messages.po';
 214  
 215      if( !file_exists( $po_file_name ) )
 216      {
 217          if( !file_exists( $locales_path.$locales[$locale]['messages'] ) )
 218          {
 219              evo_mkdir( $locales_path.$locales[$locale]['messages'] );
 220          }
 221          if( !file_exists( $locales_path.$locales[$locale]['messages'].'/LC_MESSAGES' ) )
 222          {
 223              evo_mkdir( $locales_path.$locales[$locale]['messages'].'/LC_MESSAGES' );
 224          }
 225      }
 226  
 227      $locale_name = explode( ' ', $locales[$locale]['name'] );
 228  
 229      $po_content = array();
 230      $po_content[] = '# b2evolution - '.$locale_name[0].' language file';
 231      $po_content[] = '# Copyright (C) '.date( 'Y' ).' Francois PLANQUE';
 232      $po_content[] = '# This file is distributed under the same license as the b2evolution package.';
 233      $po_content[] = '';
 234  
 235      // Get the translated strings from DB

 236      $SQL = new SQL();
 237      $SQL->SELECT( 'iost_string, itst_standard' );
 238      $SQL->FROM( 'T_i18n_original_string' );
 239      $SQL->FROM_add( 'RIGHT OUTER JOIN T_i18n_translated_string ON iost_ID = itst_iost_ID' );
 240      $SQL->WHERE( 'itst_locale = '.$DB->quote( $locale ) );
 241      $SQL->ORDER_BY( 'iost_string' );
 242      $translated_strings = $DB->get_results( $SQL->get() );
 243  
 244      foreach( $translated_strings as $string )
 245      {
 246          $po_content[] = 'msgid "'.$string->iost_string.'"';
 247          $po_content[] = 'msgstr "'.$string->itst_standard.'"';
 248          $po_content[] = '';
 249      }
 250  
 251      // Write to .PO file

 252      $ok = save_to_file( implode("\r\n", $po_content), $po_file_name, 'w+' );
 253  
 254      return (bool) $ok;
 255  }
 256  
 257  
 258  /**

 259   * Generate .POT file

 260   */
 261  function translation_generate_pot_file()
 262  {
 263      global $DB, $locales_path;
 264  
 265      $pot_file_name = $locales_path.'messages.pot';
 266  
 267      $pot_content = array();
 268      $pot_content[] = '# b2evolution - Language file';
 269      $pot_content[] = '# Copyright (C) '.date( 'Y' ).' Francois PLANQUE';
 270      $pot_content[] = '# This file is distributed under the same license as the b2evolution package.';
 271      $pot_content[] = '';
 272  
 273      global $basepath;
 274      $translation_strings = array();
 275      translation_scandir( $basepath, $translation_strings );
 276  
 277      foreach( $translation_strings as $string => $files )
 278      { // Format the translation strings to write in .POT file
 279          foreach( $files as $file )
 280          {
 281              $pot_content[] = '#: '.$file;
 282          }
 283          $pot_content[] = 'msgid "'.$string.'"';
 284          $pot_content[] = 'msgstr ""';
 285          $pot_content[] = '';
 286      }
 287  
 288      // Write to .POT file

 289      $ok = save_to_file( implode("\r\n", $pot_content), $pot_file_name, 'w+' );
 290  
 291      return (bool) $ok;
 292  }
 293  
 294  
 295  /**

 296   * Scan dir to find the translation strings

 297   *

 298   * @param string Path

 299   * @param array Translation strings (by reference)

 300   */
 301  function translation_scandir( $path, & $translation_strings )
 302  {
 303      $files = scandir( $path );
 304      foreach( $files as $file )
 305      {
 306          if( is_file( $path.$file ) && preg_match( '/\.php$/i', $path.$file ) )
 307          {    // PHP file; Find all translation strings in current file
 308              translation_find_T_strings( $path.$file, $translation_strings );
 309          }
 310          elseif( $file != '.' && $file != '..' && is_dir( $path.$file ) )
 311          {    // Directory; Scan each directory recursively to find all PHP files
 312              translation_scandir( $path.$file.'/', $translation_strings );
 313          }
 314      }
 315  }
 316  
 317  
 318  /**

 319   * Find the translation strings in the file

 320   *

 321   * @param string File path

 322   * @param array Translation strings (by reference)

 323   */
 324  function translation_find_T_strings( $file, & $translation_strings )
 325  {
 326      if( preg_match_all( '/(NT|T|TS)_\( ?\'([^\)]+)\' ?\)/i', file_get_contents( $file ), $t_matches ) )
 327      {    // The matches is found
 328          global $basepath;
 329  
 330          foreach( $t_matches[2] as $t_m )
 331          {
 332              $t_m = str_replace( array( "\r\n", '\n' ), array( '\n', '\n"'."\r\n".'"' ), $t_m );
 333  
 334              if( !isset( $translation_strings[ $t_m ] ) )
 335              {    // Set array for each string in order to store the file paths where this string is found
 336                  $translation_strings[ $t_m ] = array();
 337              }
 338              $translation_strings[ $t_m ][] = str_replace( $basepath, '/', $file );
 339          }
 340      }
 341  }
 342  
 343  ?>

title

Description

title

Description

title

Description

title

title

Body