Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding language_checker helper to deal with i18n strings

  • Loading branch information...
commit b22ae9692823636fe829b86c886fba7b8581d5f7 1 parent 918be79
Roberto Martinez authored April 09, 2012

Showing 1 changed file with 213 additions and 0 deletions. Show diff stats Hide diff stats

  1. 213  system/helpers/language_checker_helper.php
213  system/helpers/language_checker_helper.php
... ...
@@ -0,0 +1,213 @@
  1
+<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2
+/**
  3
+ * CodeIgniter
  4
+ *
  5
+ * An open source application development framework for PHP 5.1.6 or newer
  6
+ *
  7
+ * @package		CodeIgniter
  8
+ * @author		ExpressionEngine Dev Team
  9
+ * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
  10
+ * @license		http://codeigniter.com/user_guide/license.html
  11
+ * @link		http://codeigniter.com
  12
+ * @since		Version 1.0
  13
+ * @filesource
  14
+ */
  15
+
  16
+// ------------------------------------------------------------------------
  17
+
  18
+/**
  19
+ * CodeIgniter Language Checker Helpers
  20
+ *
  21
+ * @package		CodeIgniter
  22
+ * @subpackage	Helpers
  23
+ * @category	Helpers
  24
+ * @author		Roberto Martinez
  25
+ * @link		http://codeigniter.com/user_guide/helpers/form_helper.html
  26
+ *
  27
+ Sample:
  28
+ 		$this->load->helper('language_checker');
  29
+		$this->load->helper('directory');
  30
+		$struct = $map = directory_map('./application/language');		
  31
+		echo compare_language_files ( $struct );
  32
+ /
  33
+
  34
+// ------------------------------------------------------------------------
  35
+
  36
+/**
  37
+ * Compare Language Files
  38
+ *
  39
+ * Returns html string with all the language strings, ready to compare.
  40
+ *
  41
+ * @access	public
  42
+ * @param	array	array containing language directory contents
  43
+ * @return	string	tables containing all the language strings
  44
+ */
  45
+if ( ! function_exists('compare_language_files'))
  46
+{
  47
+	function compare_language_files ( $struct )
  48
+	{
  49
+		$files = get_language_files ( $struct );
  50
+		$languages = get_languages ( $struct );
  51
+		$map = map_languages ( $struct );
  52
+		$string = "";
  53
+
  54
+		//get keys by file
  55
+		foreach ( $files as $file )
  56
+		{
  57
+			$keys = get_language_lines ( $map, $file );
  58
+			
  59
+			$string .= $file;
  60
+			foreach ( $files as $file )
  61
+			{
  62
+				//create table header
  63
+				$string .= "<TABLE BORDER CELLPADDING=1><tr><td>Key</td>";
  64
+				foreach ( $languages as $language)
  65
+				{
  66
+					$string .=  "<td>".$language."</td>";
  67
+				}
  68
+				$string .= "</tr>";
  69
+				
  70
+				//create rows
  71
+				foreach ( $keys as $key )
  72
+				{
  73
+					$string .= "<tr><td>".$key."</td>";
  74
+					foreach ( $languages as $language )
  75
+					{
  76
+						if ( isset ( $map [ $language ] [ $file ] [ $key ] ) )
  77
+						{
  78
+							$string .=  "<td>".$map [ $language ] [ $file ] [ $key ]."</td>";
  79
+						}
  80
+						else
  81
+						{
  82
+							$string .=  "<td>----</td>";
  83
+						}
  84
+					}
  85
+					$string .=  "</tr>";
  86
+				}
  87
+				//end table
  88
+				$string .=  "</table>";
  89
+			}			
  90
+		}
  91
+		
  92
+		return $string;
  93
+	}
  94
+}
  95
+
  96
+/**
  97
+ * Map Languages
  98
+ *
  99
+ * Returns an array with all the languages strings: map[language][file][key].
  100
+ *
  101
+ * @access	public
  102
+ * @param	array	array containing language directory contents
  103
+ * @return	array
  104
+ */
  105
+if ( ! function_exists('map_languages'))
  106
+{
  107
+	function map_languages ( $struct )
  108
+	{
  109
+		$languages = get_languages ( $struct );
  110
+		$files = get_language_files ( $struct );
  111
+		$map = array ();
  112
+
  113
+		foreach ( $files as $file)
  114
+		{
  115
+			foreach ( $languages as $language )
  116
+			{
  117
+				$path = 'application/language/'.$language.'/'.$file;
  118
+				if ( file_exists ( $path ) )
  119
+				{
  120
+					include_once ( $path );					
  121
+					$map [ $language ] [ $file ] = $lang; 
  122
+					unset ( $lang );
  123
+				}				
  124
+			}
  125
+		}
  126
+		
  127
+		return  $map;
  128
+	}
  129
+}
  130
+
  131
+/**
  132
+ * Get Languages
  133
+ *
  134
+ * Returns an array with all the languages
  135
+ *
  136
+ * @access	public
  137
+ * @param	array	array containing language directory contents
  138
+ * @return	array
  139
+ */
  140
+ if ( ! function_exists('get_languages'))
  141
+{
  142
+	function get_languages ( $struct )
  143
+
  144
+	{
  145
+		$languages = array ();
  146
+		foreach ( $struct as $key => $language )
  147
+		{			
  148
+			array_push ( $languages, $key);
  149
+		}
  150
+		return $languages ;
  151
+	}	
  152
+}
  153
+	
  154
+/**
  155
+ * Get Language Files
  156
+ *
  157
+ * Returns an array with all the languages files
  158
+ *
  159
+ * @access	public
  160
+ * @param	array	array containing language directory contents
  161
+ * @return	array
  162
+ */
  163
+ if ( ! function_exists('get_language_files'))
  164
+{
  165
+	function get_language_files ( $struct )
  166
+	//----------------------------------
  167
+	{
  168
+		$files = array ();
  169
+		foreach ( $struct as $key => $language )
  170
+		{			
  171
+			foreach ( $language as $file)
  172
+			{
  173
+				if ( !in_array ( $file, $files ) AND strpos ( $file, '_lang.php' ) )
  174
+				{	array_push ( $files, $file);}
  175
+			}
  176
+		}
  177
+		return $files ;
  178
+	}
  179
+}
  180
+	
  181
+/**
  182
+ * Get Language Lines
  183
+ *
  184
+ * Returns an array with all the languages keys 
  185
+ *
  186
+ * @access	public
  187
+ * @param	array	array containing language directory contents
  188
+ * @param	string	contains name of a language file that will be accesed in different languages
  189
+ * @return	array
  190
+ */
  191
+if ( ! function_exists('get_language_lines'))
  192
+{
  193
+	function get_language_lines ( $map, $the_file )
  194
+	//----------------------------------
  195
+	{
  196
+		$keys = array ();
  197
+		foreach ( $map as $key => $language )
  198
+		{			
  199
+			foreach ( $language as $file => $lines )
  200
+			{
  201
+				if ( $file == $the_file )
  202
+				{
  203
+					foreach ( $lines as $key2 => $line )
  204
+					{
  205
+						if ( !in_array ( $key2, $keys ) )
  206
+						{	array_push ( $keys, $key2 );}
  207
+					}
  208
+				}
  209
+			}
  210
+		}
  211
+		return $keys ;
  212
+	}	
  213
+}

0 notes on commit b22ae96

Please sign in to comment.
Something went wrong with that request. Please try again.