2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
37 class LanguageManager{
40 * Called from VardefManager to allow for caching a lang file for a module
41 * @param module - the name of the module we are working with
42 * @param templates - an array of templates this module uses
44 function createLanguageFile($module , $templates=array('default'), $refresh = false){
45 global $mod_strings, $current_language;
46 if(!empty($GLOBALS['sugar_config']['developerMode']) || !empty($_SESSION['developerMode'])){
49 $temp_mod_strings = $mod_strings;
50 $lang = $current_language;
52 $lang = $GLOBALS['sugar_config']['default_language'];
53 static $createdModules = array();
54 if(empty($createdModules[$module]) && ($refresh || !file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'.$module.'/language/'.$lang.'.lang.php'))){
55 $loaded_mod_strings = array();
56 $loaded_mod_strings = LanguageManager::loadTemplateLanguage($module , $templates, $lang , $loaded_mod_strings);
57 $createdModules[$module] = true;
58 LanguageManager::refreshLanguage($module,$lang, $loaded_mod_strings);
63 * Load the module tempalte lauguage files
64 * @param module - the name of the module we are working with
65 * @param templates - an array of templates this module uses
66 * @param lang - current language this module use
67 * @param loaded_mod_strings - the string that we will add the module template language into
69 function loadTemplateLanguage($module , $templates , $lang, $loaded_mod_strings){
70 $templates = array_reverse($templates);
71 foreach($templates as $template){
72 $temp = LanguageManager::addTemplate($module,$lang, $template);
73 $loaded_mod_strings = sugarArrayMerge($loaded_mod_strings, $temp);
75 return $loaded_mod_strings;
78 function addTemplate($module, $lang, $template){
79 if($template == 'default')$template = 'basic';
82 if(empty($templates[$template])){
83 $path = 'include/SugarObjects/templates/' . $template . '/language/'.$lang.'.lang.php';
84 if(file_exists($path)){
86 $templates[$template] = $mod_strings;
88 $path = 'include/SugarObjects/implements/' . $template . '/language/'.$lang.'.lang.php';
89 if(file_exists($path)){
91 $templates[$template] = $mod_strings;
95 if(!empty($templates[$template])){
96 return $templates[$template];
100 function saveCache($module,$lang, $loaded_mod_strings, $additonal_objects= array()){
102 $lang = $GLOBALS['sugar_config']['default_language'];
104 $file = create_cache_directory('modules/' . $module . '/language/'.$lang.'.lang.php');
105 write_array_to_file('mod_strings',$loaded_mod_strings, $file);
108 // put the item in the sugar cache.
109 $key = self::getLanguageCacheKey($module,$lang);
110 sugar_cache_put($key,$loaded_mod_strings);
114 * clear out the language cache.
115 * @param string module_dir the module_dir to clear, if not specified then clear
116 * clear language cache for all modules.
117 * @param string lang the name of the object we are clearing this is for sugar_cache
119 function clearLanguageCache($module_dir = '', $lang = ''){
121 $languages = array_keys($GLOBALS['sugar_config']['languages']);
123 $languages = array($lang);
125 //if we have a module name specified then just remove that language file
126 //otherwise go through each module and clean up the language
127 if(!empty($module_dir)) {
128 foreach($languages as $clean_lang) {
129 LanguageManager::_clearCache($module_dir, $clean_lang);
132 $cache_dir = $GLOBALS['sugar_config']['cache_dir'].'modules/';
133 if(file_exists($cache_dir) && $dir = @opendir($cache_dir)) {
134 while(($entry = readdir($dir)) !== false) {
135 if ($entry == "." || $entry == "..") continue;
136 foreach($languages as $clean_lang) {
137 LanguageManager::_clearCache($entry, $clean_lang);
146 * PRIVATE function used within clearLanguageCache so we do not repeat logic
147 * @param string module_dir the module_dir to clear
148 * @param string lang the name of the language file we are clearing this is for sugar_cache
150 function _clearCache($module_dir = '', $lang){
151 if(!empty($module_dir) && !empty($lang)){
152 $file = $GLOBALS['sugar_config']['cache_dir'].'modules/'.$module_dir.'/language/'.$lang.'.lang.php';
153 if(file_exists($file)){
155 $key = self::getLanguageCacheKey($module_dir,$lang);
156 sugar_cache_clear($key);
162 * Given a module, search all of the specified locations, and any others as specified
163 * in order to refresh the cache file
165 * @param string $module the given module we want to load the vardefs for
166 * @param string $lang the given language we wish to load
167 * @param array $additional_search_paths an array which allows a consumer to pass in additional vardef locations to search
169 function refreshLanguage($module, $lang, $loaded_mod_strings = array(), $additional_search_paths = null){
170 // Some of the vardefs do not correctly define dictionary as global. Declare it first.
172 'modules/'.$module.'/language/'.$lang.'.lang.php',
173 'modules/'.$module.'/language/'.$lang.'.lang.override.php',
174 'custom/modules/'.$module.'/Ext/Language/'.$lang.'.lang.ext.php',
175 'custom/modules/'.$module.'/language/'.$lang.'.lang.php',
178 #27023, if this module template language file was not attached , get the template from this module vardef cache file if exsits and load the template language files.
179 static $createdModules;
180 if(empty($createdModules[$module]) && isset($GLOBALS['beanList'][$module])){
181 $object = $GLOBALS['beanList'][$module];
183 if ($object == 'aCase')
186 if(!empty($GLOBALS["dictionary"]["$object"]["templates"])){
187 $templates = $GLOBALS["dictionary"]["$object"]["templates"];
188 $loaded_mod_strings = LanguageManager::loadTemplateLanguage($module , $templates, $lang , $loaded_mod_strings);
189 $createdModules[$module] = true;
194 // Add in additional search paths if they were provided.
195 if(!empty($additional_search_paths) && is_array($additional_search_paths))
197 $lang_paths = array_merge($lang_paths, $additional_search_paths);
200 //search a predefined set of locations for the vardef files
201 foreach($lang_paths as $path){
202 if(file_exists($path)){
204 if(!empty($mod_strings)){
205 if (function_exists('sugarArrayMergeRecursive')){
206 $loaded_mod_strings = sugarArrayMergeRecursive($loaded_mod_strings, $mod_strings);
209 $loaded_mod_strings = sugarArrayMerge($loaded_mod_strings, $mod_strings);
215 //great! now that we have loaded all of our vardefs.
216 //let's go save them to the cache file.
217 if(!empty($loaded_mod_strings))
218 LanguageManager::saveCache($module, $lang, $loaded_mod_strings);
221 function loadModuleLanguage($module, $lang, $refresh=false){
222 //here check if the cache file exists, if it does then load it, if it doesn't
223 //then call refreshVardef
224 //if either our session or the system is set to developerMode then refresh is set to true
226 // Retrieve the vardefs from cache.
227 $key = self::getLanguageCacheKey($module,$lang);
231 $return_result = sugar_cache_retrieve($key);
232 if(!empty($return_result)){
233 return $return_result;
237 // Some of the vardefs do not correctly define dictionary as global. Declare it first.
238 if($refresh || !file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'.$module.'/language/'.$lang.'.lang.php')){
239 LanguageManager::refreshLanguage($module, $lang);
242 //at this point we should have the cache/modules/... file
243 //which was created from the refreshVardefs so let's try to load it.
244 if(file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module . '/language/'.$lang.'.lang.php')){
247 require($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module . '/language/'.$lang.'.lang.php');
249 // now that we hae loaded the data from disk, put it in the cache.
250 if(!empty($mod_strings))
251 sugar_cache_put($key,$mod_strings);
252 if(!empty($_SESSION['translation_mode'])){
253 $mod_strings = array_map('translated_prefix', $mod_strings);
260 * Return the cache key for the module language definition
267 public static function getLanguageCacheKey($module, $lang)
269 return "LanguageManager.$module.$lang";
273 function translated_prefix($key){
274 return '[translated]' . $key;