From 9ad072b33fb091c7cad901849662d0b865a2410b Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 10 Feb 2009 09:10:55 +0000 Subject: [PATCH] Simplification pass that preserves almost all functionality (except default_country) but removes a lot of the code. --- core/controllers/admin_languages.php | 42 +++-- core/helpers/locale.php | 233 +++++++-------------------- modules/user/helpers/user.php | 11 +- 3 files changed, 80 insertions(+), 206 deletions(-) diff --git a/core/controllers/admin_languages.php b/core/controllers/admin_languages.php index 1a89bfca..389bbc98 100644 --- a/core/controllers/admin_languages.php +++ b/core/controllers/admin_languages.php @@ -19,35 +19,31 @@ */ class Admin_Languages_Controller extends Admin_Controller { public function index() { - $view = new Admin_View("admin.html"); - $view->content = new View("admin_languages.html"); - - $locales = locale::available(); - asort($locales, SORT_LOCALE_STRING); - - $form = new Forge("admin/languages/save", "", "post", array("id" => "gLanguageSettingsForm")); - $group = $form->group("settings") - ->label(t("Please select a language")); - $group->dropdown("locale_selection") - ->options($locales) - ->selected(module::get_var("core", "default_locale")); - $group->submit("save")->value(t("Save settings")); - - $view->content->form = $form; - - print $view; + $v = new Admin_View("admin.html"); + $v->content = new View("admin_languages.html"); + $v->content->form = $this->_languages_form(); + print $v; } public function save() { - $locales = locale::available(); - $selected_locale = $this->input->post("locale_selection"); - if (!isset($locales[$selected_locale])) { - message::error(t("Invalid selection")); - } else { - module::set_var("core", "default_locale", $selected_locale); + $form = $this->_languages_form(); + if ($form->validate()) { + module::set_var("core", "default_locale", $form->choose_language->locale->value); message::success(t("Settings saved")); } url::redirect("admin/languages"); } + + private function _languages_form() { + $locales = locale::available(); + $form = new Forge("admin/languages/save", "", "post", array("id" => "gLanguageSettingsForm")); + $group = $form->group("choose_language") + ->label(t("Please select a language")); + $group->dropdown("locale") + ->options($locales) + ->selected(module::get_var("core", "default_locale")); + $group->submit("save")->value(t("Save settings")); + return $form; + } } diff --git a/core/helpers/locale.php b/core/helpers/locale.php index bd85cf6b..78ef5158 100644 --- a/core/helpers/locale.php +++ b/core/helpers/locale.php @@ -22,192 +22,69 @@ * This is the API for handling locales. */ class locale_Core { + private static $locales; + /** * Return the list of available locales. */ static function available() { - $locales = array(); - list ($supported_languages, $default_Country) = self::_get_language_data(); - foreach ($supported_languages as $language_tag => $country_locales) { - foreach ($country_locales as $country_tag => $entry) { - $locales[$language_tag . '_' . $country_tag] = - $entry['description']; - } + if (empty(self::$locales)) { + self::_init_language_data(); } - return $locales; + return self::$locales; } - private static function _get_language_data() { - static $supported_languages = array(); - static $default_country = array(); + // TODO(andy_st): Might want to add a localizable language name as well. + private static function _init_language_data() { + $l->af_ZA = 'Afrikaans'; // Afrikaans + $l->ar_SA = 'العربية'; // Arabic + $l->bg_BG = 'Български'; // Bulgarian + $l->ca_ES = 'Catalan'; // Catalan + $l->cs_CZ = 'Česky'; // Czech + $l->da_DK = 'Dansk'; // Danish + $l->de_DE = 'Deutsch'; // German + $l->el_GR = 'Greek'; // Greek + $l->en_GB = 'English (UK)'; // English (UK) + $l->en_US = 'English (US)'; // English (US) + $l->es_AR = 'Español (AR)'; // Spanish (AR) + $l->es_ES = 'Español'; // Spanish (ES) + $l->es_MX = 'Español (MX)'; // Spanish (MX) + $l->et_EE = 'Eesti'; // Estonian + $l->eu_ES = 'Euskara'; // Basque + $l->fa_IR = 'فارسي'; // Farsi + $l->fi_FI = 'Suomi'; // Finnish + $l->fr_FR = 'Français'; // French + $l->ga_IE = 'Gaeilge'; // Irish + $l->he_IL = 'עברית'; // Hebrew + $l->hu_HU = 'Magyar'; // Hungarian + $l->is_IS = 'Icelandic'; // Icelandic + $l->it_IT = 'Italiano'; // Italian + $l->ja_JP = '日本語'; // Japanese + $l->ko_KR = '한국말'; // Korean + $l->lt_LT = 'Lietuvių'; // Lithuanian + $l->lv_LV = 'Latviešu'; // Latvian + $l->nl_NL = 'Nederlands'; // Dutch + $l->no_NO = 'Norsk bokmål'; // Norwegian + $l->pl_PL = 'Polski'; // Polish + $l->pt_BR = 'Português Brasileiro'; // Portuguese (BR) + $l->pt_PT = 'Português'; // Portuguese (PT) + $l->ro_RO = 'Română'; // Romanian + $l->ru_RU = 'Русский'; // Russian + $l->sk_SK = 'Slovenčina'; // Slovak + $l->sl_SI = 'Slovenščina'; // Slovenian + $l->sr_CS = 'Srpski'; // Serbian + $l->sv_SE = 'Svenska'; // Swedish + $l->tr_TR = 'Türkçe'; // Turkish + $l->uk_UA = 'Українська'; // Ukrainian + $l->vi_VN = 'Tiếng Việt'; // Vietnamese + $l->zh_CN = '简体中文'; // Chinese (CN) + $l->zh_TW = '繁體中文'; // Chinese (TW) + asort($l, SORT_LOCALE_STRING); + self::$locales = $l; + } - // TODO(andy_st): Might want to add a localizable language name as well. - if (empty($supported_languages)) { - /* English */ - $supported_languages['en']['US']['description'] = 'English (US)'; - $supported_languages['en']['GB']['description'] = 'English (UK)'; - $default_country['en'] = 'US'; - - /* Afrikaans */ - $supported_languages['af']['ZA']['description'] = 'Afrikaans'; - $default_country['af'] = 'ZA'; - - /* Catalan */ - $supported_languages['ca']['ES']['description'] = 'Catalan'; - $default_country['ca'] = 'ES'; - - /* Czech */ - $supported_languages['cs']['CZ']['description'] = 'Česky'; - $default_country['cs'] = 'CZ'; - - /* Danish */ - $supported_languages['da']['DK']['description'] = 'Dansk'; - $default_country['da'] = 'DK'; - - /* German */ - $supported_languages['de']['DE']['description'] = 'Deutsch'; - $default_country['de'] = 'DE'; - - /* Spanish */ - $supported_languages['es']['ES']['description'] = 'Español'; - $supported_languages['es']['MX']['description'] = 'Español (MX)'; - $supported_languages['es']['AR']['description'] = 'Español (AR)'; - $default_country['es'] = 'ES'; - - /* Estonian */ - $supported_languages['et']['EE']['description'] = 'Eesti'; - $default_country['et'] = 'EE'; - - /* Basque */ - $supported_languages['eu']['ES']['description'] = 'Euskara'; - $default_country['eu'] = 'ES'; - - /* French */ - $supported_languages['fr']['FR']['description'] = 'Français'; - $default_country['fr'] = 'FR'; - - /* Irish */ - $supported_languages['ga']['IE']['description'] = 'Gaeilge'; - $default_country['ga'] = 'IE'; - - /* Greek */ - $supported_languages['el']['GR']['description'] = 'Greek'; - $default_country['el'] = 'GR'; - - /* Icelandic */ - $supported_languages['is']['IS']['description'] = 'Icelandic'; - $default_country['is'] = 'IS'; - - /* Italian */ - $supported_languages['it']['IT']['description'] = 'Italiano'; - $default_country['it'] = 'IT'; - - /* Latvian */ - $supported_languages['lv']['LV']['description'] = 'Latviešu'; - $default_country['lv'] = 'LV'; - - /* Lithuanian */ - $supported_languages['lt']['LT']['description'] = 'Lietuvių'; - $default_country['lt'] = 'LT'; - - /* Hungarian */ - $supported_languages['hu']['HU']['description'] = 'Magyar'; - $default_country['hu'] = 'HU'; - - /* Dutch */ - $supported_languages['nl']['NL']['description'] = 'Nederlands'; - $default_country['nl'] = 'NL'; - - /* Norwegian */ - $supported_languages['no']['NO']['description'] = 'Norsk bokmål'; - $default_country['no'] = 'NO'; - - /* Polish */ - $supported_languages['pl']['PL']['description'] = 'Polski'; - $default_country['pl'] = 'PL'; - - /* Portuguese */ - $supported_languages['pt']['BR']['description'] = 'Português Brasileiro'; - $supported_languages['pt']['PT']['description'] = 'Português'; - $default_country['pt'] = 'PT'; - - /* Romanian */ - $supported_languages['ro']['RO']['description'] = 'Română'; - $default_country['ro'] = 'RO'; - - /* Slovak */ - $supported_languages['sk']['SK']['description'] = 'Slovenčina'; - $default_country['sk'] = 'SK'; - - /* Slovenian */ - $supported_languages['sl']['SI']['description'] = 'Slovenščina'; - $default_country['sl'] = 'SI'; - - /* Serbian */ - $supported_languages['sr']['CS']['description'] = 'Srpski'; - $default_country['sr'] = 'CS'; - - /* Finnish */ - $supported_languages['fi']['FI']['description'] = 'Suomi'; - $default_country['fi'] = 'FI'; - - /* Swedish */ - $supported_languages['sv']['SE']['description'] = 'Svenska'; - $default_country['sv'] = 'SE'; - - /* Ukrainian */ - $supported_languages['uk']['UA']['description'] = 'Українська'; - $default_country['uk'] = 'UA'; - - /* Vietnamese */ - $supported_languages['vi']['VN']['description'] = 'Tiếng Việt'; - $default_country['vi'] = 'VN'; - - /* Turkish */ - $supported_languages['tr']['TR']['description'] = 'Türkçe'; - $default_country['tr'] = 'TR'; - - /* Bulgarian */ - $supported_languages['bg']['BG']['description'] = - 'Български'; - $default_country['bg'] = 'BG'; - - /* Russian */ - $supported_languages['ru']['RU']['description'] = - 'Русский'; - $default_country['ru'] = 'RU'; - - /* Chinese */ - $supported_languages['zh']['CN']['description'] = '简体中文'; - $supported_languages['zh']['TW']['description'] = '繁體中文'; - $default_country['zh'] = 'CN'; - - /* Korean */ - $supported_languages['ko']['KR']['description'] = '한국말'; - $default_country['ko'] = 'KR'; - - /* Japanese */ - $supported_languages['ja']['JP']['description'] = '日本語'; - $default_country['ja'] = 'JP'; - - /* Arabic */ - $supported_languages['ar']['SA']['description'] = - 'العربية'; - $supported_languages['ar']['SA']['right-to-left'] = true; - $default_country['ar'] = 'SA'; - - /* Hebrew */ - $supported_languages['he']['IL']['description'] = 'עברית'; - $supported_languages['he']['IL']['right-to-left'] = true; - $default_country['he'] = 'IL'; - - /* Farsi */ - $supported_languages['fa']['IR']['description'] = 'فارسي'; - $supported_languages['fa']['IR']['right-to-left'] = true; - $default_country['fa'] = 'IR'; - } - - return array($supported_languages, $default_country); + static function is_rtl($locale) { + return in_array($locale, array("he_IL", "fa_IR", "ar_SA")); } } \ No newline at end of file diff --git a/modules/user/helpers/user.php b/modules/user/helpers/user.php index 743719cb..e979475c 100644 --- a/modules/user/helpers/user.php +++ b/modules/user/helpers/user.php @@ -79,12 +79,13 @@ class user_Core { } private static function _add_locale_dropdown(&$form, $user=null) { - $available_locales = locale::available(); - asort($available_locales, SORT_LOCALE_STRING); - $locales['none'] = t("Language Preference"); - $locales = array_merge($locales, $available_locales); + $locales = locale::available(); + $locales->none = t("« none »"); $selected_locale = ($user && $user->locale) ? $user->locale : "none"; - $form->dropdown("locale")->options($locales)->selected($selected_locale); + $form->dropdown("locale") + ->label(t("Language Preference")) + ->options($locales) + ->selected($selected_locale); } static function get_delete_form_admin($user) {