From a6f3fcf784f26163f7f8ddcf37239f3ad6fca421 Mon Sep 17 00:00:00 2001 From: Ozh Date: Wed, 27 Mar 2013 22:02:16 +0000 Subject: [PATCH] Make the JS calendar translatable git-svn-id: http://yourls.googlecode.com/svn/trunk@903 12232710-3e20-11de-b438-597f59cd7555 --- includes/functions-html.php | 19 +++++- includes/functions-l10n.php | 120 +++++++++++++++++++++++++++++++++++- js/jquery.cal.js | 5 +- 3 files changed, 140 insertions(+), 4 deletions(-) diff --git a/includes/functions-html.php b/includes/functions-html.php index cf7e8f7..a81bbac 100644 --- a/includes/functions-html.php +++ b/includes/functions-html.php @@ -78,7 +78,7 @@ function yourls_html_head( $context = 'index', $title = '' ) { ?> -> +> <?php echo $title ?> @@ -109,6 +109,7 @@ function yourls_html_head( $context = 'index', $title = '' ) { + @@ -830,4 +831,20 @@ function yourls_html_language_attributes() { $output = implode( ' ', $attributes ); $output = yourls_apply_filters( 'html_language_attributes', $output ); echo $output; +} + +/** + * Output translated strings used by the Javascript calendar + * + * @since 1.6 + */ +function yourls_l10n_calendar_strings() { + echo "\n\n"; + + // Dummy returns, to initialize l10n strings used in the calendar + yourls__( 'Today' ); + yourls__( 'Close' ); } \ No newline at end of file diff --git a/includes/functions-l10n.php b/includes/functions-l10n.php index df6f282..361ed44 100644 --- a/includes/functions-l10n.php +++ b/includes/functions-l10n.php @@ -601,7 +601,7 @@ function yourls_date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = fal if ( false === $i ) { if ( ! $gmt ) - $i = current_time( 'timestamp' ); + $i = yourls_current_time( 'timestamp' ); else $i = time(); // we should not let date() interfere with our @@ -658,6 +658,33 @@ function yourls_date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = fal return $j; } +/** + * Retrieve the current time based on specified type. Stolen from WP. + * + * The 'mysql' type will return the time in the format for MySQL DATETIME field. + * The 'timestamp' type will return the current timestamp. + * + * If $gmt is set to either '1' or 'true', then both types will use GMT time. + * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option. + * + * @since 1.6 + * + * @param string $type Either 'mysql' or 'timestamp'. + * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false. + * @return int|string String if $type is 'gmt', int if $type is 'timestamp'. + */ +function yourls_current_time( $type, $gmt = 0 ) { + switch ( $type ) { + case 'mysql': + return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', time() + YOURLS_HOURS_OFFSET * 3600 ); + break; + case 'timestamp': + return ( $gmt ) ? time() : time() + YOURLS_HOURS_OFFSET * 3600; + break; + } +} + + /** * Class that loads the calendar locale. * @@ -1013,3 +1040,94 @@ function yourls_is_rtl() { return $yourls_locale_formats->is_rtl(); } +/** + * Return translated weekday abbreviation (3 letters, eg 'Fri' for 'Friday') + * + * The $weekday var can be a textual string ('Friday'), a integer (0 to 6) or an empty string + * If $weekday is an empty string, the function returns an array of all translated weekday abbrev + * + * @since 1.6 + * @param mixed $weekday A full textual weekday, eg "Friday", or an integer (0 = Sunday, 1 = Monday, .. 6 = Saturday) + * @return mixed Translated weekday abbreviation, eg "Ven" (abbrev of "Vendredi") for "Friday" or 5, or array of all weekday abbrev + */ +function yourls_l10n_weekday_abbrev( $weekday = '' ){ + global $yourls_locale_formats; + if( !isset( $yourls_locale_formats ) ) + $yourls_locale_formats = new YOURLS_Locale_Formats(); + + if( $weekday === '' ) + return $yourls_locale_formats->weekday_abbrev; + + if( is_int( $weekday ) ) { + $day = $yourls_locale_formats->weekday[ $weekday ]; + return $yourls_locale_formats->weekday_abbrev[ $day ]; + } else { + return $yourls_locale_formats->weekday_abbrev[ yourls__( $weekday ) ]; + } +} + +/** + * Return translated weekday initial (1 letter, eg 'F' for 'Friday') + * + * The $weekday var can be a textual string ('Friday'), a integer (0 to 6) or an empty string + * If $weekday is an empty string, the function returns an array of all translated weekday initials + * + * @since 1.6 + * @param mixed $weekday A full textual weekday, eg "Friday", an integer (0 = Sunday, 1 = Monday, .. 6 = Saturday) or empty string + * @return mixed Translated weekday initial, eg "V" (initial of "Vendredi") for "Friday" or 5, or array of all weekday initials + */ +function yourls_l10n_weekday_initial( $weekday = '' ){ + global $yourls_locale_formats; + if( !isset( $yourls_locale_formats ) ) + $yourls_locale_formats = new YOURLS_Locale_Formats(); + + if( $weekday === '' ) + return $yourls_locale_formats->weekday_initial; + + if( is_int( $weekday ) ) { + $weekday = $yourls_locale_formats->weekday[ $weekday ]; + return $yourls_locale_formats->weekday_initial[ $weekday ]; + } else { + return $yourls_locale_formats->weekday_initial[ yourls__( $weekday ) ]; + } +} + +/** + * Return translated month abbrevation (3 letters, eg 'Nov' for 'November') + * + * The $month var can be a textual string ('November'), a integer (1 to 12), a two digits strings ('01' to '12), or an empty string + * If $month is an empty string, the function returns an array of all translated abbrev months ('January' => 'Jan', ...) + * + * @since 1.6 + * @param mixed $month Empty string, a full textual weekday, eg "November", or an integer (1 = January, .., 12 = December) + * @return mixed Translated month abbrev (eg "Nov"), or array of all translated abbrev months + */ +function yourls_l10n_month_abbrev( $month = '' ){ + global $yourls_locale_formats; + if( !isset( $yourls_locale_formats ) ) + $yourls_locale_formats = new YOURLS_Locale_Formats(); + + if( $month === '' ) + return $yourls_locale_formats->month_abbrev; + + if( intval( $month ) > 0 ) { + $month = $yourls_locale_formats->month[ $month ]; + return $yourls_locale_formats->month_abbrev[ $month ]; + } else { + return $yourls_locale_formats->month_abbrev[ yourls__( $month ) ]; + } +} + +/** + * Return array of all translated months + * + * @since 1.6 + * @return array Array of all translated months + */ +function yourls_l10n_months(){ + global $yourls_locale_formats; + if( !isset( $yourls_locale_formats ) ) + $yourls_locale_formats = new YOURLS_Locale_Formats(); + + return $yourls_locale_formats->month; +} diff --git a/js/jquery.cal.js b/js/jquery.cal.js index b198e32..0391628 100644 --- a/js/jquery.cal.js +++ b/js/jquery.cal.js @@ -13,6 +13,7 @@ var today = new Date(); // used in defaults var months = 'January,February,March,April,May,June,July,August,September,October,November,December'.split(','); + var months = l10n_cal_month; var monthlengths = '31,28,31,30,31,30,31,31,30,31,30,31'.split(','); var dateRegEx = /^\d{1,2}\/\d{1,2}\/\d{2}|\d{4}$/; var yearRegEx = /^\d{4,4}$/; @@ -85,7 +86,7 @@ // month select field var monthselect = ''; // year select field @@ -94,7 +95,7 @@ yearselect += ''; jQuery("thead",table).append('« '+monthselect+yearselect+' »'); - jQuery("thead",table).append('SMTWTFS'); + jQuery("thead",table).append(''+l10n_cal_days[0]+''+l10n_cal_days[1]+''+l10n_cal_days[2]+''+l10n_cal_days[3]+''+l10n_cal_days[4]+''+l10n_cal_days[5]+''+l10n_cal_days[6]+''); jQuery("tfoot",table).append('today close'); for (var i = 0; i < 6; i++) jQuery("tbody",table).append(''); return table; -- 2.45.0