Skip to content

Commit

Permalink
Multi-language implementation and other issues
Browse files Browse the repository at this point in the history
  • Loading branch information
leomuniz committed Nov 21, 2014
1 parent df08a65 commit edae0ab
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 24 deletions.
41 changes: 30 additions & 11 deletions application/config.php
Expand Up @@ -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: /<controller>/index/<param>/
// will be the same as this url: /<controller>/<param>/ if <param> 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: <link rel=”canonical” href=”<your-url>” />
define("INDEX_METHOD", false, true);


// Constants to connect Database
// Fill-in with your own database configuration.
define("DB_TYPE","mysql", true);
Expand All @@ -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: /<controller>/index/<param>/
// will be the same as this url: /<controller>/<param>/ if <param> 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: <link rel=”canonical” href=”<your-url>” />
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: /<method>/<param>/
// will be the same as this url: /_default/<method>/<param>/ if <method> 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: <link rel=”canonical” href=”<your-url>” />
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
// ***********






7 changes: 6 additions & 1 deletion application/controllers/_default.php
Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion application/controllers/user.php
Expand Up @@ -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();
Expand Down
19 changes: 15 additions & 4 deletions application/elements/footer.php
Expand Up @@ -4,15 +4,26 @@
<script src="<?=SITE_DIRECTORY?>/static/js/main.js"></script>

<?
// Inserts common javascript file to all methods of a controller
// For homepage => Controller: _default; Javascript File: /static/js/_default/commom.js
// For other js => Javascript File: /static/js/<controller>/commom.js
$javascriptFile = SITE_DIRECTORY."/static/js/".$controller."/commom.js";
if (is_file($_SERVER["DOCUMENT_ROOT"].$javascriptFile)) echo '<script src="'.$javascriptFile.'"></script>';


// 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/<controller>/<method>.js

$javascriptFile = SITE_DIRECTORY."/static/js/".$controller."/".$method.".js";
if (is_file($_SERVER["SITE_HTMLROOT"].$javascriptFile)) echo '<script src="'.$javascriptFile.'"></script>';

?>
if (is_file($_SERVER["DOCUMENT_ROOT"].$javascriptFile)) echo '<script src="'.$javascriptFile.'"></script>';


// 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 '<script src="'.$javascriptFile.'"></script>';
}
?>

<? include "elements/google-analytics.php" ?>
</body>
Expand Down
16 changes: 15 additions & 1 deletion application/elements/header.php
Expand Up @@ -14,13 +14,27 @@
<link rel="stylesheet" href="<?=SITE_DIRECTORY?>/static/css/normalize.min.css">
<link rel="stylesheet" href="<?=SITE_DIRECTORY?>/static/css/main.css">
<?
// Inserts commom css file to all methods of a controller (inside /css/ folder)
// For homepage => Controller: _default; CSS File: /static/css/_default/commom.js
// For other css => CSS File: /static/css/<controller>/commom.js

$cssFile = SITE_DIRECTORY."/static/css/".$controller."/commom.css";
if (is_file($_SERVER["DOCUMENT_ROOT"].$cssFile)) echo '<link rel="stylesheet" href="'.$cssFile.'">';


// 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/<controller>/<method>.js

$cssFile = SITE_DIRECTORY."/static/css/".$controller."/".$method.".css";
if (is_file($_SERVER["SITE_HTMLROOT"].$cssFile)) echo '<link rel="stylesheet" href="'.$cssFile.'">';
if (is_file($_SERVER["DOCUMENT_ROOT"].$cssFile)) echo '<link rel="stylesheet" href="'.$cssFile.'">';


// 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 '<link rel="stylesheet" href="'.$cssFile.'">';
}
?>

<!--script src="<?=SITE_DIRECTORY?>/static/js/vendor/modernizr-2.6.2.min.js"></script-->
Expand Down
16 changes: 16 additions & 0 deletions application/functions.php
Expand Up @@ -78,4 +78,20 @@ function moj_pagination($foundRows,$pagina = 0,$itensPorPagina = 20, $classe="pa
$html.= '</ul>';

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];
}
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions application/translation/br/commom.php
@@ -0,0 +1,4 @@
<?
// BRAZILIAN PORTUGUESE TRANSLATE FILE

$translation["home_content"] = "Conteúdo da Home";
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions application/translation/default_language/commom.php
@@ -0,0 +1,4 @@
<?
// DEFAULT LANGUAGE - ENGLISH TRANSLATE FILE

$translation["home_content"] = "Home content";
2 changes: 1 addition & 1 deletion application/views/_default/index.php
@@ -1 +1 @@
<br> conteúdo home <br>
<p> <?=_t("home_content");?> </p>
48 changes: 45 additions & 3 deletions core/application.php
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

}

}


10 changes: 8 additions & 2 deletions core/controller.php
Expand Up @@ -9,16 +9,22 @@ 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

foreach ($this->variables as $key => $value) { $$key = $value; } // creates variables to use in 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
Expand Down
1 change: 1 addition & 0 deletions static/css/_default/commom.css
@@ -0,0 +1 @@
/* commom css file to all methods of _default controller */
1 change: 1 addition & 0 deletions static/css/widget.css
@@ -0,0 +1 @@
/* CSS file specific to an widget */
1 change: 1 addition & 0 deletions static/js/_default/commom.js
@@ -0,0 +1 @@
// commom javascript file to all methods of _default controller
1 change: 1 addition & 0 deletions static/js/widget.js
@@ -0,0 +1 @@
// javascript file specific to an widget

0 comments on commit edae0ab

Please sign in to comment.