From edae0abc97be78180922b46bf4713cfba06eab60 Mon Sep 17 00:00:00 2001 From: Leo Muniz Date: Thu, 20 Nov 2014 20:50:20 -0800 Subject: [PATCH] Multi-language implementation and other issues --- application/config.php | 41 +++++++++++----- application/controllers/_default.php | 7 ++- application/controllers/user.php | 4 +- application/elements/footer.php | 19 ++++++-- application/elements/header.php | 16 ++++++- application/functions.php | 16 +++++++ .../translation/br/_default/commom.php | 0 application/translation/br/_default/index.php | 0 application/translation/br/commom.php | 4 ++ .../default_language/_default/commom.php | 0 .../default_language/_default/index.php | 0 .../translation/default_language/commom.php | 4 ++ application/views/_default/index.php | 2 +- core/application.php | 48 +++++++++++++++++-- core/controller.php | 10 +++- static/css/_default/commom.css | 1 + static/css/widget.css | 1 + static/js/_default/commom.js | 1 + static/js/widget.js | 1 + 19 files changed, 151 insertions(+), 24 deletions(-) create mode 100755 application/translation/br/_default/commom.php create mode 100755 application/translation/br/_default/index.php create mode 100755 application/translation/br/commom.php create mode 100755 application/translation/default_language/_default/commom.php create mode 100755 application/translation/default_language/_default/index.php create mode 100755 application/translation/default_language/commom.php create mode 100755 static/css/_default/commom.css create mode 100755 static/css/widget.css create mode 100755 static/js/_default/commom.js create mode 100755 static/js/widget.js diff --git a/application/config.php b/application/config.php index 17e80eb..9397bec 100755 --- a/application/config.php +++ b/application/config.php @@ -13,19 +13,10 @@ define("SITE_DIRECTORY","/moj", true); define("DEBUG", true, true); define("DEFAULT_TEMPLATE","_default", true); +define("TRANSLATION",true,true); // Turn on multi-language site +define("LANGUAGES","br",true); // addicional site languages define("CRIPTO_KEY","choose-a-key", true); - -// Configures index method on URLs (passing parameters) -// When it is "false" this url: //index// -// will be the same as this url: /// if is not a method name -// -// Important: If it is set to "false" pay attention to not duplicate URL because of SEO reasons -// You can create a 301 redirect on .htaccess or inside controller->index() method -// You can also use a canonical tag on your head code, just like this: ” /> -define("INDEX_METHOD", false, true); - - // Constants to connect Database // Fill-in with your own database configuration. define("DB_TYPE","mysql", true); @@ -37,4 +28,32 @@ define("DB_COLLATE","", true); +// Configures index method as the default method if the called method doesn't exist (passing parameters) +// When it is "true" this url: //index// +// will be the same as this url: /// if is not a method name +// +// Important: If it is set to "true" pay attention to not duplicate URL because of SEO reasons +// You can create a 301 redirect on .htaccess or inside controller class; method index; +// You can also use a canonical tag on your head code, just like this: ” /> +define("AUTO_INDEX_METHOD", true, true); + + +// Configures _default controller as the default controller if the called controller doesn't exist (passing methods and params) +// When it is "true" this url: /// +// will be the same as this url: /_default/// if is not a controller name +// +// Important: If it is set to "true" pay attention to not duplicate URL because of SEO reasons +// You can create a 301 redirect on .htaccess or inside controller class; method index; +// You can also use a canonical tag on your head code, just like this: ” /> +define("AUTO_DEFAULT_CONTROLLER", false, true); + +// *********** +// !! WARNING: using AUTO_INDEX_METHOD=true and AUTO_DEFAULT_CONTROLLER=true together will cause never happens a 404 page. +// Every request to an unknown page will call index method of _default controller (homepage) with url variables as parameters +// *********** + + + + + diff --git a/application/controllers/_default.php b/application/controllers/_default.php index 219e4fc..008719f 100755 --- a/application/controllers/_default.php +++ b/application/controllers/_default.php @@ -6,7 +6,12 @@ class _default extends Controller { public function index() { // home - index.php - $this->renderView(); // renders views/_default/index.php using the default template defined on config.php + // Inserts specific Javascript and CSS files to this method + array_push($this->variables["jsFiles"], "widget.js"); + array_push($this->variables["cssFiles"], "widget.css"); + + // renders views/_default/index.php using the default template defined on config.php + $this->renderView(); } public function page404() { diff --git a/application/controllers/user.php b/application/controllers/user.php index 3d86caf..15d557c 100755 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -2,8 +2,10 @@ class User extends LAM\moj\Controller { + public $disableTranslation = true; // disables translation process to this controller - public function index() { + public function index($params) { + print_r($params); // Commom controller's function to establish a database connection $this->dbConnect(); diff --git a/application/elements/footer.php b/application/elements/footer.php index c422c9b..b721ead 100755 --- a/application/elements/footer.php +++ b/application/elements/footer.php @@ -4,15 +4,26 @@ Controller: _default; Javascript File: /static/js/_default/commom.js + // For other js => Javascript File: /static/js//commom.js + $javascriptFile = SITE_DIRECTORY."/static/js/".$controller."/commom.js"; + if (is_file($_SERVER["DOCUMENT_ROOT"].$javascriptFile)) echo ''; + + // Inserts javascript file with Method name inside Controller folder (inside /js/ folder) // For homepage => Controller: _default; Method: index; Javascript File: /static/js/_default/index.js // For other js => Javascript File: /static/js//.js - $javascriptFile = SITE_DIRECTORY."/static/js/".$controller."/".$method.".js"; - if (is_file($_SERVER["SITE_HTMLROOT"].$javascriptFile)) echo ''; - - ?> + if (is_file($_SERVER["DOCUMENT_ROOT"].$javascriptFile)) echo ''; + + // Inserts specific javascript files defined by $this->variables["jsFiles"] array + for ($i = 0; $i < count($jsFiles); $i++) { + $javascriptFile = SITE_DIRECTORY."/static/js/".$jsFiles[0]; + if (is_file($_SERVER["DOCUMENT_ROOT"].$javascriptFile)) echo ''; + } + ?> diff --git a/application/elements/header.php b/application/elements/header.php index 0043ad2..70a289d 100755 --- a/application/elements/header.php +++ b/application/elements/header.php @@ -14,13 +14,27 @@ Controller: _default; CSS File: /static/css/_default/commom.js + // For other css => CSS File: /static/css//commom.js + + $cssFile = SITE_DIRECTORY."/static/css/".$controller."/commom.css"; + if (is_file($_SERVER["DOCUMENT_ROOT"].$cssFile)) echo ''; + + // Inserts css file with Method name inside Controller folder (inside /css/ folder) // For homepage => Controller: _default; Method: index; CSS File: /static/css/_default/index.js // For other css => CSS File: /static/css//.js $cssFile = SITE_DIRECTORY."/static/css/".$controller."/".$method.".css"; - if (is_file($_SERVER["SITE_HTMLROOT"].$cssFile)) echo ''; + if (is_file($_SERVER["DOCUMENT_ROOT"].$cssFile)) echo ''; + + // Inserts specific css files defined by $this->variables["cssFiles"] array + for ($i = 0; $i < count($cssFiles); $i++) { + $cssFile = SITE_DIRECTORY."/static/css/".$cssFiles[0]; + if (is_file($_SERVER["DOCUMENT_ROOT"].$cssFile)) echo ''; + } ?> diff --git a/application/functions.php b/application/functions.php index 2e63288..a0840b9 100755 --- a/application/functions.php +++ b/application/functions.php @@ -78,4 +78,20 @@ function moj_pagination($foundRows,$pagina = 0,$itensPorPagina = 20, $classe="pa $html.= ''; return $html; +} + + +// functions to translate strings +// echo found translate +function __($stringIndex, $substitutions = "") { + global $translation; + + echo $translation[$stringIndex]; +} + +// return found translate +function _t($stringIndex, $substitutions = "") { + global $translation; + + return $translation[$stringIndex]; } \ No newline at end of file diff --git a/application/translation/br/_default/commom.php b/application/translation/br/_default/commom.php new file mode 100755 index 0000000..e69de29 diff --git a/application/translation/br/_default/index.php b/application/translation/br/_default/index.php new file mode 100755 index 0000000..e69de29 diff --git a/application/translation/br/commom.php b/application/translation/br/commom.php new file mode 100755 index 0000000..7dc4197 --- /dev/null +++ b/application/translation/br/commom.php @@ -0,0 +1,4 @@ + conteúdo home
\ No newline at end of file +

\ No newline at end of file diff --git a/core/application.php b/core/application.php index ec230ce..28c5bce 100755 --- a/core/application.php +++ b/core/application.php @@ -5,26 +5,39 @@ class Application { private $request; + private $language = ""; private $max_params = 3; public function __construct() { $this->processURL(); + if ((stripos(LANGUAGES,$this->request[0]) !== false) && (TRANSLATION)) $this->language = array_shift($this->request); + if ($this->request[0]) { // loads controller $controller = $this->request[0]; + if (!class_exists($controller) && (AUTO_DEFAULT_CONTROLLER)) { $controller = "LAM\Moj\_default"; } + if (class_exists($controller)) { $controller = new $controller(); // creates an instance of this controller - $this->request[1] = !$this->request[1]?"index":$this->request[1]; // index is the default method - $method = $this->request[1]; + //$this->request[1] = !$this->request[1]?"index":$this->request[1]; // index is the default method + //$method = $this->request[1]; + $method = (get_class($controller)=="LAM\Moj\_default")?$this->request[0]:((!$this->request[1])?"index":$this->request[1]); $method = str_replace("-","_",$method); // replaces hifen on url by underline - $method = ( (!method_exists($controller, $method)) && (!INDEX_METHOD) ) ? "index" : $method; + $method = ( (!method_exists($controller, $method)) && (AUTO_INDEX_METHOD) ) ? "index" : $method; if (method_exists($controller, $method)) { $firstParam = ($method == "index") && ($this->request[1] != "index") ? 1 : 2; for ($i = $firstParam; ($i < count($this->request)) && (($i - $firstParam) < $this->max_params); $i++) $params[$i - $firstParam] = $this->request[$i]; // concatenates params inside an Array + + if ((TRANSLATION) && (!$controller->disableTranslation)) { + $classname = explode("\\",get_class($controller)); // ignore namespace + $classname = strtolower(end($classname)); + $this->loadLanguage($classname, $method); + } + $controller->{$method}($params); // calls the method, passing params inside an array } else { if (DEBUG) @@ -62,9 +75,38 @@ private function processURL() { public function showDefaultPage($method) { $defaultController = new _default(); + if (TRANSLATION) $this->loadLanguage("_default",$method); $defaultController->{$method}(); } + + + private function loadLanguage ($controller = "_default",$method = "index") { + global $translation; + if ($this->language == "") $this->language = "default_language"; + + // Insert commom translation file to all controllers + if (file_exists("translation/".$this->language."/commom.php")) + include "translation/".$this->language."/commom.php"; + else if (DEBUG) + trigger_error("Translation file (application/translation/".$this->language."/commom.php) not found.",E_USER_WARNING); + + + // Insert translation file specific to controller (available to all methods) + if (file_exists("translation/".$this->language."/".$controller."/commom.php")) + include "translation/".$this->language."/".$controller."/commom.php"; + else if (DEBUG) + trigger_error("Translation file (application/translation/".$this->language."/".$controller."/commom.php) not found.",E_USER_WARNING); + + + // Insert translation file specific to method + if (file_exists("translation/".$this->language."/".$controller."/".$method.".php")) + include "translation/".$this->language."/".$controller."/".$method.".php"; + else if (DEBUG) + trigger_error("Translation file (application/translation/".$this->language."/".$controller."/".$method.".php) not found.",E_USER_WARNING); + + } + } diff --git a/core/controller.php b/core/controller.php index ef2706c..bbccc0b 100755 --- a/core/controller.php +++ b/core/controller.php @@ -9,8 +9,12 @@ class Controller { public $template = null; public $variables = []; public $db = null; + public $disableTranslation = false; - function __construct() { } + function __construct() { + $this->variables["jsFiles"] = array(); + $this->variables["cssFiles"] = array(); + } protected function renderView($view = null) { // renders the view @@ -18,7 +22,9 @@ protected function renderView($view = null) { // renders the view // discovers which method of which controller called the function $controller = get_called_class(); - $controller = strtolower(array_pop(explode("\\",$controller))); // explode to break possible namespace; array_pop to get the last element (the controller name) + $controller = explode("\\",$controller); // explode to break possible namespace; + $controller = array_pop($controller); // array_pop to get the last element (the controller name) + $controller = strtolower($controller); $method = $view != null?$view:debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; $method = str_replace("_","-",$method); // replaces underline on url by hifen diff --git a/static/css/_default/commom.css b/static/css/_default/commom.css new file mode 100755 index 0000000..f7bef33 --- /dev/null +++ b/static/css/_default/commom.css @@ -0,0 +1 @@ +/* commom css file to all methods of _default controller */ \ No newline at end of file diff --git a/static/css/widget.css b/static/css/widget.css new file mode 100755 index 0000000..719b7e1 --- /dev/null +++ b/static/css/widget.css @@ -0,0 +1 @@ +/* CSS file specific to an widget */ \ No newline at end of file diff --git a/static/js/_default/commom.js b/static/js/_default/commom.js new file mode 100755 index 0000000..9c84a28 --- /dev/null +++ b/static/js/_default/commom.js @@ -0,0 +1 @@ +// commom javascript file to all methods of _default controller \ No newline at end of file diff --git a/static/js/widget.js b/static/js/widget.js new file mode 100755 index 0000000..d7e88fe --- /dev/null +++ b/static/js/widget.js @@ -0,0 +1 @@ +// javascript file specific to an widget \ No newline at end of file