diff --git a/private/src/scripts/modules/counter.js b/private/src/scripts/modules/counter.js index ef2105e5..96092c82 100644 --- a/private/src/scripts/modules/counter.js +++ b/private/src/scripts/modules/counter.js @@ -19,7 +19,16 @@ const toFormattedString = (value) => { } const { currentLocale = 'en-GB' } = window.amnestyCoreI18n; - const formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-')); + + const { forceThousandSeparator } = window.amnestyForceThousandSeparator; + + let options = ''; + + if (forceThousandSeparator) { + options = { useGrouping: true }; + } + + const formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-'), options); return formatted; }; diff --git a/private/src/styles/admin/_localisation-options.scss b/private/src/styles/admin/_localisation-options.scss index e6e2bda8..66ee51d5 100644 --- a/private/src/styles/admin/_localisation-options.scss +++ b/private/src/styles/admin/_localisation-options.scss @@ -24,3 +24,7 @@ .cmb2-wrap .cmb2-id-ol-locale-option li { margin-left: 25px; } + +.cmb2-id-force-thousands-separator .cmb-td label { + margin-left: 25px; +} diff --git a/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php b/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php index 665cfa10..11cf7909 100644 --- a/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php +++ b/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php @@ -23,7 +23,7 @@ function amnesty_register_localisation_options(): void { 'tab_title' => __( 'Localisation', 'amnesty' ), 'parent_slug' => 'amnesty_theme_options_page', 'display_cb' => 'amnesty_options_display_with_tabs', - ] + ] ); $localisation->add_field( @@ -117,7 +117,19 @@ function amnesty_register_localisation_options(): void { /* translators: [admin] */ 'upper-roman' => __( 'Uppercase Roman numerals.', 'amnesty' ), ], - ] + ] + ); + + // Add a checkbox for forcing thousands separator + $localisation->add_field( + [ + /* translators: [admin] */ + 'name' => __( 'Force Thousands Separator', 'amnesty' ), + 'id' => 'force_thousands_separator', + 'type' => 'checkbox', + 'default' => 0, + 'desc' => __( 'Force the use of thousand separators for the Stat Counter block, for example for Spanish speaking countries "3.345"', 'amnesty' ), + ] ); do_action( 'amnesty_register_localisation_options', $localisation ); diff --git a/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php b/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php index 9f3d8ae7..2aea2315 100644 --- a/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php +++ b/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php @@ -13,6 +13,8 @@ * @return string */ function render_stat_counter_block( array $attributes ): string { + $options = get_option( 'amnesty_localisation_options_page' ); + $attributes = wp_parse_args( $attributes, [ @@ -26,6 +28,10 @@ function render_stat_counter_block( array $attributes ): string { $duration = $attributes['duration']; $value = $attributes['value']; + if ( 'on' === $options['force_thousands_separator'] ) { + $value = number_format_i18n( $value ); + } + $wrapper_attributes = get_block_wrapper_attributes( [ 'class' => $alignment, diff --git a/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php b/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php index 1c66333b..c8a7a03b 100644 --- a/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php +++ b/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php @@ -347,3 +347,26 @@ function amnesty_disable_cart_fragments() { } add_action( 'wp_enqueue_scripts', 'amnesty_disable_cart_fragments', 200 ); + +if ( ! function_exists( 'amnesty_force_thousands_separator_localisation' ) ) { + /** + * Localise the thousands separator option + * + * @package Amnesty\ThemeSetup + * + * @return void + */ + function amnesty_force_thousands_separator_localisation() { + $force_thousands_separator = get_option( 'amnesty_localisation_options_page' )['force_thousands_separator'] ?? 'off'; + + $data = [ + 'forceThousandSeparator' => amnesty_validate_boolish( $force_thousands_separator ), + ]; + + wp_localize_script( 'amnesty-theme', 'amnestyForceThousandSeparator', $data ); + wp_localize_script( 'amnesty-core-blocks-js', 'amnestyForceThousandSeparator', $data ); + } +} + +add_action( 'enqueue_block_editor_assets', 'amnesty_force_thousands_separator_localisation' ); +add_action( 'wp_loaded', 'amnesty_force_thousands_separator_localisation' );