You are here

function locale_add_language in Drupal 7

Same name and namespace in other branches
  1. 6 includes/locale.inc \locale_add_language()

API function to add a language.

Parameters

$langcode: Language code.

$name: English name of the language

$native: Native name of the language

$direction: LANGUAGE_LTR or LANGUAGE_RTL

$domain: Optional custom domain name with protocol, without trailing slash (eg. http://de.example.com).

$prefix: Optional path prefix for the language. Defaults to the language code if omitted.

$enabled: Optionally TRUE to enable the language when created or FALSE to disable.

$default: Optionally set this language to be the default.

Related topics

11 calls to locale_add_language()
DateTimeFunctionalTest::testDateFormatStorage in modules/system/system.test
Test if the date formats are stored properly.
DrupalHTTPRequestTestCase::testDrupalHTTPRequestHeaders in modules/simpletest/tests/common.test
Tests Content-language headers generated by Drupal.
FieldTranslationsTestCase::setUp in modules/field/tests/field.test
Set the default field storage backend for fields created during tests.
install_import_locales in includes/install.core.inc
Imports languages via a batch process during installation.
LocaleMultilingualFieldsFunctionalTest::setUp in modules/locale/locale.test
Sets up a Drupal site for running functional and integration tests.

... See full list

File

includes/locale.inc, line 595
Administration functions for locale.module.

Code

function locale_add_language($langcode, $name = NULL, $native = NULL, $direction = LANGUAGE_LTR, $domain = '', $prefix = '', $enabled = TRUE, $default = FALSE) {

  // Default prefix on language code.
  if (empty($prefix)) {
    $prefix = $langcode;
  }

  // If name was not set, we add a predefined language.
  if (!isset($name)) {
    include_once DRUPAL_ROOT . '/includes/iso.inc';
    $predefined = _locale_get_predefined_list();
    $name = $predefined[$langcode][0];
    $native = isset($predefined[$langcode][1]) ? $predefined[$langcode][1] : $predefined[$langcode][0];
    $direction = isset($predefined[$langcode][2]) ? $predefined[$langcode][2] : LANGUAGE_LTR;
  }
  db_insert('languages')
    ->fields(array(
    'language' => $langcode,
    'name' => $name,
    'native' => $native,
    'direction' => $direction,
    'domain' => $domain,
    'prefix' => $prefix,
    'enabled' => $enabled,
  ))
    ->execute();

  // Only set it as default if enabled.
  if ($enabled && $default) {
    variable_set('language_default', (object) array(
      'language' => $langcode,
      'name' => $name,
      'native' => $native,
      'direction' => $direction,
      'enabled' => (int) $enabled,
      'plurals' => 0,
      'formula' => '',
      'domain' => '',
      'prefix' => $prefix,
      'weight' => 0,
      'javascript' => '',
    ));
  }
  if ($enabled) {

    // Increment enabled language count if we are adding an enabled language.
    variable_set('language_count', variable_get('language_count', 1) + 1);
  }

  // Kill the static cache in language_list().
  drupal_static_reset('language_list');

  // Force JavaScript translation file creation for the newly added language.
  _locale_invalidate_js($langcode);
  watchdog('locale', 'The %language language (%code) has been created.', array(
    '%language' => $name,
    '%code' => $langcode,
  ));
  module_invoke_all('multilingual_settings_changed');
}