/** * @todo Refactor this method */ function updateLocaleFiles() { $new_core_entries = array(); $new_controller_entries = array(); $new_controller_files = array(); $used_entries = AkLocaleManager::getUsedLanguageEntries(); require AK_CONFIG_DIR . DS . 'locales' . DS . AK_FRAMEWORK_LANGUAGE . '.php'; $core_dictionary = $dictionary; $controllers_dictionaries = array(); foreach ($used_entries as $k => $v) { // This is a controller file if (is_array($v)) { if (!isset($controllers_dictionaries[$k])) { $controller = $k; $module_lang_file = AK_APP_DIR . DS . 'locales' . DS . $controller . DS . AK_FRAMEWORK_LANGUAGE . '.php'; if (is_file($module_lang_file)) { require $module_lang_file; $controllers_dictionaries[$controller] = array_merge((array) $dictionary, (array) $v); $existing_controllers_dictionaries[$controller] = (array) $dictionary; } else { $controllers_dictionaries[$controller] = (array) $v; $new_controller_files[$controller] = $module_lang_file; } } } else { if (!isset($core_dictionary[$k])) { $new_core_entries[$k] = $k; } } } $dictionary_file = ''; foreach ($new_controller_files as $controller => $file_name) { $dictionary_file = "<?php\n\n// File created on: " . date("Y-m-d G:i:s", Ak::time()) . "\n\n\$dictionary = array();\n\n"; foreach ($controllers_dictionaries[$controller] as $k => $entry) { $entry = str_replace("'", "\\'", $entry); $dictionary_file .= "\n\$dictionary['{$entry}'] = '{$entry}';"; } unset($controllers_dictionaries[$controller]); $dictionary_file .= "\n\n\n?>"; Ak::file_put_contents($file_name, $dictionary_file); } // Module files foreach ((array) $controllers_dictionaries as $controller => $controller_entries) { $dictionary_file = ''; foreach ($controller_entries as $entry) { if ($entry == '' || isset($existing_controllers_dictionaries[$controller][$entry])) { continue; } $entry = str_replace("'", "\\'", $entry); $dictionary_file .= "\n\$dictionary['{$entry}'] = '{$entry}';"; } if ($dictionary_file != '') { $original_file = Ak::file_get_contents(AK_APP_DIR . DS . 'locales' . DS . $controller . DS . AK_FRAMEWORK_LANGUAGE . '.php'); $original_file = rtrim($original_file, "?> \n\r"); $new_entries = "\n\n// " . date("Y-m-d G:i:s", Ak::time()) . "\n\n" . $dictionary_file . "\n\n\n?>\n"; $dictionary_file = $original_file . $new_entries; Ak::file_put_contents(AK_APP_DIR . DS . 'locales' . DS . $controller . DS . AK_FRAMEWORK_LANGUAGE . '.php', $dictionary_file); foreach (Ak::langs() as $lang) { if ($lang != AK_FRAMEWORK_LANGUAGE) { $lang_file = @Ak::file_get_contents(AK_APP_DIR . DS . 'locales' . DS . $controller . DS . $lang . '.php'); if (empty($lang_file)) { $dictionary_file = $original_file; } else { $lang_file = rtrim($lang_file, "?> \n\r"); $dictionary_file = $lang_file; } Ak::file_put_contents(AK_APP_DIR . DS . 'locales' . DS . $controller . DS . $lang . '.php', $dictionary_file . $new_entries); } } } } // Core locale files $dictionary_file = ''; foreach ($new_core_entries as $core_entry) { if ($core_entry == '') { continue; } $core_entry = str_replace("'", "\\'", $core_entry); $dictionary_file .= "\n\$dictionary['{$core_entry}'] = '{$core_entry}';"; } if ($dictionary_file != '') { $original_file = Ak::file_get_contents(AK_CONFIG_DIR . DS . 'locales' . DS . AK_FRAMEWORK_LANGUAGE . '.php'); $original_file = rtrim($original_file, "?> \n\r"); $new_entries = "\n\n// " . date("Y-m-d G:i:s", Ak::time()) . "\n\n" . $dictionary_file . "\n\n\n?>\n"; $dictionary_file = $original_file . $new_entries; Ak::file_put_contents(AK_CONFIG_DIR . DS . 'locales' . DS . AK_FRAMEWORK_LANGUAGE . '.php', $dictionary_file); foreach (Ak::langs() as $lang) { if ($lang != AK_FRAMEWORK_LANGUAGE) { $lang_file = Ak::file_get_contents(AK_CONFIG_DIR . DS . 'locales' . DS . $lang . '.php'); if (empty($lang_file)) { $dictionary_file = str_replace("\$locale['description'] = 'English';", "\$locale['description'] = '{$lang}';", $original_file); } else { $lang_file = rtrim($lang_file, "?> \n\r"); $dictionary_file = $lang_file; } Ak::file_put_contents(AK_CONFIG_DIR . DS . 'locales' . DS . $lang . '.php', $dictionary_file . $new_entries); } } } }
/** * Translate strings to the current locale. * * When using Ak::t(), try to put entire sentences and strings * in one Ak::t() call. * This makes it easier for translators. HTML markup within * translation strings * is acceptable, if necessary. The suggested syntax for a * link embedded * within a translation string is: * * @access public * @static * @param string $string A string containing the English string to * translate. * @param array $args An associative array of replacements to make after * translation. Incidences of any key in this array * are replaced with the corresponding value. * @return string The translated string. */ function t($string, $args = null, $controller = null) { static $framework_dictionary = array(), $lang, $_dev_shutdown = true; if (AK_AUTOMATICALLY_UPDATE_LANGUAGE_FILES && !empty($string) && is_string($string)) { require_once AK_LIB_DIR . DS . 'AkLocaleManager.php'; // This adds used strings to a stack for storing new entries on the locale file after shutdown AkLocaleManager::getUsedLanguageEntries($string, $controller); if ($_dev_shutdown) { register_shutdown_function(array('AkLocaleManager', 'updateLocaleFiles')); $_dev_shutdown = false; } } if (!isset($lang)) { if (!empty($_SESSION['lang'])) { $lang = $_SESSION['lang']; } else { $lang = Ak::lang(); } if (is_file(AK_CONFIG_DIR . DS . 'locales' . DS . $lang . '.php')) { require AK_CONFIG_DIR . DS . 'locales' . DS . $lang . '.php'; $framework_dictionary = array_merge($framework_dictionary, $dictionary); } if (!defined('AK_LOCALE')) { define('AK_LOCALE', $lang); } if (!empty($locale) && is_array($locale)) { Ak::locale(null, $lang, $locale); } } if (!empty($string) && is_array($string)) { if (!empty($string[$lang])) { return $string[$lang]; } $try_whith_lang = $args !== false && empty($string[$lang]) ? Ak::base_lang() : $lang; if (empty($string[$try_whith_lang]) && $args !== false) { foreach (Ak::langs() as $try_whith_lang) { if (!empty($string[$try_whith_lang])) { return $string[$try_whith_lang]; } } } return @$string[$try_whith_lang]; } if (isset($controller) && !isset($framework_dictionary[$controller . '_dictionary']) && is_file(AK_APP_DIR . DS . 'locales' . DS . $controller . DS . $lang . '.php')) { require AK_APP_DIR . DS . 'locales' . DS . $controller . DS . $lang . '.php'; $framework_dictionary[$controller . '_dictionary'] = (array) $dictionary; } if (isset($controller) && isset($framework_dictionary[$controller . '_dictionary'][$string])) { $string = $framework_dictionary[$controller . '_dictionary'][$string]; } else { $string = isset($framework_dictionary[$string]) ? $framework_dictionary[$string] : $string; } if (isset($args) && is_array($args)) { $string = @str_replace(array_keys($args), array_values($args), $string); } /** * @todo Prepare for multiple locales by inspecting AK_DEFAULT_LOCALE */ return $string; }
/** * @todo Refactor this method */ static function updateLocaleFiles() { if (defined('AK_LOCALE_MANAGER') && class_exists(AK_LOCALE_MANAGER) && in_array('AkLocaleManager', class_parents(AK_LOCALE_MANAGER))) { return; } $paths = array(); $new_core_entries = array(); $new_controller_entries = array(); $new_controller_files = array(); $used_entries = AkLocaleManager::getUsedLanguageEntries(); list($core_locale, $core_dictionary) = AkLocaleManager::getCoreDictionary(AK_FRAMEWORK_LANGUAGE); $controllers_dictionaries = array(); foreach ($used_entries as $k => $v) { // This is a controller file if (is_array($v)) { if (!isset($controllers_dictionaries[$k])) { $controller = $k; $controllers_dictionaries[$controller] = AkLocaleManager::getDictionary(AK_FRAMEWORK_LANGUAGE, $controller); if (!empty($controllers_dictionaries[$controller])) { $existing_controllers_dictionaries[$controller] = $controllers_dictionaries[$controller]; } else { $new_controller_files[$controller] = true; } $controllers_dictionaries[$controller] = array_merge($controllers_dictionaries[$controller], (array) $v); } } else { if (!isset($core_dictionary[$k])) { $new_core_entries[$k] = $k; } } } foreach ($new_controller_files as $controller => $true) { $paths[] = AkLocaleManager::setDictionary($controllers_dictionaries[$controller], AK_FRAMEWORK_LANGUAGE, $controller, "File created on: " . date("Y-m-d G:i:s", Ak::time())); foreach (Ak::langs() as $lang) { if ($lang != AK_FRAMEWORK_LANGUAGE) { $dictionary = AkLocaleManager::getDictionary($lang, $controller); $paths[] = AkLocaleManager::setDictionary(array_merge($controllers_dictionaries[$controller], $dictionary), $lang, $controller); } } unset($controllers_dictionaries[$controller]); } // Module files foreach ((array) $controllers_dictionaries as $controller => $controller_entries) { $controller_entries = AkLocaleManager::getNewEntries($controller_entries, (array) @$existing_controllers_dictionaries[$controller]); if (!empty($controller_entries)) { $dictionary = AkLocaleManager::getDictionary(AK_FRAMEWORK_LANGUAGE, $controller); $paths[] = AkLocaleManager::setDictionary(array_merge($dictionary, $controller_entries), AK_FRAMEWORK_LANGUAGE, $controller); foreach (Ak::langs() as $lang) { if ($lang != AK_FRAMEWORK_LANGUAGE) { $dictionary = AkLocaleManager::getDictionary($lang, $controller); $paths[] = AkLocaleManager::setDictionary(array_merge($dictionary, $controller_entries), $lang, $controller); } } } } // Core locale files $new_core_entries = AkLocaleManager::getNewEntries($new_core_entries); if (!empty($new_core_entries)) { AkLocaleManager::setCoreDictionary($core_locale, array_merge($core_dictionary, $new_core_entries), AK_FRAMEWORK_LANGUAGE); foreach (Ak::langs() as $lang) { if ($lang != AK_FRAMEWORK_LANGUAGE) { list($l, $dictionary) = AkLocaleManager::getCoreDictionary($lang); if (empty($l)) { $l = $core_locale; $l['description'] = $lang; $l['locale_description'] = $lang; } if (empty($dictionary)) { $dictionary = $core_dictionary; } AkLocaleManager::setCoreDictionary($l, array_merge($dictionary, $new_core_entries), $lang); } } } return $paths; }