Make WordPress Core

Ticket #28197: fallback-languages.diff

File fallback-languages.diff, 1.6 KB (added by downstairsdev, 9 years ago)

WordPress Patch

  • wp-includes/l10n.php

     
    452452         * @param string $mofile Path to the MO file.
    453453         * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    454454         */
     455
    455456        $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
    456457
    457         if ( !is_readable( $mofile ) ) return false;
     458        if ( !is_readable( $mofile ) ) {
     459                $mofile = get_fallback_translations( $mofile, $domain );
    458460
     461                if ( false === $mofile ) {
     462                        return false;
     463                }
     464        }
     465
    459466        $mo = new MO();
    460467        if ( !$mo->import_from_file( $mofile ) ) return false;
    461468
     
    468475}
    469476
    470477/**
     478 * Looks for fallback translation files to use if locale is not available.
     479 *
     480 * @since 4.0.0
     481 *
     482 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     483 * @param string $mofile Path to the .mo file.
     484 * @return false or string $mofile Path
     485 */
     486function get_fallback_translations( $mofile, $domain ) {
     487
     488        $languages = get_available_languages( dirname( $mofile ) );
     489
     490        if ( empty( $languages ) ) {
     491                return false;
     492        }
     493
     494        $locale_base = substr( get_locale(), 0, 2);
     495
     496        foreach ( $languages as $language ) {
     497                $language_base = substr( $language, -5, 2);
     498                if ( $locale_base == $language_base ) {
     499                        $alt_mofile = dirname( $mofile ) . '/' . $language . '.mo';
     500                        $mofile = apply_filters( 'load_textdomain_mofile', $alt_mofile, $domain );
     501                        if ( is_readable( $mofile ) ) {
     502                                return $mofile;
     503                        }
     504                }
     505        }
     506
     507        return false;
     508}
     509
     510/**
    471511 * Unload translations for a text domain.
    472512 *
    473513 * @since 3.0.0