2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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 ********************************************************************************/
38 * Language files management
45 * Called from VardefManager to allow for caching a lang file for a module
46 * @param module - the name of the module we are working with
47 * @param templates - an array of templates this module uses
49 function createLanguageFile($module , $templates=array('default'), $refresh = false){
50 global $mod_strings, $current_language;
51 if(inDeveloperMode() || !empty($_SESSION['developerMode'])){
54 $temp_mod_strings = $mod_strings;
55 $lang = $current_language;
57 $lang = $GLOBALS['sugar_config']['default_language'];
58 static $createdModules = array();
59 if(empty($createdModules[$module]) && ($refresh || !file_exists(sugar_cached('modules/').$module.'/language/'.$lang.'.lang.php'))){
60 $loaded_mod_strings = array();
61 $loaded_mod_strings = LanguageManager::loadTemplateLanguage($module , $templates, $lang , $loaded_mod_strings);
62 $createdModules[$module] = true;
63 LanguageManager::refreshLanguage($module,$lang, $loaded_mod_strings);
68 * Load the module tempalte lauguage files
69 * @param module - the name of the module we are working with
70 * @param templates - an array of templates this module uses
71 * @param lang - current language this module use
72 * @param loaded_mod_strings - the string that we will add the module template language into
74 function loadTemplateLanguage($module , $templates , $lang, $loaded_mod_strings){
75 $templates = array_reverse($templates);
76 foreach($templates as $template){
77 $temp = LanguageManager::addTemplate($module,$lang, $template);
78 $loaded_mod_strings = sugarArrayMerge($loaded_mod_strings, $temp);
80 return $loaded_mod_strings;
83 function addTemplate($module, $lang, $template){
84 if($template == 'default')$template = 'basic';
87 if(empty($templates[$template])){
88 $path = 'include/SugarObjects/templates/' . $template . '/language/'.$lang.'.lang.php';
89 if(file_exists($path)){
91 $templates[$template] = $mod_strings;
93 $path = 'include/SugarObjects/implements/' . $template . '/language/'.$lang.'.lang.php';
94 if(file_exists($path)){
96 $templates[$template] = $mod_strings;
100 if(!empty($templates[$template])){
101 return $templates[$template];
105 function saveCache($module,$lang, $loaded_mod_strings, $additonal_objects= array()){
107 $lang = $GLOBALS['sugar_config']['default_language'];
109 $file = create_cache_directory('modules/' . $module . '/language/'.$lang.'.lang.php');
110 write_array_to_file('mod_strings',$loaded_mod_strings, $file);
113 // put the item in the sugar cache.
114 $key = self::getLanguageCacheKey($module,$lang);
115 sugar_cache_put($key,$loaded_mod_strings);
119 * clear out the language cache.
120 * @param string module_dir the module_dir to clear, if not specified then clear
121 * clear language cache for all modules.
122 * @param string lang the name of the object we are clearing this is for sugar_cache
124 function clearLanguageCache($module_dir = '', $lang = ''){
126 $languages = array_keys($GLOBALS['sugar_config']['languages']);
128 $languages = array($lang);
130 //if we have a module name specified then just remove that language file
131 //otherwise go through each module and clean up the language
132 if(!empty($module_dir)) {
133 foreach($languages as $clean_lang) {
134 LanguageManager::_clearCache($module_dir, $clean_lang);
137 $cache_dir = sugar_cached('modules/');
138 if(file_exists($cache_dir) && $dir = @opendir($cache_dir)) {
139 while(($entry = readdir($dir)) !== false) {
140 if ($entry == "." || $entry == "..") continue;
141 foreach($languages as $clean_lang) {
142 LanguageManager::_clearCache($entry, $clean_lang);
151 * PRIVATE function used within clearLanguageCache so we do not repeat logic
152 * @param string module_dir the module_dir to clear
153 * @param string lang the name of the language file we are clearing this is for sugar_cache
155 function _clearCache($module_dir = '', $lang){
156 if(!empty($module_dir) && !empty($lang)){
157 $file = sugar_cached('modules/').$module_dir.'/language/'.$lang.'.lang.php';
158 if(file_exists($file)){
160 $key = self::getLanguageCacheKey($module_dir,$lang);
161 sugar_cache_clear($key);
167 * Given a module, search all of the specified locations, and any others as specified
168 * in order to refresh the cache file
170 * @param string $module the given module we want to load the vardefs for
171 * @param string $lang the given language we wish to load
172 * @param array $additional_search_paths an array which allows a consumer to pass in additional vardef locations to search
174 function refreshLanguage($module, $lang, $loaded_mod_strings = array(), $additional_search_paths = null){
175 // Some of the vardefs do not correctly define dictionary as global. Declare it first.
177 'modules/'.$module.'/language/'.$lang.'.lang.php',
178 'modules/'.$module.'/language/'.$lang.'.lang.override.php',
179 'custom/modules/'.$module.'/Ext/Language/'.$lang.'.lang.ext.php',
180 'custom/modules/'.$module.'/language/'.$lang.'.lang.php',
183 #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.
184 static $createdModules;
185 if(empty($createdModules[$module]) && isset($GLOBALS['beanList'][$module])){
186 $object = $GLOBALS['beanList'][$module];
188 if ($object == 'aCase')
191 if(!empty($GLOBALS["dictionary"]["$object"]["templates"])){
192 $templates = $GLOBALS["dictionary"]["$object"]["templates"];
193 $loaded_mod_strings = LanguageManager::loadTemplateLanguage($module , $templates, $lang , $loaded_mod_strings);
194 $createdModules[$module] = true;
199 // Add in additional search paths if they were provided.
200 if(!empty($additional_search_paths) && is_array($additional_search_paths))
202 $lang_paths = array_merge($lang_paths, $additional_search_paths);
205 //search a predefined set of locations for the vardef files
206 foreach($lang_paths as $path){
207 if(file_exists($path)){
209 if(!empty($mod_strings)){
210 if (function_exists('sugarArrayMergeRecursive')){
211 $loaded_mod_strings = sugarArrayMergeRecursive($loaded_mod_strings, $mod_strings);
214 $loaded_mod_strings = sugarArrayMerge($loaded_mod_strings, $mod_strings);
220 //great! now that we have loaded all of our vardefs.
221 //let's go save them to the cache file.
222 if(!empty($loaded_mod_strings))
223 LanguageManager::saveCache($module, $lang, $loaded_mod_strings);
226 function loadModuleLanguage($module, $lang, $refresh=false){
227 //here check if the cache file exists, if it does then load it, if it doesn't
228 //then call refreshVardef
229 //if either our session or the system is set to developerMode then refresh is set to true
231 // Retrieve the vardefs from cache.
232 $key = self::getLanguageCacheKey($module,$lang);
236 $return_result = sugar_cache_retrieve($key);
237 if(!empty($return_result)){
238 return $return_result;
242 // Some of the vardefs do not correctly define dictionary as global. Declare it first.
243 $cachedfile = sugar_cached('modules/').$module.'/language/'.$lang.'.lang.php';
244 if($refresh || !file_exists($cachedfile)){
245 LanguageManager::refreshLanguage($module, $lang);
248 //at this point we should have the cache/modules/... file
249 //which was created from the refreshVardefs so let's try to load it.
250 if(file_exists($cachedfile)){
255 // now that we hae loaded the data from disk, put it in the cache.
256 if(!empty($mod_strings))
257 sugar_cache_put($key,$mod_strings);
258 if(!empty($_SESSION['translation_mode'])){
259 $mod_strings = array_map('translated_prefix', $mod_strings);
266 * Return the cache key for the module language definition
273 public static function getLanguageCacheKey($module, $lang)
275 return "LanguageManager.$module.$lang";
279 * Remove any cached js language strings.
284 public static function removeJSLanguageFiles()
287 getFiles($jsFiles, sugar_cached('jsLanguage'));
288 foreach($jsFiles as $file) {
292 if( empty($GLOBALS['sugar_config']['js_lang_version']) )
293 $GLOBALS['sugar_config']['js_lang_version'] = 1;
295 $GLOBALS['sugar_config']['js_lang_version'] += 1;
297 write_array_to_file( "sugar_config", $GLOBALS['sugar_config'], "config.php");
301 function translated_prefix($key){
302 return '[translated]' . $key;