Drupal 6,以编程方式添加字符串翻译

时间:2012-02-21 12:09:50

标签: localization drupal-6

我需要以编程方式添加字符串翻译(例如翻译分类术语)。是否有一些功能,如add_translation($my_term_name, $language, $translation)

1 个答案:

答案 0 :(得分:4)

不幸的是,核心没有很好的功能。您可以通过直接写入数据库来检查区域设置菜单的工作方式:

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;
}

如果您的文本来自不受信任的来源(例如用户输入),请不要忘记验证。