In #357529: Implement translation of customized 'translatable' views properties we came across an issue to do with PHP in translations.
Some strings that need to be translated have a filter format, which can include the PHP filter.
Passing these to translation can both (a) expose sensitive data and (b) enable users without PHP access (translators) to alter or inject new PHP.
What to do? The attached draft module uses the following approach:
(a) replace PHP with placeholders before sending for translation,
(b) fetch translated versions and strip out PHP,
(c) substitute back in the original PHP for the placeholders.
This might be a good addition to i18n because we could use it in other places, e.g., in custom blocks that are being translated with tt() and might contain PHP.
From the code documentation:
This module implements two hooks. As with hook_nodeapi(), implementations of these hooks need to both pass by reference and return values. For this reason they can't be invoked with module_invoke_all(). Instead, module authors wanting to use these hooks should include a method like the following in their modules:
function example_invoke_translation_process(&$value, $arg, $op) {
$return = array();
$hook = 'translation_' . $op . '_process';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$result = $function($value, $arg);
if (isset($result)) {
$return[$module] = $result;
}
}
return $return;
}
Then, to ensure that any strings with associated filters are safely handled, follow these two steps:
1. Call 'pre' processing before sending for translation.
$translation_data = example_invoke_translation_process($string, $format, 'pre');
In this call, $string is the string to be translated and $format is the ID of the filter format to be applied.
2. Call 'post' processing after sending for translation.
example_invoke_translation_process($string, $translation_data, 'post');
In this call, $string is the string to be translated and $translation_data is the data returned from the previous call.
| Comment | File | Size | Author |
|---|---|---|---|
| php_translation.tar_.gz | 1.97 KB | nedjo |
Comments
Comment #1
jose reyero commented@nedjo,
The idea looks great and we are needing something like this now we are moving on with views translations, #360024: Write views localization plugin
Questions:
- Can we make this an api module in i18n* namespace? I'm thinking of something like 'i18ntext' as this could serve a more general goal like translating text handling input filters and permissions
- Do you think this can be extended to handle generic HTML tags? Like: strip out html tags > translate the resulting plain text strings > put back HTML tags. Also think about javascript code.
So if you want to move on with this, you can create a new module and commit it into i18n. It's ok if it's just an API module not used anywhere else yet. I'm thinking we should aim at consolidating i18n as the basic multilingual API other modules can build nice features upon.
Comment #2
jose reyero commentedNo more new features into i18n 6.x