4
我需要以編程方式添加字符串翻譯(例如翻譯分類術語)。有沒有像add_translation($my_term_name, $language, $translation)
這樣的功能?Drupal 6,以編程方式添加字符串翻譯
我需要以編程方式添加字符串翻譯(例如翻譯分類術語)。有沒有像add_translation($my_term_name, $language, $translation)
這樣的功能?Drupal 6,以編程方式添加字符串翻譯
不幸的是,核心沒有很好的功能。您可以檢查區域設置菜單是如何做的直接寫入數據庫:
function locale_translate_edit_form_submit($form, &$form_state) {
$lid = $form_state['values']['lid'];
foreach ($form_state['values']['translations'] as $key => $value) {
$translation = db_result(db_query("SELECT translation FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key));
if (!empty($value)) {
// Only update or insert if we have a value to use.
if (!empty($translation)) {
db_query("UPDATE {locales_target} SET translation = '%s' WHERE lid = %d AND language = '%s'", $value, $lid, $key);
}
else {
db_query("INSERT INTO {locales_target} (lid, translation, language) VALUES (%d, '%s', '%s')", $lid, $value, $key);
}
}
elseif (!empty($translation)) {
// Empty translation entered: remove existing entry from database.
db_query("DELETE FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key);
}
// Force JavaScript translation file recreation for this language.
_locale_invalidate_js($key);
}
drupal_set_message(t('The string has been saved.'));
// Clear locale cache.
_locale_invalidate_js();
cache_clear_all('locale:', 'cache', TRUE);
$form_state['redirect'] = 'admin/build/translate/search';
return;
}
如果你的文本來自不受信任的來源(如用戶輸入)不要忘記驗證。