Add template doc, fixup rendering locale select

This commit is contained in:
Vojtěch Sajdl 2018-01-07 20:43:04 +01:00
parent 61f563c9b0
commit 5dd9a2a1bd
No known key found for this signature in database
GPG Key ID: 5D4EB1361A272390
3 changed files with 65 additions and 50 deletions

View File

@ -14,12 +14,13 @@ class LocaleNegotiator
*/ */
function __construct($default_language) function __construct($default_language)
{ {
$tmp = glob(__DIR__ . '/locale/*' , GLOB_ONLYDIR); $tmp = glob(__DIR__ . '/../locale/*' , GLOB_ONLYDIR);
$this->default_language = $default_language; $this->default_language = $default_language;
//Works only if the server supports the locale //Works only if the server supports the locale
//This basically means $accepted_langs[<lang_code>] = "<lang name>"; //This basically means $accepted_langs[<lang_code>] = "<lang name>";
foreach ($accepted_langs as $key => $value) { foreach ($tmp as $key => $value) {
$this->accepted_langs[basename($value)] = self::mb_ucfirst(locale_get_display_language($lang, $lang)); $lang = basename($value);
$this->accepted_langs[$lang] = self::mb_ucfirst(locale_get_display_language($lang, $lang));
} }
} }

View File

@ -11,10 +11,11 @@ define("DEFAULT_LANGUAGE", "en_GB");
require("classes/locale-negotiator.php"); require("classes/locale-negotiator.php");
$negotiator = new LocaleNegotiator(DEFAULT_LANGUAGE);
if (!isset($_SESSION['locale'])||isset($_GET['lang'])) if (!isset($_SESSION['locale'])||isset($_GET['lang']))
{ {
$override = ((isset($_GET['lang']))?$_GET['lang']:null); $override = ((isset($_GET['lang']))?$_GET['lang']:null);
$negotiator = new LocaleNegotiator(DEFAULT_LANGUAGE);
$best_match = $negotiator->negotiate($override); $best_match = $negotiator->negotiate($override);
$_SESSION['locale'] = $best_match; $_SESSION['locale'] = $best_match;
setlocale(LC_ALL, $_SESSION['locale'].".UTF-8"); setlocale(LC_ALL, $_SESSION['locale'].".UTF-8");

View File

@ -1,5 +1,13 @@
<?php <?php
class Template(){ /**
* Class that encapsulates methods to render header and footer
*/
class Template{
/**
* Renders header
* @param String $page_name name of the page to be displayed as title
* @param Boolean $admin decides whether to show admin menu
*/
public static function render_header($page_name, $admin = false){ public static function render_header($page_name, $admin = false){
if (!$admin) if (!$admin)
{ {
@ -73,52 +81,57 @@ class Template(){
</div> </div>
</div> </div>
<div id="wrapper" class="center admin"> <div id="wrapper" class="center admin">
<?php <?php
} }
} }
public static function render_footer($admin = false) /**
{ * Renders footer
global $lang_names; * @param Boolean $admin decides whether to load admin scripts
?> */
</div> public static function render_footer($admin = false)
<div id="footerwrap"> {
<div class="container"> global $negotiator;
<div class="row centered"> $lang_names = $negotiator->get_accepted_langs();
<div class="col-md-4 text-left">Copyright © <?php echo date("Y");?> Vojtěch Sajdl</div> ?>
<div class="col-md-4 text-center"> </div>
<div class="btn-group dropup"> <div id="footerwrap">
<button type="button" class="btn btn-primary"><?php echo '<img src="'.WEB_URL.'/locale/'.$_SESSION['locale'].'/flag.png" alt="'.$lang_names[$_SESSION['locale']].'">'.$lang_names[$_SESSION['locale']];?></button> <div class="container">
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <div class="row centered">
<span class="caret"></span> <div class="col-md-4 text-left">Copyright © <?php echo date("Y");?> Vojtěch Sajdl</div>
<span class="sr-only"><?php echo _("Toggle Dropdown");?></span> <div class="col-md-4 text-center">
</button> <div class="btn-group dropup">
<ul class="dropdown-menu"> <button type="button" class="btn btn-primary"><?php echo '<img src="'.WEB_URL.'/locale/'.$_SESSION['locale'].'/flag.png" alt="'.$lang_names[$_SESSION['locale']].'">'.$lang_names[$_SESSION['locale']];?></button>
<?php <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
foreach ($lang_names as $key => $value) { <span class="caret"></span>
echo '<a href="?lang='.$key.'"><img src="'.WEB_URL.'/locale/'.$key.'/flag.png" alt="'.$value.'">'.$value.'</a>'; <span class="sr-only"><?php echo _("Toggle Dropdown");?></span>
} </button>
?> <ul class="dropdown-menu">
<hr role="separator" class="divider"> <?php
<a href="https://poeditor.com/join/project/37SpmJtyOm"><?php echo _("Help with translation!");?></a> foreach ($lang_names as $key => $value) {
</ul> echo '<a href="?lang='.$key.'"><img src="'.WEB_URL.'/locale/'.$key.'/flag.png" alt="'.$value.'">'.$value.'</a>';
</div> }
?>
<hr role="separator" class="divider">
<a href="https://poeditor.com/join/project/37SpmJtyOm"><?php echo _("Help with translation!");?></a>
</ul>
</div> </div>
<div class="col-md-4 text-right"><a href="https://github.com/Pryx/server-status/" target="_blank"><i class="fa fa-github" aria-hidden="true"></i></a></div> </div>
</div><!--/row --> <div class="col-md-4 text-right"><a href="https://github.com/Pryx/server-status/" target="_blank"><i class="fa fa-github" aria-hidden="true"></i></a></div>
</div><!--/container --> </div><!--/row -->
</div> </div><!--/container -->
<script src="/js/vendor/jquery-1.11.2.min.js"></script> </div>
<script src="/js/vendor/jquery.timeago.js"></script> <script src="/js/vendor/jquery-1.11.2.min.js"></script>
<?php if ($admin){?> <script src="/js/vendor/jquery.timeago.js"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script> <?php if ($admin){?>
<script src="/js/admin.js"></script> <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script src="/js/vendor/jquery.growl.js"></script> <script src="/js/admin.js"></script>
<? }?> <script src="/js/vendor/jquery.growl.js"></script>
<script src="/js/vendor/bootstrap.min.js"></script> <? }?>
<script src="/js/main.js"></script> <script src="/js/vendor/bootstrap.min.js"></script>
</body> <script src="/js/main.js"></script>
</html> </body>
<? </html>
<?
} }
} }