I18n::compile_translations( TribeEventsarray $strings, string|TribeEventsarray $domains, int $flags = 7 )
Compiles the translations for a set of strings iterating on a set of domains.
Contents
The 4th argument is a bitmask to control the compiled translations. E.g. $i18n->compile_translations( $strings, $domains, I18n::COMPILE_STRTOLOWER); will only compile translations of the strings in their strtolower versions. Combine the flags using the usual PHP syntax: I18n::COMPILE_INPUT | I18n::COMPILE_STRTOLOWER to compile only the translation of the string as input and in their lowercase version.
Parameters
- $strings
-
(<span class="TribeEventsarrayTribeEventsarray<string,array|">TribeEventsstring>) (Required) The set of strings to compile the translations for.
- $domains
-
(string|<span class="TribeEventsarray">TribeEventsarray) (Required) The domain(s) that should be used to compile the string translations.
- $flags
-
(int) (Optional) An integer resulting from the combination of compilation flags; defaults to
static::COMPILE_ALLto compile all versions of the translations.static::COMPILE_INPUTwill compile the translation for the string, as input.static::COMPILE_STRTOLOWERwill compile the translation for the string in its lowercase version.static::COMPILE_UCFIRSTwill compile the translation for the string in its title version.Default value: 7
Return
(TribeEventsarray<string|TribeEventsarray>) A map of the compiled string translations.
Source
File: src/Tribe/I18n.php
public function compile_translations( array $strings, $domains ) {
$cache_salts = [ $strings, $domains, get_locale() ];
$cache_key = __METHOD__ . md5( serialize( $cache_salts ) );
$expiration_trigger = Cache_Listener::TRIGGER_UPDATED_OPTION;
$cached = tribe_cache()->get( $cache_key, $expiration_trigger, false, DAY_IN_SECONDS );
if ( false !== $cached ) {
return $cached;
}
foreach ( (array) $domains as $domain => $file ) {
// Reload it with the correct language.
unload_textdomain( $domain );
if ( 'default' === $domain ) {
load_default_textdomain();
} else {
Common::instance()->load_text_domain( $domain, $file );
}
// Loop on the strings the build the possible translations.
foreach ( $strings as $key => $value ) {
$value = is_array( $value ) ? reset( $value ) : $value;
if ( ! is_string( $value ) ) {
continue;
}
// Make sure we have an array.
$strings[ $key ] = (array) $strings[ $key ];
// Grab the possible strings for default and any other domain.
if ( 'default' === $domain ) {
$strings[ $key ][] = __( $value );
$strings[ $key ][] = __( strtolower( $value ) );
$strings[ $key ][] = __( ucfirst( $value ) );
} else {
$strings[ $key ][] = __( $value, $domain );
$strings[ $key ][] = __( strtolower( $value ), $domain );
$strings[ $key ][] = __( ucfirst( $value ), $domain );
}
}
// Reload it with the correct language.
unload_textdomain( $domain );
if ( 'default' === $domain ) {
load_default_textdomain();
} else {
Common::instance()->load_text_domain( $domain, $file );
}
}
tribe_cache()->set( $cache_key, $strings, DAY_IN_SECONDS, $expiration_trigger );
return $strings;
}
Changelog
| Version | Description |
|---|---|
| 5.1.5 | Add support for the $flags argument. |
| 5.1.1 | Introduced. |