diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/README.md b/README.md index a6fe53b..4965053 100644 --- a/README.md +++ b/README.md @@ -24,14 +24,9 @@ Recommended schedule: Translating: ------------ -Copy `lang_en.php` and rename it to `lang_YOUR_LANGCODE.php` -Then edit the file and translate the messages into your language and change $I to $T at the top. -If you ever use a ' character, you have to escape it by using \' instead, or the script will fail. -When you are done, you have to edit `common_config.php`, to include your translation. Simply add a line with -`'lang_code' => 'Language name',` -to the $L array below the settings, similar to what I did for the German translation. -Please share your translation with me, so I can add it to the official version. -To update your translation, you can copy each new string to your translation file or edit the automated `lang_update.php` script to reflect you language and run it. +The scrip `update-translations.sh` can be used to update the language template and translation files from source. +It will generate the file `locale/main-website.pot` which you can then use as basis to create a new language file in `YOUR_LANG_CODE/LC_MESSAGES/main-website.po` and edit it with a translation program, such as [Poedit](https://poedit.net/). +Once you are done, you can open a pull request, or [email me](mailto:daniel@danwin1210.de), to include the translation. Live Demo: ---------- diff --git a/common_config.php b/common_config.php index 3c5cbdc..7c33c0f 100644 --- a/common_config.php +++ b/common_config.php @@ -2,7 +2,7 @@ /* * Onion Link List - Configuration * -* Copyright (C) 2016-2020 Daniel Winzen +* Copyright (C) 2016-2020 Daniel Winzen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,54 +35,76 @@ const PER_PAGE = 50; // Sites listed per page const VERSION = '1.1'; // Script version const DBVERSION = 8; // Database layout version const REQUIRE_APPROVAL = false; // require admin approval of new sites? true/false -const CANONICAL_URL = 'https://onions.danwin1210.me'; // our preferred domain for search engines +const CANONICAL_URL = 'https://onions.danwin1210.de'; // our preferred domain for search engines //Categories - new links will always be put into the first one, leave it to Unsorted //once configured, only add new categories at the end or you have to manually adjust the database. $categories=['Unsorted', 'Adult/Porn', 'Communication/Social', 'Forums', 'Hacking/Programming/Software', 'Hosting', 'Libraries/Wikis', 'Link Lists', 'Market/Shop/Store', 'Other', 'Personal Sites/Blogs', 'Security/Privacy/Encryption', 'Whistleblowing', 'Empty/Error/Unknown', 'Cryptocurrencies', 'Scams', 'Fun/Games/Joke', 'Search']; // Language selection -$I = $T = []; -$language = LANG; -$L=[ - 'de' => 'Deutsch', - 'en' => 'English', - 'fa' => 'فارسی', - 'ja' => '日本語', - 'pt' => 'Português', - 'tr' => 'Türkçe', +const LANGUAGES = [ + 'de' => ['name' => 'Deutsch', 'locale' => 'de_DE', 'flag' => '🇩🇪', 'show_in_menu' => true, 'dir' => 'ltr'], + 'en' => ['name' => 'English', 'locale' => 'en_GB', 'flag' => '🇬🇧', 'show_in_menu' => true, 'dir' => 'ltr'], + 'fa' => ['name' => 'فارسی', 'locale' => 'fa_IR', 'flag' => '🇮🇷', 'show_in_menu' => true, 'dir' => 'rtl'], + 'ja' => ['name' => '日本語', 'locale' => 'ja_JP', 'flag' => '🇯🇵', 'show_in_menu' => true, 'dir' => 'ltr'], + 'pt' => ['name' => 'Português', 'locale' => 'pt_PT', 'flag' => '🇵🇹', 'show_in_menu' => true, 'dir' => 'ltr'], + 'tr' => ['name' => 'Türkçe', 'locale' => 'tr_TR', 'flag' => '🇹🇷', 'show_in_menu' => true, 'dir' => 'ltr'], ]; -if(isset($_REQUEST['lang']) && isset($L[$_REQUEST['lang']])){ - $language=$_REQUEST['lang']; - if(!isset($_COOKIE['language']) || $_COOKIE['language']!==$language){ - set_secure_cookie('language', $language); - } -}elseif(isset($_COOKIE['language']) && isset($L[$_COOKIE['language']])){ - $language=$_COOKIE['language']; -} -require_once(__DIR__.'/lang_en.php'); //always include English -if($language!=='en'){ - require_once(__DIR__."/lang_$language.php"); //replace with translation if available - foreach($T as $name=>$translation){ - $I[$name]=$translation; +$language = LANG; +$locale = LANGUAGES[LANG]['locale']; +$dir = LANGUAGES[LANG]['dir']; +if(isset($_REQUEST['lang']) && isset(LANGUAGES[$_REQUEST['lang']])){ + $locale = LANGUAGES[$_REQUEST['lang']]['locale']; + $language = $_REQUEST['lang']; + $dir = LANGUAGES[$_REQUEST['lang']]['dir']; + setcookie('language', $_REQUEST['lang'], ['expires' => 0, 'path' => '/', 'domain' => '', 'secure' => ($_SERVER['HTTPS'] ?? '' === 'on'), 'httponly' => true, 'samesite' => 'Strict']); +}elseif(isset($_COOKIE['language']) && isset(LANGUAGES[$_COOKIE['language']])){ + $locale = LANGUAGES[$_COOKIE['language']]['locale']; + $language = $_COOKIE['language']; + $dir = LANGUAGES[$_COOKIE['language']]['dir']; +}elseif(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){ + $prefLocales = array_reduce( + explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']), + function (array $res, string $el) { + list($l, $q) = array_merge(explode(';q=', $el), [1]); + $res[$l] = (float) $q; + return $res; + }, []); + arsort($prefLocales); + foreach($prefLocales as $l => $q){ + $lang = locale_lookup(array_keys(LANGUAGES), $l); + if(!empty($lang)){ + $locale = LANGUAGES[$lang]['locale']; + $language = $lang; + $dir = LANGUAGES[$lang]['dir']; + setcookie('language', $lang, ['expires' => 0, 'path' => '/', 'domain' => '', 'secure' => ($_SERVER['HTTPS'] ?? '' === 'on'), 'httponly' => true, 'samesite' => 'Strict']); + break; + } } } +putenv('LC_ALL='.$locale); +setlocale(LC_ALL, $locale); -function print_langs(){ - global $I, $L; - echo "