【问题标题】:Drupal 6, programmatically add string translationDrupal 6,以编程方式添加字符串翻译
【发布时间】:2012-02-21 12:09:49
【问题描述】:

我需要以编程方式添加字符串翻译(例如翻译分类术语)。有add_translation($my_term_name, $language, $translation)这样的功能吗?

【问题讨论】:

    标签: localization drupal-6


    【解决方案1】:

    不幸的是,核心中没有好的功能。您可以通过直接写入数据库来检查语言环境菜单的操作方式:

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多