From a1bcc5b88c09dcbbfb2f2ad0d509d32731ff04c3 Mon Sep 17 00:00:00 2001 From: Jerome Jutteau Date: Thu, 8 Feb 2018 21:48:34 +0100 Subject: [PATCH] [TASK] Refactor lang engine - refactor small lang engine with less code duplication - prepare for strings to be expressed as a code instead of english - add english translation as a separated file ref #158 Signed-off-by: Jerome Jutteau --- lib/functions.js.php | 21 ++-- lib/lang.php | 114 +++++++++------------ lib/locales/en.json | 234 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 297 insertions(+), 72 deletions(-) create mode 100644 lib/locales/en.json diff --git a/lib/functions.js.php b/lib/functions.js.php index 79d7c95..2080386 100644 --- a/lib/functions.js.php +++ b/lib/functions.js.php @@ -28,12 +28,21 @@ require(JIRAFEAU_ROOT . 'lib/lang.php'); ?> var web_root = ""; -function translate (expr) -{ - var lang_array = ; - if (lang_array.hasOwnProperty(expr)) - return lang_array[expr]; - return expr; +var lang_array = ; +var lang_array_fallback = ; + +function translate (expr) { + if (lang_array.hasOwnProperty(expr)) { + var e = lang_array[expr]; + if (!isEmpty(e)) + return e; + } + if (lang_array_fallback.hasOwnProperty(expr)) { + var e = lang_array_fallback[expr]; + if (!isEmpty(e)) + return e; + } + return "FIXME: " + expr; } function isEmpty(str) { diff --git a/lib/lang.php b/lib/lang.php index 71bfbf0..1af1259 100644 --- a/lib/lang.php +++ b/lib/lang.php @@ -37,86 +37,68 @@ $languages_list = array('auto' => 'Automatic', 'tr' => 'Türkçe', 'zh' => '汉语'); -/* Translation */ -function t($text) +function t($string_id) { - $cfg = $GLOBALS['cfg']; - $languages_list = $GLOBALS['languages_list']; - - /* Detect user's langage if we are in automatic mode. */ - if (strcmp($cfg['lang'], 'auto') == 0) { - if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - $l = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); - } else { - $l = "en"; - } - } else { - $l = $cfg['lang']; - } - - /* Is the langage in the list ? */ - $found = false; - foreach ($languages_list as $key => $v) { - if (strcmp($l, $key) == 0) { - $found = true; + $r = t_in($string_id, t_select_lang()); + if ($r === false) { + $r = t_in($string_id, "en"); + if ($r === false) { + return ""; } } + return $r; +} - /* Don't translate english. */ - if (!($found && strcmp($l, "en"))) { - return $text; +function t_select_lang() { + $cfg = $GLOBALS['cfg']; + if (strcmp($cfg['lang'], 'auto') != 0) { + return $cfg['lang']; } - - /* Open translation file. */ - $trans_j = file_get_contents(JIRAFEAU_ROOT . "lib/locales/$l.json"); - if ($trans_j === false) { - return $text; + else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); + } else { + return "en"; } +} - /* Decode JSON. */ - $trans = json_decode($trans_j, true); - if ($trans === null) { - return $text; +function t_in($string_id, $lang) { + $trans = t_get_json($lang); + if ($trans === false) { + return false; } - - /* Try to find translation. */ - if (!array_key_exists($text, $trans)) { - return $text; + if (!array_key_exists($string_id, $trans)) { + return false; } - - return $trans[$text]; + return $trans[$string_id]; } -function json_lang_generator() -{ - $cfg = $GLOBALS['cfg']; - $languages_list = $GLOBALS['languages_list']; - - /* Detect user's langage if we are in automatic mode. */ - if (strcmp($cfg['lang'], 'auto') == 0) { - if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - $l = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); - } else { - $l = "en"; - } - } else { - $l = $cfg['lang']; +function t_get_raw_json($lang) { + $p = JIRAFEAU_ROOT . "lib/locales/$lang.json"; + if (!file_exists($p)) { + return false; } - - /* Is the langage in the list ? */ - $found = false; - foreach ($languages_list as $key => $v) { - if (strcmp($l, $key) == 0) { - $found = true; - } + $json = file_get_contents($p); + if ($json === false) { + return false; } + return $json; +} - /* Don't translate english. */ - if (!($found && strcmp($l, "en"))) { - return "{}"; +function t_get_json($lang) { + $raw_j = t_get_raw_json($lang); + $array = json_decode($raw_j, true); + if ($array === null) { + return false; } + return $array; +} - /* Open translation file. */ - $trans_j = file_get_contents(JIRAFEAU_ROOT . "lib/locales/$l.json"); - return $trans_j; +function json_lang_generator($lang) { + $r = ""; + if ($lang === null) { + $r = t_get_raw_json(t_select_lang()); + } else { + $r = t_get_raw_json($lang); + } + return $r === false ? "{}" : $r; } diff --git a/lib/locales/en.json b/lib/locales/en.json new file mode 100644 index 0000000..c2a3089 --- /dev/null +++ b/lib/locales/en.json @@ -0,0 +1,234 @@ +{ + "Jirafeau, your web file repository": + "Jirafeau, your web file repository", + "Select a file": + "Select a file", + "Send": + "Send", + "Uploading ...": + "Uploading ...", + "One time download": + "One time download", + "Password": + "Password", + "Time limit": + "Time limit", + "Maximum file size": + "Maximum file size", + "powered by Open-Source project Jirafeau": + "powered by Open-Source project Jirafeau", + "Made with": + "Made with", + "Jirafeau Project": + "Jirafeau Project", + "One minute": + "One minute", + "One hour": + "One hour", + "One day": + "One day", + "One week": + "One week", + "One month": + "One month", + "One quarter": + "One quarter", + "One year": + "One year", + "None": + "None", + "Upload password": + "Upload password", + "File is too big": + "File is too big", + "File size is limited to": + "File size is limited to", + "The file directory is not writable": + "The file directory is not writable", + "The link directory is not writable": + "The link directory is not writable", + "The async directory is not writable!": + "The async directory is not writable!", + "Installer script still present": + "Installer script still present", + "Please make sure to delete the installer script \"install.php\" before continuing.": + "Please make sure to delete the installer script \"install.php\" before continuing.", + "An error occurred.": + "An error occurred.", + "File uploaded !": + "File uploaded !", + "Download page": + "Download page", + "This file is valid until the following date": + "This file is valid until the following date", + "View link": + "View link", + "Direct download link": + "Direct download link", + "Delete link": + "Delete link", + "Download": + "Download", + "Preview": + "Preview", + "Sorry, the requested file is not found": + "Sorry, the requested file is not found", + "File not available.": + "File not available.", + "Confirm deletion": + "Confirm deletion", + "You are about to delete": + "You are about to delete", + "Delete": + "Delete", + "File has been deleted.": + "File has been deleted.", + "The time limit of this file has expired.": + "The time limit of this file has expired.", + "Password protection": + "Password protection", + "Give the password of this file": + "Give the password of this file", + "Access denied": + "Access denied", + "You are about to download": + "You are about to download", + "By using our services, you accept our": + "By using our services, you accept our", + "Terms of Service": + "Terms of Service", + "Warning, this file will self-destruct after being read": + "Warning, this file will self-destruct after being read", + "Internal error during file creation.": + "Internal error during file creation.", + "This file was generated by the install process. You can edit it. Please see config.original.php to understand the configuration items.": + "This file was generated by the install process. You can edit it. Please see config.original.php to understand the configuration items.", + "The following directory could not be created": + "The following directory could not be created", + "You should create this directory manually.": + "You should create this directory manually.", + "The following directory is not writable": + "The following directory is not writable", + "You should give the write permission to the web server on this directory.": + "You should give the write permission to the web server on this directory.", + "Here is a solution": + "Here is a solution", + "The local configuration file could not be created. Create a lib/config.local.php file and give the write permission to the web server (preferred solution), or give the write permission to the web server on the lib directory.": + "The local configuration file could not be created. Create a lib/config.local.php file and give the write permission to the web server (preferred solution), or give the write permission to the web server on the lib directory.", + "The local configuration is not writable by the web server. Give the write permission to the web server on the 'lib/config.local.php file.": + "The local configuration is not writable by the web server. Give the write permission to the web server on the 'lib/config.local.php file.", + "Installation of Jirafeau": + "Installation of Jirafeau", + "step": + "step", + "out of": + "out of", + "Administration password": + "Administration password", + "Finalisation": + "Finalisation", + "Jirafeau is setting the website according to the configuration you provided.": + "Jirafeau is setting the website according to the configuration you provided.", + "Previous step": + "Previous step", + "Retry this step": + "Retry this step", + "Jirafeau is now fully operational": + "Jirafeau is now fully operational", + "Information": + "Information", + "The base address of Jirafeau is the first part of the URL, until (and including) the last slash. For example: \"http://www.example.com/\". Do not forget the trailing slash!": + "The base address of Jirafeau is the first part of the URL, until (and including) the last slash. For example: \"http://www.example.com/\". Do not forget the trailing slash!", + "Base address": + "Base address", + "The data directory is where your files and information about your files will be stored. You should put it outside your web site, or at least restrict the access to this directory. Do not forget the trailing slash!": + "The data directory is where your files and information about your files will be stored. You should put it outside your web site, or at least restrict the access to this directory. Do not forget the trailing slash!", + "Data directory": + "Data directory", + "Jirafeau is internationalised. Choose a specific langage or choose Automatic (langage is provided by user's browser).": + "Jirafeau is internationalised. Choose a specific langage or choose Automatic (langage is provided by user's browser).", + "Choose the default language": + "Choose the default language", + "Next step": + "Next step", + "Jirafeau has an administration interface (through admin.php). You can set a password to access the interface or leave it empty to disable the interface.": + "Jirafeau has an administration interface (through admin.php). You can set a password to access the interface or leave it empty to disable the interface.", + "Administration password": + "Administration password", + "Sorry, the admin interface is not enabled.": + "Sorry, the admin interface is not enabled.", + "Sorry, you are not authenticated on admin interface.": + "Sorry, you are not authenticated on admin interface.", + "Login": + "Login", + "Wrong password.": + "Wrong password.", + "Admin interface": + "Admin interface", + "Clean expired files": + "Clean expired files", + "Clean old unfinished transfers": + "Clean old unfinished transfers", + "Clean": + "Clean", + "Search files by name": + "Search files by name", + "Search": + "Search", + "List all files": + "List all files", + "List": + "List", + "Actions": + "Actions", + "Search files by file hash": + "Search files by file hash", + "Search a specific link": + "Search a specific link", + "Number of cleaned files": + "Number of cleaned files", + "Logout": + "Logout", + "You are now loggued out": + "You are now loggued out", + "Link deleted": + "Link deleted", + "Filename": + "Filename", + "file": + "file", + "link": + "link", + "Type": + "Type", + "Size": + "Size", + "Expire": + "Expire", + "Onetime": + "Onetime", + "Upload date": + "Upload date", + "Origin": + "Origin", + "Action": + "Action", + "Del link": + "Del link", + "Del file and links": + "Del file and links", + "Deleted links": + "Deleted links", + "year": + "year", + "day": + "day", + "minute": + "minute", + "hour": + "hour", + "second": + "second", + "less than a second": + "less than a second" +} -- 2.34.1