From 11e58b71bc0dfcabb96e80e65f6aa9e08334623d Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Tue, 29 Sep 2009 18:12:19 +0300 Subject: [PATCH] Feature #306 - Moving the function api_get_language_isocode() in the internationalization library. Deprecation of Database::get_language_isocode(). Making a formal test for api_get_language_isocode(). At the moment the test fails, some expexted values are not returned. This has to be fixed. --- main/inc/lib/database.lib.php | 44 +++------ main/inc/lib/internationalization.lib.php | 54 ++++++----- tests/main/inc/lib/database.lib.test.php | 6 -- .../inc/lib/internationalization.lib.test.php | 89 +++++++++++++++++++ tests/main/inc/lib/main_api.lib.test.php | 10 --- 5 files changed, 134 insertions(+), 69 deletions(-) diff --git a/main/inc/lib/database.lib.php b/main/inc/lib/database.lib.php index f552dde323..099ed54aa7 100644 --- a/main/inc/lib/database.lib.php +++ b/main/inc/lib/database.lib.php @@ -459,36 +459,6 @@ class Database { return self::format_table_name(self::get_user_personal_database(), $short_table_name); } - // TODO: This method should not belong to Database class. The internationaization ibrary is a better place. - /** - * Returns the isocode corresponding to the language directory given. - * @param string $language This is the name of the folder containing translations for the corresponding language (e.g arabic, english). - * If $language is omitted, interface language is assumed then. - * @return string The found isocode or null on error.. - * Returned codes are according to the following standards (in order of preference): - * - ISO 639-1 : Alpha-2 code (two-letters code - en, fr, es, ...) - * - RFC 4646 : five-letter code based on the ISO 639 two-letter language codes - * and the ISO 3166 two-letter territory codes (pt-BR, ...) - * - ISO 639-2 : Alpha-3 code (three-letters code - ast, fur, ...) - */ - public static function get_language_isocode($language) { - static $iso_code = array(); - if (empty($language)) { - $language = api_get_interface_language(); - } - if (!isset($iso_code[$language])) { - $table = self::get_main_table(TABLE_MAIN_LANGUAGE); - $sql_result = self::query("SELECT isocode FROM $table WHERE dokeos_folder = '$language'", __FILE__, __LINE__); - if (self::num_rows($sql_result)) { - $result = self::fetch_array($sql_result); - $iso_code[$language] = $result['isocode']; - } else { - $iso_code[$language] = null; - } - } - return $iso_code[$language]; - } - /* ----------------------------------------------------------------------------- Query Functions @@ -833,5 +803,19 @@ class Database { return $array; } + + /* + ============================================================================== + DEPRECATED METHODS + ============================================================================== + */ + + /** + * @deprecated Use api_get_language_isocode($language) instead. + */ + public static function get_language_isocode($language) { + return api_get_language_isocode($language); + } + } //end class Database diff --git a/main/inc/lib/internationalization.lib.php b/main/inc/lib/internationalization.lib.php index ca07cdd3d7..a36debebda 100644 --- a/main/inc/lib/internationalization.lib.php +++ b/main/inc/lib/internationalization.lib.php @@ -212,22 +212,6 @@ function get_lang($variable, $notrans = 'DLTT', $language = null) { $variable."&language=".$language."\" target=\"_blank\" style=\"color:#FF0000\">#"); } -// TODO: Database::get_language_isocode() to be deprecated. -/** - * Gets language isocode column from the language table, taking the current language as a query parameter. - * @param string $language This is the name of the folder containing translations for the corresponding language (e.g arabic, english). - * If $language is omitted, interface language is assumed then. - * @return string The found isocode or null on error. - * Returned codes are according to the following standards (in order of preference): - * - ISO 639-1 : Alpha-2 code (two-letters code - en, fr, es, ...) - * - RFC 4646 : five-letter code based on the ISO 639 two-letter language codes - * and the ISO 3166 two-letter territory codes (pt-BR, ...) - * - ISO 639-2 : Alpha-3 code (three-letters code - ast, fur, ...) - */ -function api_get_language_isocode($language = null) { - return Database::get_language_isocode($language); -} - /** * Gets the current interface language. * @param bool $purified (optional) When it is true, a purified (refined) language value will be returned, for example 'french' instead of 'french_unicode'. @@ -235,13 +219,7 @@ function api_get_language_isocode($language = null) { */ function api_get_interface_language($purified = false) { global $language_interface; - if (empty($language_interface)) { - return 'english'; - } - if ($purified) { - return api_refine_language_id($language_interface); - } - return $language_interface; + return empty($language_interface) ? 'english' : ($purified ? api_refine_language_id($language_interface) : $language_interface); } /** @@ -289,6 +267,36 @@ function api_refine_language_id($language) { return $purified[$language]; } +// TODO: To be added a sanity check whether Database class has been loaded. +/** + * Gets language isocode column from the language table, taking the current language as a query parameter. + * @param string $language This is the name of the folder containing translations for the corresponding language (e.g arabic, english). + * If $language is omitted, interface language is assumed then. + * @return string The found isocode or null on error. + * Returned codes are according to the following standards (in order of preference): + * - ISO 639-1 : Alpha-2 code (two-letters code - en, fr, es, ...) + * - RFC 4646 : five-letter code based on the ISO 639 two-letter language codes + * and the ISO 3166 two-letter territory codes (pt-BR, ...) + * - ISO 639-2 : Alpha-3 code (three-letters code - ast, fur, ...) + */ +function api_get_language_isocode($language = null) { + static $iso_code = array(); + if (empty($language)) { + $language = api_get_interface_language(); + } + if (!isset($iso_code[$language])) { + $table = Database::get_main_table(TABLE_MAIN_LANGUAGE); + $sql_result = Database::query("SELECT isocode FROM $table WHERE dokeos_folder = '$language'", __FILE__, __LINE__); + if (Database::num_rows($sql_result)) { + $result = Database::fetch_array($sql_result); + $iso_code[$language] = $result['isocode']; + } else { + $iso_code[$language] = null; + } + } + return $iso_code[$language]; +} + /** * This function check whether a given language can use Latin 1 encoding. * @param string $language The checked language. diff --git a/tests/main/inc/lib/database.lib.test.php b/tests/main/inc/lib/database.lib.test.php index 35518ab1ab..95da1062ae 100644 --- a/tests/main/inc/lib/database.lib.test.php +++ b/tests/main/inc/lib/database.lib.test.php @@ -185,12 +185,6 @@ class TestDatabase extends UnitTestCase { $this->assertTrue(is_string($res)); } - function testGetLanguageIsocode() { - $lang_folder='arabic'; - $res=$this->dbase->get_language_isocode($lang_folder); - $this->assertTrue(is_string($res)); - } - function testGetLastInsertId() { $res=$this->dbase->get_last_insert_id(); $this->assertTrue(is_numeric($res)); diff --git a/tests/main/inc/lib/internationalization.lib.test.php b/tests/main/inc/lib/internationalization.lib.test.php index 7feb2e302f..ba8e3444bb 100644 --- a/tests/main/inc/lib/internationalization.lib.test.php +++ b/tests/main/inc/lib/internationalization.lib.test.php @@ -1041,6 +1041,95 @@ class TestInternationalization extends UnitTestCase { //var_dump($res); } + function test_api_get_language_isocode() { + $test_language_table = array( + '*** invalid entry ***' => null, // An invalid entry. + 'arabic' => 'ar', + 'arabic_unicode' => 'ar', + 'asturian' => 'ast', + 'bosnian' => 'bs', + 'brazilian' => 'pt-BR', + 'bulgarian' => 'bg', + 'catalan' => 'ca', + 'croatian' => 'hr', + 'czech' => 'cs', + 'danish' => 'da', + 'dari' => 'prs', + 'dutch' => 'nl', + 'dutch_corporate' => 'nl', + 'english' => 'en', + 'english_org' => 'en', + 'esperanto' => 'eo', + 'euskera' => 'eu', + 'finnish' => 'fi', + 'french' => 'fr', + 'french_corporate' => 'fr', + 'french_KM' => 'fr', + 'french_org' => 'fr', + 'french_unicode' => 'fr', + 'friulian' => 'fur', + 'galician' => 'gl', + 'georgian' => 'ka', + 'german' => 'de', + 'greek' => 'el', + 'hebrew' => 'he', + 'hungarian' => 'hu', + 'indonesian' => 'id', + 'italian' => 'it', + 'japanese' => 'ja', + 'japanese_unicode' => 'ja', + 'korean' => 'kr', + 'latvian' => 'lv', + 'lithuanian' => 'lt', + 'macedonian' => 'mk', + 'malay' => 'ms', + 'norwegian' => 'no', + 'occitan' => '', + 'pashto' => 'ps', + 'persian' => 'fa', + 'polish' => 'pl', + 'portuguese' => 'pt', + 'quechua_cusco' => 'qu', + 'romanian' => 'ro', + 'russian' => 'ru', + 'russian_unicode' => 'ru', + 'serbian' => 'sr', + 'simpl_chinese' => 'zh', + 'simpl_chinese_unicode' => 'zh', + 'slovak' => 'sk', + 'slovenian' => 'sl', + 'slovenian_unicode' => 'sl', + 'spanish' => 'es', + 'spanish_latin' => '', + 'swahili' => 'sw', + 'swedish' => 'sv', + 'thai' => 'th', + 'trad_chinese' => '', + 'trad_chinese_unicode' => '', + 'turkce' => 'tr', + 'ukrainian' => 'uk', + 'vietnamese' => 'vi', + 'yoruba' => 'yo' + ); + $res = array(); + foreach ($test_language_table as $language => $expected_result) { + $test_result = api_get_language_isocode($language); + $res[$language] = array( + 'expected_result' => $expected_result, + 'test_result' => $test_result, + 'is_ok' => $expected_result === $test_result + ); + } + $this->assertTrue(is_array($res)); + $is_ok = true; + foreach ($res as $language => $test_case) { + $is_ok = $is_ok && $test_case['is_ok']; + } + $this->assertTrue($is_ok); + //var_dump($res); + //foreach ($res as $language => $test_case) { echo ($test_case['is_ok'] ? 'Ok' : 'Failed').' '.$language.' => '.(is_null($test_case['test_result']) ? 'NULL' : $test_case['test_result']).'
'; } + } + public function test_api_is_latin1_compatible() { $language = 'portuguese'; $res = api_is_latin1_compatible($language); diff --git a/tests/main/inc/lib/main_api.lib.test.php b/tests/main/inc/lib/main_api.lib.test.php index 467c44c195..02a799bb02 100755 --- a/tests/main/inc/lib/main_api.lib.test.php +++ b/tests/main/inc/lib/main_api.lib.test.php @@ -584,16 +584,6 @@ class TestMainApi extends UnitTestCase { $this->assertFalse($row); } - function testApiGetLanguageIsocode(){ - $query=''; - $sql= true; - $var=Database::query($sql,_FILE_,_LINE_); - $res=api_get_language_isocode($query); - $this->assertTrue(is_string($query)); - $this->assertTrue(isset($var)); - //var_dump($query); - } - function testApiGetThemes(){ $cssdir= api_get_path(SYS_PATH).'main/css/'; $res=api_get_themes();