wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.I18nUtil.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.I18nUtil.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/util/class.Message.php");
20 require_once(BASE."wcmf/lib/util/class.InifileParser.php");
21 
22 /**
23  * @class I18nUtil
24  * @ingroup Util
25  * @brief I18nUtil provides support i18n functionality.
26  *
27  * @author ingo herwig <ingo@wemove.com>
28  */
29 class I18nUtil
30 {
31  var $_errorMsg = '';
32 
33  /**
34  * Get last error message.
35  * @return The error string
36  */
37  function getErrorMsg()
38  {
39  return $this->_errorMsg;
40  }
41  /**
42  * Get all messages from a directory recursively.
43  * @param directory The directory to search in
44  * @param pattern The pattern the names of the files to search in must match
45  * @param depth Internal use only
46  * @result An assoziative array with the filenames as keys and the values as array of strings.
47  * @see I18nUtil::getMessagesFromFile
48  */
49  function getMessages($directory, $pattern, $depth=0)
50  {
51  static $result = array();
52  static $baseDir = '';
53  if ($depth == 0)
54  $baseDir = $directory;
55 
56  if (substr($directory, -1) != '/')
57  $directory .= '/';
58  if (is_dir($directory))
59  {
60  $d = dir($directory);
61  $d->rewind();
62  while(false !== ($file = $d->read()))
63  if($file != '.' && $file != '..')
64  {
65  if (is_dir($directory.$file))
66  $this->getMessages($directory.$file, $pattern, ++$depth);
67  elseif (preg_match($pattern, $file))
68  {
69  $messages = $this->getMessagesFromFile($directory.$file);
70  if (sizeof($messages) > 0)
71  {
72  $key = str_replace($baseDir, '', $directory.$file);
73  $result[$key] = $messages;
74  }
75  }
76  }
77  $d->close();
78  }
79  else
80  $this->_errorMsg = "The directory '".$directory."' does not exist.";
81  return $result;
82  }
83  /**
84  * Get all messages from a file. Searches for parameters of the Message::get method
85  * and usage of the smarty 'translate' function.
86  * @param file The file to search in
87  * @result An array of strings.
88  * @note This method searches for occurences of 'Message::get("...")', 'Message::get('...')'
89  * and 'translate text="..."' where '...' is supposed to be the message to translate.
90  * So it might not find the usage of the Message::get() method with concatenated strings
91  * (like Message::get($login." says hello")).
92  * But this usage is not recommended anyway (see Message::get()).
93  */
94  function getMessagesFromFile($file)
95  {
96  $result = array();
97  if (file_exists($file))
98  {
99  $fh = fopen($file, "r");
100  $content = fread($fh, filesize ($file));
101  fclose($fh);
102  preg_match_all('/Message->get\(([\'"])(.*?)\\1|Message::get\(([\'"])(.*?)\\3|translate.*? text=([\'"])(.*?)\\5/i', $content, $matchesTmp);
103  $matches = array();
104  // filter out empty and duplicates
105  foreach(array_merge($matchesTmp[2], $matchesTmp[4], $matchesTmp[6]) as $match)
106  if ($match != '' && !in_array($match, $matches))
107  array_push($matches, $match);
108  if (sizeof($matches) > 0)
109  $result = $matches;
110  }
111  return $result;
112  }
113  /**
114  * Create a message catalog (*.PO file) for use with 'gettext'. The file will be created in
115  * the directory 'localeDir/language/LC_MESSAGES/' where 'localeDir' must be given in the configuration
116  * file section 'cms'.
117  * @param projectID The name of the project
118  * @param teamName The name of the translation team
119  * @param teamEmail The email of the translation team
120  * @param language The language of the file (language code e.g. 'de')
121  * @param country The country of the file (country code e.g. 'DE')
122  * @param charset The charset used (e.g. 'iso-8859-1')
123  * @param filename The name of the file (The Message::get method uses 'main' per default)
124  * @param messages An assoziative array with the messages as keys (becomes 'msgid' in the *.PO file)
125  * and assoziative array values with keys 'translation' (becomes 'msgstr' in the *.PO file), 'files' (becomes the reference comment)
126  * @result True/False whether successful or not.
127  */
128  function createPOFile($projectID, $teamName, $teamEmail, $language, $country, $charset, $filename, $messages)
129  {
130  // get locale directory
131  $parser = &InifileParser::getInstance();
132  if (($localDir = $parser->getValue('localeDir', 'cms')) === false)
133  {
134  $this->_errorMsg = $parser->getErrorMsg();
135  return false;
136  }
137  if (substr($localDir, -1) != '/')
138  $localDir .= '/';
139 
140  $languageCode = $language.'_'.$country;
141  $directory = $localDir.$languageCode.'/LC_MESSAGES/';
142  if (!file_exists($localDir))
143  mkdir($localDir);
144  if (!file_exists($localDir.$languageCode))
145  mkdir($localDir.$languageCode);
146  if (!file_exists($directory))
147  mkdir($directory);
148 
149  $file = $directory.$filename.'.po';
150 
151  // backup old file
152  if (file_exists($file))
153  rename($file, $file.".bak");
154 
155  $fh = fopen($file, "w");
156 
157  // write header
158  $header = 'msgid ""'."\n";
159  $header .= 'msgstr ""'."\n";
160  $header .= '"Project-Id-Version: '.$projectID.'\n"'."\n";
161  $header .= '"POT-Creation-Date: '.date("Y-m-d H:iO", mktime()).'\n"'."\n";
162  $header .= '"PO-Revision-Date: '.date("Y-m-d H:iO", mktime()).'\n"'."\n";
163  $header .= '"Last-Translator: '.$teamName.' <'.$teamEmail.'>\n"'."\n";
164  $header .= '"Language-Team: '.$teamName.' <'.$teamEmail.'>\n"'."\n";
165  $header .= '"MIME-Version: 1.0\n"'."\n";
166  $header .= '"Content-Type: text/plain; charset='.$charset.'\n"'."\n";
167  $header .= '"Content-Transfer-Encoding: 8bit\n"'."\n";
168 
169  fwrite($fh, $header."\n");
170 
171  // write messages
172  foreach($messages as $message => $attributes)
173  fwrite($fh, '#: '.$attributes['files']."\n".'msgid "'.$message.'"'."\n".'msgstr "'.$attributes['translation'].'"'."\n\n");
174 
175  fclose($fh);
176  }
177 }
178 ?>
getMessagesFromFile($file)
I18nUtil provides support i18n functionality.
createPOFile($projectID, $teamName, $teamEmail, $language, $country, $charset, $filename, $messages)
getMessages($directory, $pattern, $depth=0)