Translates a string when some systems are not available.
Used during the install process, when database, theme, and localization system is possibly not yet available.
Use t() if your code will never run during the Drupal installation phase. Use st() if your code will only run during installation and never any other time. Use get_t() if your code could run in either circumstance.
t()
get_t()
function st($string, array $args = array(), array $options = array()) {
global $install_state;
static $install_translation;
// This may be invoked before the container has been initialized.
$container = Drupal::getContainer();
if ($container && $container
->has('translation')) {
// Since the container is properly initialized, we can use standard translation.
return t($string, $args, $options);
}
elseif (drupal_installation_attempted() && isset($install_state['parameters']['langcode'])) {
// The translation service is not there yet, use a temporary one.
if (!isset($install_translation)) {
$install_translation = new TranslationManager();
foreach (array(
new CustomStrings(),
install_file_translation_service(),
) as $translator) {
$install_translation
->addTranslator($translator);
}
$install_translation
->setDefaultLangcode($install_state['parameters']['langcode']);
}
return $install_translation
->translate($string, $args, $options);
}
// Just return an untraslated string.
return format_string($string, $args);
}