public function testBasicI18nOperation() { $bundle = new TextBundle("system/resources/messages"); // English $bundle->load("test"); $this->assertEquals("Hello, World!", $bundle->getText("hello.world")); $this->assertEquals("How are you, PHPUnit?", $bundle->getText("how.are.you", 'PHPUnit')); $this->assertEquals("This message is only in <strong>English</strong>", $bundle->getText("not.translated")); // Japanese $bundle->load("test", "jp"); $this->assertEquals("こんにちは、世界。", $bundle->getText("hello.world")); $this->assertEquals("どのようにあなたは、PHPUnitさん?", $bundle->getText("how.are.you", 'PHPUnit')); $this->assertEquals("This message is only in <strong>English</strong>", $bundle->getText("not.translated")); // Ukrainian $bundle->load("test", "ua"); $this->assertEquals("Здоров, світе!", $bundle->getText("hello.world")); $this->assertEquals("Як си маєте, пане PHPUnit?", $bundle->getText("how.are.you", 'PHPUnit')); $this->assertEquals("This message is only in <strong>English</strong>", $bundle->getText("not.translated")); // Not recognized: $this->assertEquals("***gibberish***", $bundle->getText("gibberish")); }
/** * Same as t() but here you have to set an language. This is espescially for email purpose. * @param unknown $lang * @param unknown $txt * @return Ambigous <unknown, NULL> */ function t2($lang, $txt) { global $i18n; $return = null; $args = func_get_args(); array_splice($args, 0, 1); if (isset($i18n) && getConf("language") == $lang) { // calls the function with the values in $data as arguments like $i18n->getText($data[0], $data[1], ...) $return = call_user_func_array(array($i18n, "getText"), $args); } else { $i18n2 = new TextBundle(CHURCHCORE . "/resources/messages"); $i18n2->load("churchcore", $lang); $return = call_user_func_array(array($i18n2, "getText"), $args); } return $return ? $return : $txt; }
/** * Get file name for i18n javascript file. * If not exists, it will be created. * TODO: should be renamed to getI18nFile * * @param string $modulename * * @return string; filename */ function createI18nFile($modulename) { global $config, $files_dir; if (!file_exists("{$files_dir}/files/messages/")) { mkdir("{$files_dir}/files/messages", 0777, true); } $filename = "{$files_dir}/files/messages/{$modulename}" . "_" . $config["language"] . ".js"; if (!file_exists($filename)) { //rrr $i18n=loadI18nFile($modulename) $i18n = new TextBundle("system/{$modulename}/resources/messages"); $i18n->load($modulename, $config["language"]); $i18n->writeJSFile($filename, $modulename); } return $filename; }
/** * TODO: put this into churchtools_main, no need for two functions * * Main entry point for churchtools. * This will be called from /index.php * Function loads i18n, configuration, check data security. * If everything is ok, it calls churchtools_processRequest() */ function churchtools_app() { global $q, $q_orig, $currentModule, $add_header, $config, $mapping, $content, $base_url, $files_dir, $user, $embedded, $i18n; include_once CHURCHCORE . "/churchcore_db.php"; $files_dir = DEFAULT_SITE; // which module is requested? $q = $q_orig = getVar("q", userLoggedIn() ? "home" : getConf("site_startpage", "home")); // $currentModule is needed for class autoloading and maybe other include paths list($currentModule) = explode('/', getVar("q")); // get first part of $q or churchcore $embedded = getVar("embedded", false); $base_url = getBaseUrl(); $config = loadConfig(); if ($config) { if (db_connect()) { // DBConfig overwrites the config files loadDBConfig(); if (empty($config['site_name'])) { $config['site_name'] = 'ChurchTools'; } //dont allow site_name to be empty date_default_timezone_set(getConf("timezone", "Europe/Berlin")); if (isset($_COOKIE["language"])) { $config["language"] = $_COOKIE["language"]; } // Load i18n churchcore-bundle if (!isset($config["language"])) { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $config["language"] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); } else { $config["language"] = DEFAULT_LANGUAGE; } } $i18n = new TextBundle(CHURCHCORE . "/resources/messages"); $i18n->load("churchcore", $config["language"] != null ? $config["language"] : null); // Session Init if (!file_exists($files_dir . "/tmp")) { @mkdir($files_dir . "/tmp", 0775, true); } if (!file_exists($files_dir . "/tmp")) { // Admin should act accordingly, default suggestion is 0755. addErrorMessage(t("permission.denied.write.dir", $files_dir)); } else { session_save_path($files_dir . "/tmp"); } session_name("ChurchTools_" . $config["db_name"]); session_start(); register_shutdown_function('handleShutdown'); // Check for offline mode. If it's activated display message and return false; if (getConf("site_offline") == 1) { if (!isset($_SESSION["user"]) || !in_array($_SESSION["user"]->id, getConf("admin_ids"))) { echo t("site.is.down"); return false; } } $embedded = getVar("embedded", false); $mapping = loadMapping(); $success = true; // Check for DB-Updates and loginstr only if this is not an ajax call. if (strrpos($q, "ajax") === false) { $success = checkForDBUpdates(); } // Log if debug ist activated if (isset($config["debug"])) { logParams(); } if ($success) { // Is there a loginstr which does not fit to the current logged in user? if (getVar("loginstr") && getVar("id") && userLoggedIn() && $_SESSION["user"]->id != getVar("id")) { logout_current_user(); session_start(); } else { loadUserObjectInSession(); } } if ($success) { if (isset($_SESSION['user'])) { $user = $_SESSION['user']; } // Accept data security? if (userLoggedIn() && !isset($_SESSION["simulate"]) && $q != "logout" && isset($config["accept_datasecurity"]) && $config["accept_datasecurity"] == 1 && !isset($user->acceptedsecurity)) { $content .= pleaseAcceptDatasecurity(); } else { $content .= churchtools_processRequest($q); } } } } // TODO: i changed header/footer to a sort of template // probably some more logic could be removed from them by setting some more variables here // put header/footer into new file layout.php and add a variable $content $lang = getConf("language"); $simulate = getVar("simulate", false, $_SESSION); $sitename = getConf("site_name"); if (getConf("test")) { $sitename .= " TEST "; } $logo = ($logo = getConf("site_logo")) ? "{$files_dir}/files/logo/{$logo}" : ''; include INCLUDES . "/header.php"; echo $content; include INCLUDES . "/footer.php"; }