Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Get the correct localized date formatting by WordPress Locale.
- *
- * Retrieves the short or full date formatting for PHP and jQuery (Date Picker) by WordPress Locale.
- * (WordPress Dashboard -> Settings -> General)
- *
- * Formats are based on the official ICU formatting. This resource bypasses the need for the Intl module.
- * The JSON file 'wp-locale-to-date-format.json' is located here: https://pastebin.com/eT3ku5fR
- */
- function stonehenge_localize_date_format( $type = 'php', $length = 'short' ) {
- $json = file_get_contents( __DIR__ . '/wp-locale-to-date-format.json' );
- $locales = json_decode( $json, true );
- $locale = str_replace( '_formal', '', determine_locale() ); // Dates are not different in formal.
- return array_key_exists( $locale, $locales ) ? $locales[$locale][$type][$length] : ($type != 'jquery' ? 'Y-m-d' : 'yy-mm-dd');
- }
- /**
- * Localize the date for PHP output.
- * Usage:
- * echo stonehenge_localize_date( $value ); => 26-01-2021 (d-m-Y)
- * echo stonehenge_localize_date( $value, 'full' ); => dinsdag 26 januari 2021 (l j F Y)
- */
- function stonehenge_localize_date( $date, $length = 'short' ) {
- $date = is_numeric( $date ) ? $date : strtotime( $date ); // Prevent errors.
- $format = stonehenge_localize_date_format( 'php', $length );
- return date_i18n( $format, $input );
- }
- /**
- * Localize jQuery Date Picker by WordPress Locale.
- */
- function stonehenge_localize_datepicker( $type ) {
- global $wp_locale;
- switch( $type ) {
- case 'months_full':
- $values = array_values( $wp_locale->month );
- break;
- case 'months_short':
- $values = array_values( $wp_locale->month_abbrev );
- break;
- case 'weekdays_full':
- $values = array_values( $wp_locale->weekday );
- break;
- case 'weekdays_short':
- $values = array_values( $wp_locale->weekday_abbrev );
- break;
- }
- return array_map( 'ucfirst', $values ); // Correction, as most languages only use lowercase mid-sentence.
- }
- /**
- * Use wp_localize_script() to send the localized jQuery date picker options.
- */
- wp_localize_script( 'your-script-handle', 'your-object-name', array(
- 'dateFormat' => stonehenge_localize_date_format( 'jquery', 'short' ),
- 'monthNames' => stonehenge_localize_datepicker( 'months_full' ),
- 'monthNamesShort' => stonehenge_localize_datepicker( 'months_short' ),
- 'dayNames' => stonehenge_localize_datepicker( 'weekdays_full' ),
- 'dayNamesShort' => stonehenge_localize_datepicker( 'weekdays_short' ),
- 'firstDay' => get_option( 'start_of_week' ),
- 'prevText' => __('Previous'),
- 'nextText' => __('Next'),
- 'currentText' => __('Today')
- ) );
- /**
- * Call the localized values in your script.js
- */
- jQuery(document).ready( function($) {
- $.datepicker({
- dateFormat: object.dateFormat, // dd-mm-yy
- monthNames: object.monthNames, // Januari, Februari, Maart, April ...
- monthNamesShort: object.monthNamesShort, // Jan, Feb, Maa, Apr ...
- dayNames: object.dayNames, // Zondag, Maandag, Dinsdag, Woensdag ...
- dayNamesShort: object.dayNamesShort, // Zo, Ma, Di, Wo ...
- firstDay: object.firstDay, // 1 (Monday)
- prevText: object.prevText, // Vorige
- nextText: object.nextText, // Volgende
- currentText: object.currentText, // Vandaag
- yearRange: "-1:+4", // 2020 - 2025
- changeMonth: true,
- changeYear: true
- });
- });
Add Comment
Please, Sign In to add comment