Projet

Général

Profil

Paste
Télécharger (4,9 ko) Statistiques
| Branche: | Révision:

root / .drush / drush_language / language.drush.inc @ b884db3d

1
<?php
2

    
3
/**
4
 * Implementation of hook_drush_command().
5
 *
6
 * @See drush_parse_command() for a list of recognized keys.
7
 *
8
 * @return
9
 *   An associative array describing your command(s).
10
 */
11
function language_drush_command() {
12
  $items = array();
13
  $items['language-add'] = array(
14
    'description' => "Add and import a new language definition",
15
    'aliases' => array('langadd'),
16
  );
17
  $items['language-enable'] = array(
18
    'description' => "Enable an already defined language",
19
    'aliases' => array('langen'),
20
  );
21
  $items['language-disable'] = array(
22
    'description' => "Disable an already defined language",
23
    'aliases' => array('langdis'),
24
  );
25
  $items['language-default'] = array(
26
    'description' => "Assign an enabled language as default",
27
    'aliases' => array('langdef'),
28
  );
29
  return $items;
30
}
31

    
32
/**
33
 * Add a language
34
 */
35
function drush_language_add() {
36
  if (!function_exists('locale_add_language')) {
37
    drush_set_error(dt('Could not add language. Is the \'locale\' module enabled?'));
38
    return;
39
  }
40

    
41
  $args = func_get_args();
42
  if (count($args) == 0) {
43
    drush_set_error(dt('Please provide one or more language codes as arguments.'));
44
    return;
45
  }
46

    
47
  foreach ($args as $langcode) {
48
    if (array_key_exists($langcode, language_list())) {
49
      drush_log(dt('The language with code !code already exists.', array('!code' => $langcode)), 'warning');
50
    }
51
    else {
52
      // Predefined language selection.
53
      include_once DRUPAL_ROOT . '/includes/iso.inc';
54
      $predefined = _locale_get_predefined_list();
55
      if (!isset($predefined[$langcode])) {
56
        drush_log(dt('Invalid language code !language', array('!language' => $langcode)), 'warning');
57
      }
58
      else {
59
        // Add the language definition
60
        locale_add_language($langcode);
61
        // See if we have language files to import for the newly added
62
        // language, collect and import them.
63
        if ($batch = locale_batch_by_language($langcode, '_locale_batch_language_finished')) {
64
          batch_set($batch);
65

    
66
          $batch =& batch_get();
67
          $batch['progressive'] = FALSE;
68

    
69
          // Process the batch.
70
          drush_backend_batch_process();
71
        }
72
        drush_log(dt('Added language: !language', array('!language' => $langcode)), 'ok');
73
      }
74
    }
75
  }
76
}
77

    
78
/**
79
 * Enable a language
80
 */
81
function drush_language_enable() {
82
  $args = func_get_args();
83
  if (count($args) == 0) {
84
    drush_set_error(dt('Please provide one or more language codes as arguments.'));
85
    return;
86
  }
87

    
88
  foreach ($args as $langcode) {
89
    $languages = language_list();
90
    if (array_key_exists($langcode, $languages)) {
91
      if (!$languages[$langcode]->enabled) {
92
        // disable the default english
93
        db_update('languages')
94
          ->condition('language', $langcode)
95
          ->fields(array(
96
            'enabled' => 1,
97
          ))
98
          ->execute();
99

    
100
        // Changing the language settings impacts the interface.
101
        cache_clear_all('*', 'cache_page', TRUE);
102
        drush_log(dt("Enabled language : !language ", array('!language' => $langcode)), 'ok');
103
      }
104
      else {
105
        drush_log(dt("Language already enabled: !language ", array('!language' => $langcode)), 'warning');
106
      }
107
    }
108
    else {
109
      drush_log(dt("Specified language does not exist !language", array('!language' => $langcode)), 'warning');
110
    }
111
  }
112
}
113

    
114
/**
115
 * Disables a language
116
 */
117
function drush_language_disable() {
118
  $args = func_get_args();
119
  if (count($args) == 0) {
120
    drush_set_error(dt('Please provide one or more language codes as arguments.'));
121
    return;
122
  }
123

    
124
  foreach ($args as $langcode) {
125
    $languages = language_list();
126
    if (array_key_exists($langcode, $languages)) {
127
      if ($languages[$langcode]->enabled) {
128
        // disable the default english
129
        db_update('languages')
130
          ->condition('language', $langcode)
131
          ->fields(array(
132
            'enabled' => 0,
133
          ))
134
          ->execute();
135

    
136
        // Changing the language settings impacts the interface.
137
        cache_clear_all('*', 'cache_page', TRUE);
138
        drush_log(dt("Disabled language : !language ", array('!language' => $langcode)), 'ok');
139
      }
140
      else {
141
        drush_print(dt("Language already disabled: !language ", array('!language' => $langcode)), 'warning');
142
      }
143
    }
144
  }
145
}
146

    
147
/**
148
 * Assigns the default language
149
 */
150
function drush_language_default() {
151
  $args = func_get_args();
152
  if (count($args) == 0) {
153
    drush_set_error(dt('Please provide one or more language codes as arguments.'));
154
    return;
155
  }
156

    
157
  foreach ($args as $langcode) {
158
    // get all the languages
159
    $languages = language_list();
160
    if (array_key_exists($langcode, $languages)) {
161
      variable_set('language_default', (object) $languages[$langcode]);
162
      drush_log(dt("!language assigned as default", array('!language' => $langcode)), 'ok');
163
    }
164
    else {
165
      drush_log(dt("Specified language does not exist !language", array('!language' => $langcode)), 'warning');
166
    }
167
  }
168
}