You are here

function lingotek_set_target_language in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.2 lingotek.util.inc \lingotek_set_target_language()
  2. 7.3 lingotek.util.inc \lingotek_set_target_language()
  3. 7.4 lingotek.util.inc \lingotek_set_target_language()
  4. 7.5 lingotek.util.inc \lingotek_set_target_language()
  5. 7.6 lingotek.util.inc \lingotek_set_target_language()

Sets the extended target language locale in the languages table and whether or not it is enabled

Parameters

$drupal_language_code:

$lingotek_enable whether or not to enable the language on TMS (default: 1):

$lingotek_locale the lingotek locale that the drupal code should be associated with (it will try to pick the right one when not passed in):

$api_add boolean whether or not to add the language to all documents in the project (default: TRUE):

1 call to lingotek_set_target_language()
lingotek_add_target_language in ./lingotek.util.inc
Adds the target language as being enabled.

File

./lingotek.util.inc, line 1076
Utility functions.

Code

function lingotek_set_target_language($drupal_language_code, $lingotek_enable = 1, $lingotek_locale = NULL, $api_add = TRUE) {
  $result = FALSE;
  $one_success = FALSE;

  //tracks if any result has been true
  $lingotek_locale = is_null($lingotek_locale) ? Lingotek::convertDrupal2Lingotek($drupal_language_code, FALSE) : $lingotek_locale;
  if (is_string($drupal_language_code) && strlen($drupal_language_code) && $lingotek_enable && $lingotek_locale) {
    if ($api_add) {

      // Add the language globally to all documents in the project
      $api = LingotekApi::instance();
      $projects = LingotekSync::getSyncProjects();
      foreach ($projects as $project_id) {
        $result = $api
          ->addTranslationTarget(NULL, $project_id, $lingotek_locale);
        if ($result) {
          LingotekSync::insertTargetEntriesForAllEntities($lingotek_locale);
          LingotekSync::insertTargetEntriesForAllSets($lingotek_locale);
        }
        $one_success = $one_success || $result;
      }
      if (!$one_success) {
        drupal_set_message(t('@lingotek_locale could not be added as a language for Lingotek to translate.', array(
          '@lingotek_locale' => $lingotek_locale,
        )), 'error', FALSE);
        LingotekLog::error("Target language could not be added: @drupal_language_code (@lingotek_locale)", array(
          '@drupal_language_code' => $drupal_language_code,
          '@lingotek_locale' => $lingotek_locale,
        ));
        return FALSE;
      }
    }
    else {

      // Don't add the language globally to all documents in the project.
      // Instead, disable this language in all profiles except config.
      $profiles = lingotek_get_profiles();
      foreach ($profiles as $profile) {
        if ($profile
          ->getId() !== LingotekSync::PROFILE_CONFIG) {
          $profile
            ->disableTargetLocale($lingotek_locale);
          $profile
            ->save();
        }
      }

      // If there are any documents related to config translation,
      // automatically add the target language to all of them
      $config_profile = LingotekProfile::loadById(LingotekSync::PROFILE_CONFIG);
      $doc_ids = $config_profile
        ->getDocumentIds();
      if ($doc_ids) {
        $api = LingotekApi::instance();
        foreach ($doc_ids as $doc_id) {
          $api
            ->addTranslationTarget($doc_id, NULL, $lingotek_locale);
        }
      }
    }
    db_update('languages')
      ->fields(array(
      'enabled' => 1,
      'lingotek_enabled' => $lingotek_enable ? 1 : 0,
      'lingotek_locale' => $lingotek_locale,
    ))
      ->condition('language', $drupal_language_code)
      ->execute();
    drupal_static_reset('language_list');
    LingotekLog::info("Target language added: @drupal_language_code (@lingotek_locale)", array(
      '@drupal_language_code' => $drupal_language_code,
      '@lingotek_locale' => $lingotek_locale,
    ));
  }
  return TRUE;
}