/** * check if value in the given attribute is a valid language code * return true if valid, otherwise, return false */ public static function isValidLangCode($code) { // The allowed characters in a valid language code are letters, numbers or dash(-). if (!preg_match("/^[a-zA-Z0-9-]+\$/", $code)) { return false; } $code = BasicChecks::cutOutLangCode($code); $langCodesDAO = new LangCodesDAO(); if (strlen($code) == 2) { $rows = $langCodesDAO->GetLangCodeBy2LetterCode($code); } else { if (strlen($code) == 3) { $rows = $langCodesDAO->GetLangCodeBy3LetterCode($code); } else { return false; } } return is_array($rows); }
/** * Return a valid 3-character language code * 1. if input is a valid 3-character language code, return itself; * 2. if input is a valid 2-character language code, return according 3-character language code * 3. if input is an invalid language code, return default language code */ public static function get3LetterLangCode($code) { require_once TR_INCLUDE_PATH . 'classes/DAO/LangCodesDAO.class.php'; $langCodesDAO = new LangCodesDAO(); if (!Utility::isValidLangCode($code)) { return $_config['default_language']; } else { if (strlen($code) == 3) { return $code; } if (strlen($code) == 2) { $rows = $langCodesDAO->GetLangCodeBy2LetterCode($code); return $rows[0]['code_3letters']; } } }