Skip to content

Commit

Permalink
Leverage lang_dir_for_domain filter (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Aug 12, 2024
1 parent 3bbe15c commit 70d514f
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 299 deletions.
14 changes: 3 additions & 11 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,17 @@ jobs:
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
php: ['8.1', '8.0', '7.2', '7.4']
php: ['8.2', '8.1', '8.0', '7.2', '7.4']
wp: ['latest']
coverage: [false]
experimental: [false]
include:
- php: '8.2'
- php: '8.3'
wp: 'latest'
coverage: true
experimental: false

- php: '8.2'
wp: 'trunk'
experimental: true

- php: '7.4'
wp: '6.2'
experimental: false

- php: '8.3'
- php: '8.4'
wp: 'trunk'
experimental: true

Expand Down
18 changes: 0 additions & 18 deletions inc/class-preferred-languages-noop-translations.php

This file was deleted.

219 changes: 59 additions & 160 deletions inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
* @since 2.0.1
*/
function preferred_languages_boot() {
add_filter( 'gettext', 'preferred_languages_filter_gettext', 10, 3 );
add_filter( 'gettext_with_context', 'preferred_languages_filter_gettext_with_context', 10, 4 );
add_filter( 'ngettext', 'preferred_languages_filter_ngettext', 10, 5 );
add_filter( 'ngettext_with_context', 'preferred_languages_filter_ngettext_with_context', 10, 6 );
add_filter( 'lang_dir_for_domain', 'preferred_languages_filter_lang_dir_for_domain', 10, 3 );

add_action( 'init', 'preferred_languages_register_setting' );
add_action( 'init', 'preferred_languages_register_meta' );
Expand Down Expand Up @@ -600,23 +597,6 @@ function preferred_languages_override_load_textdomain( $override, $domain, $mofi
$current_locale = determine_locale();
}

$merge_translations = class_exists( 'WP_Translations' );

/**
* Filters whether translations should be merged with existing ones.
*
* @since 1.7.0
*
* @param bool $merge Whether translations should be merged. Defaults to true on WordPress 6.5 and newer.
* @param string $domain The text domain
* @param string $current_locale The current locale.
*/
$merge_translations = apply_filters( 'preferred_languages_merge_translations', $merge_translations, $domain, $current_locale );

if ( ! $merge_translations ) {
return $override;
}

$preferred_locales = preferred_languages_get_list();

if ( empty( $preferred_locales ) ) {
Expand Down Expand Up @@ -648,6 +628,17 @@ function preferred_languages_override_load_textdomain( $override, $domain, $mofi
);
}

/**
* Filters whether translations should be merged with existing ones.
*
* @since 1.7.0
*
* @param bool $merge Whether translations should be merged. Defaults to true.
* @param string $domain The text domain
* @param string $current_locale The current locale.
*/
$merge_translations = apply_filters( 'preferred_languages_merge_translations', true, $domain, $current_locale );

$first_mofile = null;

remove_filter( 'override_load_textdomain', 'preferred_languages_override_load_textdomain' );
Expand All @@ -666,6 +657,10 @@ function preferred_languages_override_load_textdomain( $override, $domain, $mofi
if ( null === $first_mofile ) {
$first_mofile = $preferred_mofile;
}

if ( ! $merge_translations ) {
break;
}
}
}

Expand Down Expand Up @@ -741,10 +736,8 @@ function preferred_languages_pre_load_script_translations( $translations, $file,

$current_locale = determine_locale();

$merge_translations = class_exists( 'WP_Translations' );

/** This filter is documented in inc/functions.php */
$merge_translations = apply_filters( 'preferred_languages_merge_translations', $merge_translations, $domain, $current_locale );
$merge_translations = apply_filters( 'preferred_languages_merge_translations', true, $domain, $current_locale );

if ( ! $merge_translations ) {
return $translations;
Expand Down Expand Up @@ -1165,161 +1158,67 @@ function preferred_languages_display_form( $args = array() ) {


/**
* Helper function used for just-in-time loading of translations.
* Filters the language directory path for a specific domain and locale.
*
* @since 2.1.2
* @access private
* Used for hooking into just-in-time translation loading.
*
* @param string $translation Translated text.
* @param string $single The text to be used if the number is singular.
* @param string $plural The text to be used if the number is plural.
* @param int $number The number to compare against to use either the singular or plural form.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @since 2.4.0
*
* @return string Translated text.
* @param string|false $path Languages directory path for the given domain and locale.
* @param string $domain Text domain.
* @param string $locale Locale.
*
* @return string|false Filtered directory path.
*/
function preferred_languages_load_just_in_time( $translation, $single, $plural = null, $number = null, $context = null, $domain = 'default' ) {
global $wp_textdomain_registry, $l10n;

static $noop_translations = null;
if ( null === $noop_translations ) {
$noop_translations = new Preferred_Languages_Noop_Translations();
}
function preferred_languages_filter_lang_dir_for_domain( $path, $domain, $locale ) {
global $wp_textdomain_registry;

if ( 'default' === $domain ) {
return $translation;
if ( $path ) {
return $path;
}

$translations = get_translations_for_domain( $domain );
$preferred_locales = preferred_languages_get_list();

if ( $translations instanceof Preferred_Languages_Noop_Translations ) {
return $translation;
if ( empty( $preferred_locales ) ) {
return $path;
}

if ( $translations instanceof NOOP_Translations ) {
$current_locale = determine_locale();

$preferred_locales = preferred_languages_get_list();

if ( empty( $preferred_locales ) ) {
return $translation;
}

// Locale has been filtered by something else.
if ( $preferred_locales[0] !== $current_locale && ! preferred_languages_is_locale_switched() ) {
return $translation;
}
// Locale has been filtered by something else.
if ( $preferred_locales[0] !== $locale && ! preferred_languages_is_locale_switched() ) {
return $path;
}

/*
* If locale has been switched to a specific locale, ignore the ones before it.
* Example:
* Preferred Languages: fr_FR, de_CH, de_DE, es_ES.
* Switched to locale: de_CH
* In that case, only check for de_CH, de_DE, es_ES.
*/
if ( preferred_languages_is_locale_switched() ) {
$offset = array_search( $current_locale, $preferred_locales, true );

if ( ! is_int( $offset ) ) {
return $translation;
}
/*
* If locale has been switched to a specific locale, ignore the ones before it.
* Example:
* Preferred Languages: fr_FR, de_CH, de_DE, es_ES.
* Switched to locale: de_CH
* In that case, only check for de_CH, de_DE, es_ES.
*/
if ( preferred_languages_is_locale_switched() ) {
$offset = array_search( $locale, $preferred_locales, true );

$preferred_locales = array_slice(
$preferred_locales,
$offset
);
if ( ! is_int( $offset ) ) {
return $path;
}

foreach ( $preferred_locales as $locale ) {
$path = $wp_textdomain_registry->get( $domain, $locale );

if ( ! $path ) {
continue;
}

$mofile = "{$path}/{$domain}-{$locale}.mo";

if ( load_textdomain( $domain, $mofile ) ) {
$translations = get_translations_for_domain( $domain );
$preferred_locales = array_slice(
$preferred_locales,
$offset
);
}

if ( null !== $plural && null !== $number ) {
return $translations->translate_plural( $single, $plural, $number, $context );
}
foreach ( $preferred_locales as $preferred_locale ) {
remove_filter( 'lang_dir_for_domain', 'preferred_languages_filter_lang_dir_for_domain' );
$new_path = $wp_textdomain_registry->get( $domain, $preferred_locale );
add_filter( 'lang_dir_for_domain', 'preferred_languages_filter_lang_dir_for_domain', 10, 3 );

return $translations->translate( $single, $context );
}
if ( $new_path ) {
return $new_path;
}

$l10n[ $domain ] = &$noop_translations;
}

return $translation;
}

/**
* Filters gettext calls to work around limitations in just-in-time loading of translations.
*
* @since 1.1.0
*
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*
* @return string Translated text.
*/
function preferred_languages_filter_gettext( $translation, $text, $domain ) {
return preferred_languages_load_just_in_time( $translation, $text, null, null, null, $domain );
}

/**
* Filters gettext calls to work around limitations in just-in-time loading of translations.
*
* @since 2.1.2
*
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*
* @return string Translated text.
*/
function preferred_languages_filter_gettext_with_context( $translation, $text, $context, $domain ) {
return preferred_languages_load_just_in_time( $translation, $text, null, null, $context, $domain );
}

/**
* Filters gettext calls to work around limitations in just-in-time loading of translations.
*
* @since 2.1.2
*
* @param string $translation Translated text.
* @param string $single The text to be used if the number is singular.
* @param string $plural The text to be used if the number is plural.
* @param int $number The number to compare against to use either the singular or plural form.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*
* @return string Translated text.
*/
function preferred_languages_filter_ngettext( $translation, $single, $plural, $number, $domain ) {
return preferred_languages_load_just_in_time( $translation, $single, $plural, $number, null, $domain );
}
/**
* Filters gettext calls to work around limitations in just-in-time loading of translations.
*
* @since 2.1.2
*
* @param string $translation Translated text.
* @param string $single The text to be used if the number is singular.
* @param string $plural The text to be used if the number is plural.
* @param int $number The number to compare against to use either the singular or plural form.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*
* @return string Translated text.
*/
function preferred_languages_filter_ngettext_with_context( $translation, $single, $plural, $number, $context, $domain ) {
return preferred_languages_load_just_in_time( $translation, $single, $plural, $number, $context, $domain );
return $path;
}

/**
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ parameters:
- inc/
- tests/phpunit
ignoreErrors:
- message: '/^Call to private\/internal function preferred_languages_load_just_in_time\(\)\.$/'
- message: '/has no return type specified\.$/'
- message: '/^Call to method PHPUnit\\Framework\\Assert::assertFalse\(\) with false will always evaluate to true\.$/'
- message: '/^Cannot access offset 1 on mixed\.$/'
Expand Down
7 changes: 3 additions & 4 deletions preferred-languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* Plugin Name: Preferred Languages
* Plugin URI: https://github.com/swissspidy/preferred-languages/
* Description: Enables you to choose languages for displaying WordPress in, in order of preference.
* Version: 2.3.0
* Version: 2.4.0
* Author: Pascal Birchler
* Author URI: https://pascalbirchler.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: preferred-languages
* Requires at least: 6.2.1
* Requires PHP: 7.2
* Requires at least: 6.6
* Requires PHP: 7.2.24
*
* Copyright (c) 2017 Pascal Birchler (email: swissspidy@chat.wordpress.org)
*
Expand All @@ -34,7 +34,6 @@
/**
* Plugin functions.
*/
require_once __DIR__ . '/inc/class-preferred-languages-noop-translations.php';
require_once __DIR__ . '/inc/functions.php';

// We need to load before plugins_loaded, see https://core.trac.wordpress.org/ticket/58546.
Expand Down
Loading

0 comments on commit 70d514f

Please sign in to comment.