Merge pull request #24 from foxbot/master

Better language manager + add htaccess to .gitignore
master
foxbot 10 years ago
commit eef9cacbec

3
.gitignore vendored

@ -1,4 +1,5 @@
.DS_Store
.sass-cache/
css/.sass-cache/
compass_app_log.txt
compass_app_log.txt
.htaccess

File diff suppressed because one or more lines are too long

@ -95,7 +95,7 @@
<div id="langswitch">
<form action="#" method="post">
<select name="lang">
<?=printlangs()?>
<?=$l->printLanguageOptions()?>
</select>
</form>
</div>

@ -85,7 +85,7 @@
<div id="langswitch">
<form action="#" method="post">
<select name="lang">
<?=printlangs()?>
<?=$l->printLanguageOptions()?>
</select>
</form>
</div>

@ -1,4 +1,6 @@
<?php
require_once(__DIR__ . '/language.class.php');
define('DOMAIN', 'http://get-popcorn.com/');
define('VERSION', '2.9');
@ -23,28 +25,8 @@
);
/* language manager */
$langlist = array();
foreach(scandir("inc/lang") as $value) {
if(preg_match("/^([a-z-]{2,5}).php$/i", $value, $langname)){
$langlist[]=$langname[1];
}
}
$lang = "en";
if (!empty($_GET["lang"])) {
$newlang = $_GET["lang"];
} else if (!empty($_COOKIE["lang"])) {
$newlang = $_COOKIE["lang"];
}
if (isset($newlang) && preg_match("/^[a-z-]{2,5}$/i", $newlang) && in_array($newlang, $langlist)) {
$lang = $newlang;
setcookie("lang", $lang,time()+31536000);
}
if(!empty($_GET["lang"])) header("Location: ".strtok($_SERVER['REQUEST_URI'], '?'));
include("inc/lang/$lang.php");
define('LANG_DIR', __DIR__.'/lang');
define('DEFAULT_LANGUAGE', 'en');
function printlangs(){
global $langlist,$lang;
foreach($langlist as $langname){
echo "<option id='$langname' value='$langname' ".($langname == $lang? 'selected':'').">&nbsp;</option>\n";
}
}
$l = new LanguageManager(LANG_DIR, DEFAULT_LANGUAGE);
$langsite = $l->includeLanguage();

@ -0,0 +1,93 @@
<?php
class LanguageManager {
private $languageDirectory,
$defaultLanguage;
public function __construct($languageDirectory, $defaultLanguage) {
$this->setLanguageDirectory($languageDirectory);
$this->setDefaultLanguage($defaultLanguage);
}
public function getLanguageDirectory() {
return $this->languageDirectory;
}
private function setLanguageDirectory($languageDirectory) {
$this->languageDirectory = $languageDirectory;
}
public function getDefaultLanguage() {
return $this->defaultLanguage;
}
private function setDefaultLanguage($defaultLanguage) {
$this->defaultLanguage = $defaultLanguage;
}
public function includeLanguage($language = null) {
$language = ($language && $this->isValidLanguage($language)) ? $language : $this->getUserLanguage();
if(!$this->checkCookie($language)) {
$this->createCookie($language);
}
require_once($this->getLanguageDirectory() . '/' . $this->formatLanguageFileName($language));
return $langsite;
}
public function printLanguageOptions() {
$optionsString = '';
foreach($this->getLanguages() as $language) {
$optionsString .= "<option id='" . $language . "' value='" . $language . "' " . ($language == $this->getUserLanguage() ? 'selected' : '') . ">&nbsp;</option>\n";
}
return $optionsString;
}
private function getLanguages() {
$languages = array();
foreach(scandir($this->getLanguageDirectory()) as $language) {
$language = $this->formatLanguageName($language);
if($this->isValidLanguage($language)) {
$languages[] = $language;
}
}
return $languages;
}
private function getUserLanguage() {
if(isset($_GET["lang"]) && $this->isValidLanguage($_GET["lang"])) {
$language = $_GET["lang"];
} else if(isset($_COOKIE["lang"]) && $this->isValidLanguage($_COOKIE["lang"])) {
$language = $_COOKIE["lang"];
} else if($this->isValidLanguage(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2))) {
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
} else {
$language = $this->getDefaultLanguage();
}
return $language;
}
private function checkCookie($language) {
return (isset($_COOKIE['lang']) && $_COOKIE['lang'] == $language);
}
private function createCookie($language) {
setcookie("lang", $language, time()+31536000);
header("Location: ".strtok($_SERVER['REQUEST_URI'], '?'));
}
private function isValidLanguage($language) {
$languageFile = $this->formatLanguageFileName($language);
return (file_exists($this->getLanguageDirectory() . '/' . $languageFile) && is_file($this->getLanguageDirectory() . '/' . $languageFile) && preg_match("/^([a-z-]{2,5})$/i", $language));
}
private function formatLanguageFileName($language) {
return $language . '.php';
}
private function formatLanguageName($language) {
return str_replace('.php', '', $language);
}
}

@ -86,7 +86,7 @@
<div id="langswitch">
<form action="#" method="post">
<select name="lang">
<?=printlangs()?>
<?=$l->printLanguageOptions()?>
</select>
</form>
</div>

@ -1,375 +1,43 @@
// JavaScript Document
/* ---------------------------------------------------------------------- */
/* "Polyglot" Language Switcher
/* ----------------------------------------------------------------------
Version: 2.2
Author: Ixtendo
Author URI: http://www.ixtendo.com
License: MIT License
License URI: http://www.opensource.org/licenses/mit-license.php
------------------------------------------------------------------------- */
/**
* jquery.timer.js
*
* Copyright (c) 2011 Jason Chavannes <jason.chavannes@gmail.com>
*
* http://jchavannes.com/jquery-timer
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
(function ($) {
//jquery.timer.js
$.timer = function(func, time, autostart) {
this.set = function(func, time, autostart) {
this.init = true;
if(typeof func == 'object') {
var paramList = ['autostart', 'time'];
for(var arg in paramList) {if(func[paramList[arg]] != undefined) {eval(paramList[arg] + " = func[paramList[arg]]");}};
func = func.action;
}
if(typeof func == 'function') {this.action = func;}
if(!isNaN(time)) {this.intervalTime = time;}
if(autostart && !this.active) {
this.active = true;
this.setTimer();
}
return this;
};
this.once = function(time) {
var timer = this;
if(isNaN(time)) {time = 0;}
window.setTimeout(function() {timer.action();}, time);
return this;
};
this.play = function(reset) {
if(!this.active) {
if(reset) {this.setTimer();}
else {this.setTimer(this.remaining);}
this.active = true;
}
return this;
};
this.pause = function() {
if(this.active) {
this.active = false;
this.remaining -= new Date() - this.last;
this.clearTimer();
}
return this;
};
this.stop = function() {
this.active = false;
this.remaining = this.intervalTime;
this.clearTimer();
return this;
};
this.toggle = function(reset) {
if(this.active) {this.pause();}
else if(reset) {this.play(true);}
else {this.play();}
return this;
};
this.reset = function() {
this.active = false;
this.play(true);
return this;
};
this.clearTimer = function() {
window.clearTimeout(this.timeoutObject);
};
this.setTimer = function(time) {
var timer = this;
if(typeof this.action != 'function') {return;}
if(isNaN(time)) {time = this.intervalTime;}
this.remaining = time;
this.last = new Date();
this.clearTimer();
this.timeoutObject = window.setTimeout(function() {timer.go();}, time);
};
this.go = function() {
if(this.active) {
this.action();
this.setTimer();
}
};
if(this.init) {
return new $.timer(func, time, autostart);
} else {
this.set(func, time, autostart);
return this;
}
};
$.fn.polyglotLanguageSwitcher = function (op) {
var ls = $.fn.polyglotLanguageSwitcher;
var rootElement = $(this);
var rootElementId = $(this).attr('id');
var aElement;
var ulElement = $("<ul class=\"dropdown\">");
var length = 0;
var isOpen = false;
var liElements = [];
var settings = $.extend({}, ls.defaults, op);
var closePopupTimer;
var isStaticWebSite = settings.websiteType == 'static';
init();
installListeners();
function triggerEvent(evt) {
if(settings[evt.name]){
settings[evt.name].call($(this), evt);
}
}
function open() {
if(!isOpen){
triggerEvent({name:'beforeOpen', element:rootElement, instance:ls});
aElement.addClass("active");
doAnimation(true);
setTimeout(function () {
isOpen = true;
triggerEvent({name:'afterOpen', element:rootElement, instance:ls});
}, 100);
}
}
function close() {
if(isOpen){
triggerEvent({name:'beforeClose', element:rootElement, instance:ls});
doAnimation(false);
aElement.removeClass("active");
isOpen = false;
if (closePopupTimer && closePopupTimer.active) {
closePopupTimer.clearTimer();
}
triggerEvent({name:'afterClose', element:rootElement, instance:ls});
}
}
function suspendCloseAction() {
if (closePopupTimer && closePopupTimer.active) {
closePopupTimer.pause();
}
}
function resumeCloseAction() {
if (closePopupTimer) {
closePopupTimer.play(false);
}
}
function doAnimation(open) {
if (settings.effect == 'fade') {
if (open) {
ulElement.fadeIn(settings.animSpeed);
} else {
ulElement.fadeOut(settings.animSpeed);
}
} else {
if (open) {
ulElement.slideDown(settings.animSpeed);
} else {
ulElement.slideUp(settings.animSpeed);
}
}
}
function doAction(item) {
close();
var selectedAElement = $(item).children(":first-child");
var selectedId = $(selectedAElement).attr("id");
var selectedText = $(selectedAElement).text();
$(ulElement).children().each(function () {
$(this).detach();
});
for (var i = 0; i < liElements.length; i++) {
if ($(liElements[i]).children(":first-child").attr("id") != selectedId) {
ulElement.append(liElements[i]);
}
}
var innerSpanElement = aElement.children(":first-child");
aElement.attr("id", selectedId);
aElement.text(selectedText);
aElement.append(innerSpanElement);
}
function installListeners() {
$(document).click(function () {
close();
});
$(document).keyup(function (e) {
if (e.which == 27) {
close();
}
});
if (settings.openMode == 'hover') {
closePopupTimer = $.timer(function () {
close();
});
closePopupTimer.set({ time:settings.hoverTimeout, autostart:true });
}
}
function init() {
var selectedItem;
var options = $("#" + rootElementId + " > form > select > option");
if (isStaticWebSite) {
var selectedId;
var url = window.location.href;
options.each(function(){
var id = $(this).attr("id");
if(url.indexOf('/'+id+'/')>=0){
selectedId = id;
}
});
}
options.each(function () {
var id = $(this).attr("id");
var selected;
if (isStaticWebSite) {
selected = selectedId === id;
}else{
selected = $(this).attr("selected")
}
var liElement = toLiElement($(this));
if (selected) {
selectedItem = liElement;
}
liElements.push(liElement);
if (length > 0) {
ulElement.append(liElement);
} else {
aElement = $("<a id=\"" + $(this).attr("id") + "\" class=\"current\" href=\"#\">" + $(this).text() + " <span class=\"trigger\">&raquo;</span></a>");
if (settings.openMode == 'hover') {
aElement.hover(function () {
open();
suspendCloseAction();
}, function () {
resumeCloseAction();
});
} else {
aElement.click(
function () {
open();
}
);
}
}
length++;
});
$("#" + rootElementId + " form:first-child").remove();
rootElement.append(aElement);
rootElement.append(ulElement);
if (selectedItem) {
doAction(selectedItem);
}
}
function toLiElement(option) {
var id = $(option).attr("id");
var value = $(option).attr("value");
var text = $(option).text();
var liElement;
if (isStaticWebSite) {
var url = window.location.href;
var page = url.substring(url.lastIndexOf("/")+1);
var urlPage = 'http://' + document.domain + '/' + settings.pagePrefix + id + '/' + page;
liElement = $("<li><a id=\"" + id + "\" href=\"" + urlPage + "\">" + text + "</a></li>");
} else {
var href = document.URL.replace('#', '');
var params = parseQueryString();
params[settings.paramName] = value;
if (href.indexOf('?') > 0) {
href = href.substring(0, href.indexOf('?'));
}
href += toQueryString(params);
liElement = $("<li><a id=\"" + id + "\" href=\"" + href + "\">" + text + "</a></li>");
}
liElement.bind('click', function () {
triggerEvent({name:'onChange', selectedItem: $(this).children(":first").attr('id'), element:rootElement, instance:ls});
doAction($(this));
});
if (settings.openMode == 'hover') {
liElement.hover(function () {
suspendCloseAction();
}, function () {
resumeCloseAction();
});
}
return liElement;
}
function parseQueryString() {
var params = {};
var query = window.location.search.substr(1).split('&');
if (query.length > 0) {
for (var i = 0; i < query.length; ++i) {
var p = query[i].split('=');
if (p.length != 2) {
continue;
}
params[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
}
return params;
}
function toQueryString(params) {
if (settings.testMode) {
return '#';
} else {
var queryString = '?';
var i = 0;
for (var param in params) {
var x = '';
if (i > 0) {
x = '&';
}
queryString += x + param + "=" + params[param];
i++;
}
return queryString;
}
}
ls.open = function () {
open();
};
ls.close = function () {
close();
};
triggerEvent({name:'afterLoad', element:rootElement, instance:ls});
return ls;
};
/**
* jquery.timer.js
*
* Copyright (c) 2011 Jason Chavannes <jason.chavannes@gmail.com>
*
* http://jchavannes.com/jquery-timer
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
$.timer=function(func,time,autostart){this.set=function(func,time,autostart){this.init=true;if(typeof func=="object"){var paramList=["autostart","time"];for(var arg in paramList){if(func[paramList[arg]]!=undefined){eval(paramList[arg]+" = func[paramList[arg]]")}}func=func.action}if(typeof func=="function"){this.action=func}if(!isNaN(time)){this.intervalTime=time}if(autostart&&!this.active){this.active=true;this.setTimer()}return this};this.once=function(time){var timer=this;if(isNaN(time)){time=0}window.setTimeout(function(){timer.action()},time);return this};this.play=function(reset){if(!this.active){if(reset){this.setTimer()}else{this.setTimer(this.remaining)}this.active=true}return this};this.pause=function(){if(this.active){this.active=false;this.remaining-=new Date()-this.last;this.clearTimer()}return this};this.stop=function(){this.active=false;this.remaining=this.intervalTime;this.clearTimer();return this};this.toggle=function(reset){if(this.active){this.pause()}else{if(reset){this.play(true)}else{this.play()}}return this};this.reset=function(){this.active=false;this.play(true);return this};this.clearTimer=function(){window.clearTimeout(this.timeoutObject)};this.setTimer=function(time){var timer=this;if(typeof this.action!="function"){return}if(isNaN(time)){time=this.intervalTime}this.remaining=time;this.last=new Date();this.clearTimer();this.timeoutObject=window.setTimeout(function(){timer.go()},time)};this.go=function(){if(this.active){this.action();this.setTimer()}};if(this.init){return new $.timer(func,time,autostart)}else{this.set(func,time,autostart);return this}};
/* ---------------------------------------------------------------------- */
/* "Polyglot" Language Switcher
/* ----------------------------------------------------------------------
Version: 2.2
Author: Ixtendo
Author URI: http://www.ixtendo.com
License: MIT License
License URI: http://www.opensource.org/licenses/mit-license.php
------------------------------------------------------------------------- */
$.fn.polyglotLanguageSwitcher=function(h){var g=$.fn.polyglotLanguageSwitcher;var e=$(this);var n=$(this).attr("id");var p;var x=$('<ul class="dropdown">');var d=0;var m=false;var r=[];var w=$.extend({},g.defaults,h);var v;var a=w.websiteType=="static";u();q();function b(y){if(w[y.name]){w[y.name].call($(this),y)}}function k(){if(!m){b({name:"beforeOpen",element:e,instance:g});p.addClass("active");o(true);setTimeout(function(){m=true;b({name:"afterOpen",element:e,instance:g})},100)}}function l(){if(m){b({name:"beforeClose",element:e,instance:g});o(false);p.removeClass("active");m=false;if(v&&v.active){v.clearTimer()}b({name:"afterClose",element:e,instance:g})}}function c(){if(v&&v.active){v.pause()}}function f(){if(v){v.play(false)}}function o(y){if(w.effect=="fade"){if(y){x.fadeIn(w.animSpeed)}else{x.fadeOut(w.animSpeed)}}else{if(y){x.slideDown(w.animSpeed)}else{x.slideUp(w.animSpeed)}}}function j(C){l();var y=$(C).children(":first-child");var z=$(y).attr("id");var D=$(y).text();$(x).children().each(function(){$(this).detach()});for(var A=0;A<r.length;A++){if($(r[A]).children(":first-child").attr("id")!=z){x.append(r[A])}}var B=p.children(":first-child");p.attr("id",z);p.text(D);p.append(B)}function q(){$(document).click(function(){l()});$(document).keyup(function(y){if(y.which==27){l()}});if(w.openMode=="hover"){v=$.timer(function(){l()});v.set({time:w.hoverTimeout,autostart:true})}}function u(){var B;var A=$("#"+n+" > form > select > option");if(a){var y;var z=window.location.href;A.each(function(){var C=$(this).attr("id");if(z.indexOf("/"+C+"/")>=0){y=C}})}A.each(function(){var E=$(this).attr("id");var C;if(a){C=y===E}else{C=$(this).attr("selected")}var D=t($(this));if(C){B=D}r.push(D);if(d>0){x.append(D)}else{p=$('<a id="'+$(this).attr("id")+'" class="current" href="#">'+$(this).text()+' <span class="trigger">&raquo;</span></a>');if(w.openMode=="hover"){p.hover(function(){k();c()},function(){f()})}else{p.click(function(){k()})}}d++});$("#"+n+" form:first-child").remove();e.append(p);e.append(x);if(B){j(B)}}function t(C){var A=$(C).attr("id");var E=$(C).attr("value");var F=$(C).text();var G;if(a){var z=window.location.href;var D=z.substring(z.lastIndexOf("/")+1);var H="http://"+document.domain+"/"+w.pagePrefix+A+"/"+D;G=$('<li><a id="'+A+'" href="'+H+'">'+F+"</a></li>")}else{var y=document.URL.replace("#","");var B=s();B[w.paramName]=E;if(y.indexOf("?")>0){y=y.substring(0,y.indexOf("?"))}y+=i(B);G=$('<li><a id="'+A+'" href="'+y+'">'+F+"</a></li>")}G.bind("click",function(){b({name:"onChange",selectedItem:$(this).children(":first").attr("id"),element:e,instance:g});j($(this))});if(w.openMode=="hover"){G.hover(function(){c()},function(){f()})}return G}function s(){var B={};var z=window.location.search.substr(1).split("&");if(z.length>0){for(var y=0;y<z.length;++y){var A=z[y].split("=");if(A.length!=2){continue}B[A[0]]=decodeURIComponent(A[1].replace(/\+/g," "))}}return B}function i(B){if(w.testMode){return"#"}else{var C="?";var z=0;for(var A in B){var y="";if(z>0){y="&"}C+=y+A+"="+B[A];z++}return C}}g.open=function(){k()};g.close=function(){l()};b({name:"afterLoad",element:e,instance:g});return g};
var ls = $.fn.polyglotLanguageSwitcher;
ls.defaults = {
@ -388,6 +56,4 @@
beforeClose:NaN,
afterClose:NaN
};
})(jQuery);

@ -85,7 +85,7 @@
<div id="langswitch">
<form action="#" method="post">
<select name="lang">
<?=printlangs()?>
<?=$l->printLanguageOptions()?>
</select>
</form>
</div>

Loading…
Cancel
Save