aboutsummaryrefslogtreecommitdiffstats
path: root/htdocs
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2021-10-20 22:49:22 +0200
committerArmand Philippot <git@armandphilippot.com>2021-10-20 22:49:22 +0200
commitc14da77997880189e6f4b7012d40dec227b9b225 (patch)
tree114dbe1f13d51dcdae2bc9e0e4f69a71178adb31 /htdocs
parentd439d65db1c4f56aa5bafa5df6b595855501fcb9 (diff)
chore: add php template and includes (config, i18n and utils)
Diffstat (limited to 'htdocs')
-rw-r--r--htdocs/includes/config.php19
-rw-r--r--htdocs/includes/i18n.php150
-rw-r--r--htdocs/includes/utils.php31
-rw-r--r--htdocs/index.php88
4 files changed, 288 insertions, 0 deletions
diff --git a/htdocs/includes/config.php b/htdocs/includes/config.php
new file mode 100644
index 0000000..cf7dba6
--- /dev/null
+++ b/htdocs/includes/config.php
@@ -0,0 +1,19 @@
+<?php
+/**
+ * Config of demo.armandphilippot.com
+ *
+ * @package DemoArmandphilippotCom
+ * @author Armand Philippot <contact@armandphilippot.com>
+ * @copyright 2021 Armand Philippot
+ * @license http://www.opensource.org/licenses/mit-license.php MIT
+ * @link https://demo.armandphilippot.com/
+ * @since 1.0.0
+ */
+
+/**
+ * The website will check if the HTTP accepted locales have a matching
+ * translation. If not, the default locale is "en_US". If you want to
+ * overwrite the default locale, use this variable. Make sure the translation
+ * exists.
+ */
+$dap_default_locale = 'en_US';
diff --git a/htdocs/includes/i18n.php b/htdocs/includes/i18n.php
new file mode 100644
index 0000000..37dee90
--- /dev/null
+++ b/htdocs/includes/i18n.php
@@ -0,0 +1,150 @@
+<?php
+/**
+ * Functions to translate demo.armandphilippot.com
+ *
+ * @package DemoArmandphilippotCom
+ * @author Armand Philippot <contact@armandphilippot.com>
+ * @copyright 2021 Armand Philippot
+ * @license http://www.opensource.org/licenses/mit-license.php MIT
+ * @link https://demo.armandphilippot.com/
+ * @since 1.0.0
+ */
+
+/**
+ * Get a list of available translations.
+ *
+ * @return array The available languages.
+ */
+function dap_get_available_languages()
+{
+ $languages_dir = glob('languages' . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
+ $languages = str_replace('languages' . DIRECTORY_SEPARATOR, '', $languages_dir);
+
+ return $languages;
+}
+
+/**
+ * Get accepted languages based on HTTP header.
+ *
+ * @return array The accepted languages.
+ */
+function dap_get_languages_from_http()
+{
+ $languages = '';
+
+ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+ $languages = preg_split('/(,|;)/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], -1, PREG_SPLIT_NO_EMPTY);
+ $languages = array_filter(
+ $languages,
+ function ($value) {
+ return substr($value, 0, 2) !== 'q=';
+ }
+ );
+ }
+
+ return $languages;
+}
+
+/**
+ * Check if a locale is a valid locale.
+ *
+ * @param string $locale The locale to test.
+ * @param array $valid_locales The valid locales.
+ * @return boolean True if the locale exists.
+ */
+function dap_is_valid_locale(string $locale, array $valid_locales) : bool
+{
+ if (empty($locale)) {
+ return false;
+ }
+
+ return in_array($locale, $valid_locales, true);
+}
+
+/**
+ * Filter the accepted locales to determine the locale to use.
+ *
+ * @param array $locales The locales to test.
+ * @param string $default_locale The default locale.
+ * @return string The first valid locale.
+ */
+function dap_get_valid_locale(array $locales, string $default_locale) : string
+{
+ $valid_locales = dap_get_available_languages();
+
+ if (isset($default_locale) && dap_is_valid_locale($default_locale, $valid_locales)) {
+ $default_locale = $default_locale;
+ } else {
+ $default_locale = 'en_US';
+ }
+
+ $possible_locales = array_intersect($locales, $valid_locales);
+ $possible_locales = array_values($possible_locales);
+ $locale = '';
+
+ if (count($possible_locales) > 0) {
+ $locale = $possible_locales[0];
+ } else {
+ $locale = $default_locale;
+ }
+
+ return $locale;
+}
+
+/**
+ * Define the locale to use.
+ *
+ * @param string $default_locale The default locale.
+ * @return string The locale to use.
+ */
+function dap_define_locale(string $default_locale)
+{
+ $http_language = dap_get_languages_from_http();
+ $http_locale = str_replace('-', '_', $http_language);
+ $http_locale = is_array($http_locale) ? $http_locale : array($http_locale);
+ $locale = dap_get_valid_locale($http_locale, $default_locale);
+
+ return $locale;
+}
+
+/**
+ * Get a list of possible format based on the locale to use.
+ *
+ * @param string $locale The locale to use.
+ * @return array The locale in different formats to use.
+ */
+function dap_get_formatted_locale(string $locale)
+{
+ $formatted_locale = array( $locale );
+ $formatted_locale[] = $locale . '.utf8';
+ $formatted_locale[] = $locale . '.UTF-8';
+ $formatted_locale[] = strtok($locale, '_');
+
+ return $formatted_locale;
+}
+
+/**
+ * Transform a locale format (ex: `en_US`) to a language format (ex: `en-US`).
+ *
+ * @param string $locale The used locale.
+ * @return string The language to use.
+ */
+function dap_get_language(string $locale)
+{
+ $language = str_replace('_', '-', $locale);
+
+ return $language;
+}
+
+/**
+ * Set the locale.
+ *
+ * @param array $formatted_locale The locale with its different formats.
+ */
+function dap_set_locale(array $formatted_locale)
+{
+ setlocale(LC_ALL, $formatted_locale);
+ bindtextdomain('DAP', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'languages');
+ bind_textdomain_codeset('DAP', 'UTF-8');
+ textdomain('DAP');
+}
diff --git a/htdocs/includes/utils.php b/htdocs/includes/utils.php
new file mode 100644
index 0000000..8b661d7
--- /dev/null
+++ b/htdocs/includes/utils.php
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Utilities for demo.armandphilippot.com
+ *
+ * @package DemoArmandphilippotCom
+ * @author Armand Philippot <contact@armandphilippot.com>
+ * @copyright 2021 Armand Philippot
+ * @license http://www.opensource.org/licenses/mit-license.php MIT
+ * @link https://demo.armandphilippot.com/
+ * @since 1.0.0
+ */
+
+/**
+ * Get current environment defined in .env file.
+ *
+ * @since 1.0.0
+ *
+ * @return string Current env or empty string.
+ */
+function dap_get_current_env()
+{
+ if (file_exists(__DIR__ . '/vendor/autoload.php')) {
+ require_once __DIR__ . '/vendor/autoload.php';
+ $dap_dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
+ $dap_dotenv->safeLoad();
+ $dap_current_env = $_ENV['WP_THEME_ENV'];
+ return $dap_current_env;
+ } else {
+ return '';
+ }
+}
diff --git a/htdocs/index.php b/htdocs/index.php
new file mode 100644
index 0000000..8e6c870
--- /dev/null
+++ b/htdocs/index.php
@@ -0,0 +1,88 @@
+<?php
+/**
+ * Main entry of demo.armandphilippot.com
+ *
+ * @package DemoArmandphilippotCom
+ * @author Armand Philippot <contact@armandphilippot.com>
+ * @copyright 2021 Armand Philippot
+ * @license http://www.opensource.org/licenses/mit-license.php MIT
+ * @link https://demo.armandphilippot.com/
+ * @since 1.0.0
+ */
+
+// Load config and functions.
+require './includes/config.php';
+require './includes/i18n.php';
+require './includes/utils.php';
+
+$dap_current_env = dap_get_current_env();
+$dap_locale = dap_define_locale($dap_default_locale);
+$dap_language = dap_get_language($dap_locale);
+$dap_formatted_locale = dap_get_formatted_locale($dap_locale);
+
+dap_set_locale($dap_formatted_locale);
+?>
+<!DOCTYPE html>
+<html lang="en">
+
+ <head>
+ <meta charset="UTF-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>Armand Philippot</title>
+ <?php if ($dap_current_env === "production") { ?>
+ <link rel="stylesheet" href="assets/css/style.css">
+ <?php } else { ?>
+ <script src="assets/js/style.js"></script>
+ <?php } ?>
+ </head>
+
+ <body class="body">
+ <button type="button" class="btn btn--toggle">
+ <?php echo htmlspecialchars(_('Menu')); ?>
+ </button>
+ <header class="header">
+ <div class="branding">
+ <div class="branding__logo logo">
+ <a href="/" rel="home" class="logo__link">
+ <img src="./assets/images/armand-philippot.jpg"
+ alt="<?php echo htmlspecialchars(_('Back to homepage')); ?>"
+ class="logo__image logo__image--front">
+ <img src="./assets/images/armand-philippot-logo.svg"
+ alt="<?php echo htmlspecialchars(_('Back to homepage')); ?>"
+ class="logo__image logo__image--back">
+ </a>
+ </div>
+ <h1 class="branding__title">
+ <a href="/" rel="home" class="branding__link">Armand Philippot</a>
+ </h1>
+ <p class="branding__description">
+ <?php echo htmlspecialchars(_('Front-end developer')); ?>
+ </p>
+ </div>
+ <nav class="nav">
+ <a href="https://www.armandphilippot.com/" class="nav__link nav__link--back">
+ <?php echo htmlspecialchars(_('Back to ArmandPhilippot.com')); ?>
+ </a>
+ <p class="nav__label"><?php echo htmlspecialchars(_('Projects list:')); ?>
+ </p>
+ <ul class="nav__list">
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <div class="instructions"><?php echo htmlspecialchars(_('Select a project inside menu to see a live preview and a link to the source code.')); ?>
+ </div>
+ </main>
+ <footer class="footer">
+ <div class="copyright">
+ <span class="copyright__author">Armand Philippot.</span>
+ <img src="assets/images/cc-by-sa.svg" alt="CC BY SA" class="copyright__icon">
+ <span class="copyright__date">2021.</span>
+ </div>
+ </footer>
+ <script src="./assets/js/runtime.js"></script>
+ <script src="./assets/js/app.js"></script>
+ </body>
+
+</html>