• Hi,
    I’ve a problem with the timing of string translations in a custom theme.
    I’ve defined the domain in this way:

    add_action('after_setup_theme', 'tuo_tema_load_textdomain', 0);
    function tuo_tema_load_textdomain() {
    load_theme_textdomain('my-textdomain', get_template_directory() . '/languages');
    }

    However now I have this warning:

    Notice: The _load_textdomain_just_in_time function was called incorrectly. The translation loading for the text domain my-textdomain was triggered too early…

    because in some modules I’ve defined constants like this:

    define(
    'DLI_SPINOFF_STATUS',
    array(
    'In attività' => __( 'Cerca area tematica', 'my-textdomain' ),
    'Cessata' => __( 'Cessata', 'my-textdomain' ),
    )
    );

    I need to translate these values. What’s the best way to have constants translated without triggering the warning?

    Thank you very much.

    Claudio

Viewing 3 replies - 1 through 3 (of 3 total)
  • Take a look at this article: https://make.wordpress.org/core/2025/03/12/i18n-improvements-6-8/

    I would therefore say that you no longer need to use load_theme_textdomain() if you set the information in the theme header as described.

    Then I would recommend that you do not store any translations in constants. Find another way that doesn’t lead to this problem.

    You’re getting the _load_textdomain_just_in_time warning because you’re calling __() too early—before your theme’s textdomain is loaded with load_theme_textdomain().

    This happens when constants like DLI_SPINOFF_STATUS are defined at the top level of your code, which runs before WordPress has loaded translations.

    Solutions

    1. Use a function instead of a constant:

    function get_dli_spinoff_status() {
    return array(
    'In attività' => __( 'Cerca area tematica', 'my-textdomain' ),
    'Cessata' => __( 'Cessata', 'my-textdomain' ),
    );
    }

    Call this function where needed. It ensures translations are only used after they’re loaded.

    2. Delay constant definition:

    add_action('after_setup_theme', 'define_dli_spinoff_constants');
    function define_dli_spinoff_constants() {
    define('DLI_SPINOFF_STATUS', array(
    'In attività' => __( 'Cerca area tematica', 'my-textdomain' ),
    'Cessata' => __( 'Cessata', 'my-textdomain' ),
    ));
    }

    But constants are not ideal for translated text. Functions are safer and more flexible.

    Thread Starter ioclaudio

    (@ioclaudio)

    Thanks to both of you for your suggestions.

    Claudio

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.