Skip to:
Content

BuddyPress.org

Changeset 13936


Ignore:
Timestamp:
06/24/2024 02:05:55 AM (2 years ago)
Author:
imath
Message:

i18n: bring back custom locations for translation files

Prior to WordPress 4.6, it was possible to completely override the translation file with a custom one from one of the 'buddypress_locale_locations' filterable list items.

With WordPress 6.5 latest improvements about l10n, we are now able to bring these back. Making it easier for users to customize every BuddyPress strings should help them build match their community needs (e.g.: using sport/school/working/any other wording).

3 functions has been introduced:

  • bp_get_custom_translation_file() is looking for a file name into 'buddypress_locale_locations' filterable list items.
  • bp_load_custom_translation_file() is the 'load_translation_file' filter callback for regular translation files.
  • bp_load_custom_script_translation_file() is the 'load_script_translation_file' filter callback for JavaScript translation files.

The bp_core_load_buddypress_textdomain() function has been deprecated as it was pretty useless since WordPress 4.6.

Props espellcaste

Fixes #9187
Closes https://github.com/buddypress/buddypress/pull/316

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r13909 r13936  
    24852485
    24862486/**
    2487  * Load the buddypress translation file for current language.
    2488  *
    2489  * @since 1.0.2
    2490  *
    2491  * @see load_textdomain() for a description of return values.
    2492  *
    2493  * @return bool
    2494  */
    2495 function bp_core_load_buddypress_textdomain() {
    2496         $domain = 'buddypress';
    2497 
    2498         /**
    2499          * Filters the locale to be loaded for the language files.
    2500          *
    2501          * @since 1.0.2
    2502          *
    2503          * @param string $value Current locale for the install.
    2504          */
    2505         $mofile_custom = sprintf( '%s-%s.mo', $domain, apply_filters( 'buddypress_locale', get_locale() ) );
    2506 
    2507         /**
    2508          * Filters the locations to load language files from.
    2509          *
    2510          * @since 2.2.0
    2511          *
    2512          * @param array $value Array of directories to check for language files in.
    2513          */
    2514         $locations = apply_filters( 'buddypress_locale_locations', array(
    2515                 trailingslashit( WP_LANG_DIR . '/' . $domain  ),
    2516                 trailingslashit( WP_LANG_DIR ),
    2517         ) );
    2518 
    2519         // Try custom locations in WP_LANG_DIR.
    2520         foreach ( $locations as $location ) {
    2521                 if ( load_textdomain( 'buddypress', $location . $mofile_custom ) ) {
    2522                         return true;
    2523                 }
    2524         }
    2525 
    2526         // Default to WP and glotpress.
    2527         return load_plugin_textdomain( $domain );
    2528 }
    2529 add_action( 'bp_core_loaded', 'bp_core_load_buddypress_textdomain' );
     2487 * Looks for the requested file name into a list of custom language locations.
     2488 *
     2489 * @since 14.0.0
     2490 *
     2491 * @param string $file_name The file name.
     2492 * @return string A file path or an empty string if no files were found into custom language locations.
     2493 */
     2494function bp_get_custom_translation_file( $file_name = '' ) {
     2495        $file_path = '';
     2496
     2497        if ( $file_name ) {
     2498                /**
     2499                 * Filters the locations to load language files from.
     2500                 *
     2501                 * Custom translation files can be put in:
     2502                 * 1. `/wp-content/languages/plugins/buddypress`
     2503                 * 2. `/wp-content/languages/buddypress`
     2504                 * 3. `/wp-content/languages`
     2505                 *
     2506                 * @since 2.2.0
     2507                 * @since 14.0.0 Adds a new location.
     2508                 *
     2509                 * @param array $value Array of directories to check for language files in.
     2510                 */
     2511                $locations = apply_filters( 'buddypress_locale_locations',
     2512                        array(
     2513                                trailingslashit( WP_LANG_DIR . '/plugins/buddypress'  ),
     2514                                trailingslashit( WP_LANG_DIR . '/buddypress'  ),
     2515                                trailingslashit( WP_LANG_DIR ),
     2516                        )
     2517                );
     2518
     2519                // Try custom locations in WP_LANG_DIR.
     2520                foreach ( $locations as $location ) {
     2521                        $custom_file = $location . $file_name;
     2522
     2523                        // Use the first found.
     2524                        if ( file_exists( $custom_file ) ) {
     2525                                $file_path = $custom_file;
     2526                                break;
     2527                        }
     2528                }
     2529        }
     2530
     2531        return $file_path;
     2532}
     2533
     2534/**
     2535 * Override translation file for current language.
     2536 *
     2537 * @since 14.0.0
     2538 *
     2539 * @param  string $file   Absolut path to the translation file to use.
     2540 * @param  string $domain The text domain to check against `buddypress`.
     2541 * @param  string $locale The current locale for the WordPress site.
     2542 * @return string Absolut path to the translation file to use.
     2543 */
     2544function bp_load_custom_translation_file( $file, $domain, $locale = '' ) {
     2545        $bp_domain = 'buddypress';
     2546
     2547        if ( $domain !== $bp_domain ) {
     2548                return $file;
     2549        }
     2550
     2551        if ( ! $locale ) {
     2552                $locale = determine_locale();
     2553        }
     2554
     2555        $mofile_custom = bp_get_custom_translation_file(
     2556                /**
     2557                 * Filters the locale to be loaded for the language files.
     2558                 *
     2559                 * @since 1.0.2
     2560                 *
     2561                 * @param string $locale Current locale.
     2562                 */
     2563                sprintf( '%s-%s.mo', $domain, apply_filters( 'buddypress_locale', $locale ) )
     2564        );
     2565
     2566        if ( $mofile_custom ) {
     2567                $file = $mofile_custom;
     2568        }
     2569
     2570        // Returns the translation file to use.
     2571        return $file;
     2572}
     2573add_filter( 'load_translation_file', 'bp_load_custom_translation_file', 10, 3 );
     2574
     2575/**
     2576 * Override script translation file for current language.
     2577 *
     2578 * @since 14.0.0
     2579 *
     2580 * @param string|false $file   Path to the translation file to load. False if there isn't one.
     2581 * @param string       $handle Name of the script to register a translation domain to.
     2582 * @param string       $domain The text domain.
     2583 * @return string Path to the translation file to load.
     2584 */
     2585function bp_load_custom_script_translation_file( $file, $handle, $domain ) {
     2586        $bp_domain = 'buddypress';
     2587
     2588        if ( $domain !== $bp_domain ) {
     2589                return $file;
     2590        }
     2591
     2592        $file_name   = wp_basename( $file );
     2593        $custom_file = bp_get_custom_translation_file( $file_name );
     2594
     2595        if ( $custom_file ) {
     2596                $file = $custom_file;
     2597        }
     2598
     2599        // Returns the translation file to use.
     2600        return $file;
     2601}
     2602add_filter( 'load_script_translation_file', 'bp_load_custom_script_translation_file', 10, 3 );
    25302603
    25312604/**
  • trunk/src/bp-core/deprecated/14.0.php

    r13877 r13936  
    8585        );
    8686}
     87
     88/**
     89 * Load the buddypress translation file for current language.
     90 *
     91 * @since 1.0.2
     92 * @deprecated 14.0.0
     93 *
     94 * @return bool
     95 */
     96function bp_core_load_buddypress_textdomain() {
     97        _deprecated_function( __FUNCTION__, '14.0.0' );
     98
     99        return false;
     100}
Note: See TracChangeset for help on using the changeset viewer.