figuren-theater / ft-core

Core components for a WordPress multisite plattform like figuren.theater
GNU General Public License v3.0
0 stars 0 forks source link

Loading .json translation files from an external vendor directory #1

Open carstingaxion opened 1 year ago

carstingaxion commented 1 year ago

After moving some plugins from the default WP_PLUGIN_DIR to an composer-powered-vendor directory some i18n related functions stopped working.

Some failing translation files are:

content/languages/v/abbreviation-button-for-the-block-editor-de_DE-905251f92e3c6581795dad44a8d7902c.json

content/languages/v/koko-analytics-de_DE-e6ea4fe3eb014300d65a6b5b667706a9.json

content/languages/v/wordpress-seo-de_DE-af13d5bac4604f14038e81c783c6c67a.json
carstingaxion commented 1 year ago
carstingaxion commented 1 year ago

Registering block with block.json not load translation · Issue #34478 · WordPress/gutenberg

carstingaxion commented 1 year ago

wordpress-develop/wp-includes/l10n.php#L1192-L1239 at 6.1 · WordPress/wordpress-develop

load_script_translations() | #Hooks | Function | WordPress Developer Resources

carstingaxion commented 1 year ago

Leftover from https://github.com/figuren-theater/ft-platform/blob/6a9a2b8df9b6413fe55a4fdf5dfa414641f8380f/content/mu-plugins/01-ft-platform__mu-loader.php

//
// temporary TEST & DEBUG stuff //
//
function load_script_textdomain_relative_path( string $relative, string $src  ) : string {

    // if ('wordpress-seo/js/dist/externals/helpers.js' === $relative ) {
    \do_action( 'qm/warning', 'load_script_textdomain_relative_path()' );

    // \do_action( 'qm/debug', [ $relative, $src ] );
        // $relative = FT_VENDOR_DIR . '/wpackagist-plugin/' . $relative;
    \do_action( 'qm/debug', [ $relative, $src ] );

    $vendor = str_replace(
        WP_CONTENT_URL . '/v/',
        '',
        str_replace(
            '/' . $relative,
            '',
            $src
        )
    );
    $textdomain = explode('/', $relative)[0];

    \do_action( 'qm/debug', $vendor );
    \do_action( 'qm/debug', $textdomain );

    add_filter(
        'load_script_translation_file',
        function( string $file, string $handle, string $domain ) use ( $vendor, $textdomain ) : string {

            // if it is not our textdomain
            if ( $domain !== $textdomain )
                return $file;

            // if does not start wrong, return
            if ( 0 !== strpos( $file, \WP_LANG_DIR . '/v' ) )
                return $file;

            $filename = str_replace(
                \WP_LANG_DIR . '/v/',
                '',
                $file
            );

            // everything ok
            // lets change a path
            //
            // THIS IS BETTER GUESSING,
            // but nothing more ... **** it
            \do_action( 'qm/error', [ $file, $handle, $domain, $vendor, $textdomain ] );
            $file = FT_VENDOR_DIR . '/' . $vendor . '/' . $textdomain . '/languages/' . $filename;
            \do_action( 'qm/info', [ $file, file_exists($file) ] );

            return $file;
        },
        1001,
        3
    );

    // }

    return $relative;
}
// add_filter( 'load_script_textdomain_relative_path', __NAMESPACE__ . '\\load_script_textdomain_relative_path', 1000, 3 );
/**
 * Filters the file path for loading script translations for the given script handle and text domain.
 *
 * Should help against misleading i18n urls for json files, like:
 * - content/languages/v/abbreviation-button-for-the-block-editor-de_DE-905251f92e3c6581795dad44a8d7902c.json
 * - content/languages/v/wordpress-seo-de_DE-af13d5bac4604f14038e81c783c6c67a.json
 * - content/languages/v/koko-analytics-de_DE-e6ea4fe3eb014300d65a6b5b667706a9.json
 *
 * @since 5.0.2
 *
 * @param string|false $file   Path to the translation file to load. False if there isn't one.
 * @param string       $handle Name of the script to register a translation domain to.
 * @param string       $domain The text domain.
 */
function load_script_translation_file( string $file, string $handle, string $domain ) : string {

    $file_exists = file_exists($file);

    if ( ! $file_exists ) {
        \do_action( 'qm/warning', 'load_script_translation_file()' );
        \do_action( 'qm/debug', [ $file, $handle, $domain ] );
        #\do_action( 'qm/debug', 'file_exists(): {file_exists}', [
        #    'file_exists' => ,
        #] );
    }

    // $file = str_replace(
    //  'content/languages/v/',
    //  'content/languages/plugins/',
    //  $file
    // );
    // $file = str_replace(
    //  'content/pluginsvendor',
    //  // 'content/v',
    //  'vendor',
    //  $file
    // );

    return $file;
}
// add_filter( 'load_script_translation_file', __NAMESPACE__ . '\\load_script_translation_file', 1000, 3 );