Commit 9c668d1f authored by catch's avatar catch

Issue #1215716 by Gábor Hojtsy, svendecabooter: Introduce locale_language_save().

parent 68c0c8c6
......@@ -2579,6 +2579,7 @@ function language_list($field = 'language') {
$languages = &drupal_static(__FUNCTION__);
// Init language list
if (!isset($languages)) {
$default = language_default();
if (drupal_multilingual() || module_exists('locale')) {
$languages['language'] = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC')->fetchAllAssoc('language');
// Users cannot uninstall the native English language. However, we allow
......@@ -2590,9 +2591,14 @@ function language_list($field = 'language') {
}
else {
// No locale module, so use the default language only.
$default = language_default();
$languages['language'][$default->language] = $default;
}
// Initialize default property so callers have an easy reference and
// can save the same object without data loss.
foreach ($languages['language'] as $langcode => $language) {
$languages['language'][$langcode]->default = ($langcode == $default->language);
}
}
// Return the array indexed by the right field
......
......@@ -1382,11 +1382,21 @@ function install_import_locales(&$install_state) {
if (!isset($predefined[$install_locale])) {
// Drupal does not know about this language, so we prefill its values with
// our best guess. The user will be able to edit afterwards.
locale_add_language($install_locale, $install_locale, $install_locale, LANGUAGE_LTR, '', '', TRUE, TRUE);
$language = (object) array(
'language' => $install_locale,
'name' => $install_locale,
'native' => $install_locale,
'default' => TRUE,
);
locale_language_save($language);
}
else {
// A known predefined language, details will be filled in properly.
locale_add_language($install_locale, NULL, NULL, NULL, '', '', TRUE, TRUE);
$language = (object) array(
'language' => $install_locale,
'default' => TRUE,
);
locale_language_save($language);
}
// Collect files to import for this language.
......
......@@ -429,85 +429,66 @@ function locale_string_is_safe($string) {
}
/**
* @defgroup locale-api-add Language addition API
* @{
* Add a language.
* API function to add or update a language.
*
* The language addition API is used to create languages and store them.
* @param $language
* Language object with properties corresponding to 'languages' table columns.
*/
/**
* API function to add a language.
*
* @param $langcode
* Language code.
* @param $name
* English name of the language
* @param $native
* Native name of the language
* @param $direction
* LANGUAGE_LTR or LANGUAGE_RTL
* @param $domain
* Optional custom domain name with protocol, without
* trailing slash (eg. http://de.example.com).
* @param $prefix
* Optional path prefix for the language. Defaults to the
* language code if omitted.
* @param $enabled
* Optionally TRUE to enable the language when created or FALSE to disable.
* @param $default
* Optionally set this language to be the default.
*/
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;
function locale_language_save($language) {
$language->is_new = !(bool) db_query_range('SELECT 1 FROM {languages} WHERE language = :language', 0, 1, array(':language' => $language->language))->fetchField();
// Default prefix on language code if not provided otherwise.
if (!isset($language->prefix)) {
$language->prefix = $language->language;
}
// If name was not set, we add a predefined language.
if (!isset($name)) {
if (!isset($language->name)) {
include_once DRUPAL_ROOT . '/includes/standard.inc';
$predefined = standard_language_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;
$language->name = $predefined[$language->language][0];
$language->native = isset($predefined[$language->language][1]) ? $predefined[$language->language][1] : $predefined[$language->language][0];
$language->direction = isset($predefined[$language->language][2]) ? $predefined[$language->language][2] : LANGUAGE_LTR;
}
db_insert('languages')
->fields(array(
'language' => $langcode,
'name' => $name,
'native' => $native,
'direction' => $direction,
'domain' => $domain,
'prefix' => $prefix,
'enabled' => $enabled,
))
->execute();
// Set to enabled for the default language and unless specified otherwise.
if (!empty($language->default) || !isset($language->enabled)) {
$language->enabled = TRUE;
}
// Let other modules modify $language before saved.
module_invoke_all('locale_language_presave', $language);
// 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' => ''));
// Save the record and inform others about the change.
if ($language->is_new) {
drupal_write_record('languages', $language);
module_invoke_all('locale_language_insert', $language);
watchdog('locale', 'The %language (%langcode) language has been created.', array('%language' => $language->name, '%langcode' => $language->language));
}
else {
drupal_write_record('languages', $language, array('language'));
module_invoke_all('locale_language_update', $language);
watchdog('locale', 'The %language (%langcode) language has been updated.', array('%language' => $language->name, '%langcode' => $language->language));
}
if ($enabled) {
// Increment enabled language count if we are adding an enabled language.
variable_set('language_count', variable_get('language_count', 1) + 1);
if (!empty($language->default)) {
// Set the new version of this language as default in a variable.
$default_language = language_default();
variable_set('language_default', $language);
}
// Update language count based on enabled language count.
variable_set('language_count', db_query('SELECT COUNT(language) FROM {languages} WHERE enabled = 1')->fetchField());
// 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);
// @todo move these two cache clears out. See http://drupal.org/node/1293252
// Changing the language settings impacts the interface.
cache_clear_all('*', 'cache_page', TRUE);
// Force JavaScript translation file re-creation for the modified language.
_locale_invalidate_js($language->language);
watchdog('locale', 'The %language language (%code) has been created.', array('%language' => $name, '%code' => $langcode));
module_invoke_all('multilingual_settings_changed');
return $language;
}
/**
* @} End of "locale-api-add"
*/
/**
* Parses a JavaScript file, extracts strings wrapped in Drupal.t() and
......
......@@ -59,6 +59,20 @@
* field is returned by field_language().
*/
/**
* Implements hook_locale_language_insert().
*/
function field_locale_language_insert() {
field_info_cache_clear();
}
/**
* Implements hook_locale_language_update().
*/
function field_locale_language_update() {
field_info_cache_clear();
}
/**
* Implements hook_locale_language_delete().
*/
......
......@@ -2644,7 +2644,12 @@ class FieldTranslationsTestCase extends FieldTestCase {
require_once DRUPAL_ROOT . '/includes/locale.inc';
for ($i = 0; $i < 3; ++$i) {
locale_add_language('l' . $i, $this->randomString(), $this->randomString());
$language = (object) array(
'language' => 'l' . $i,
'name' => $this->randomString(),
'native' => $this->randomString(),
);
locale_language_save($language);
}
}
......
......@@ -119,60 +119,35 @@ function theme_locale_languages_overview_form($variables) {
*/
function locale_languages_overview_form_submit($form, &$form_state) {
$languages = language_list();
$default = language_default();
$old_default = language_default();
$url_prefixes = variable_get('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX;
$enabled_count = 0;
foreach ($languages as $langcode => $language) {
if ($form_state['values']['site_default'] == $langcode || $default->language == $langcode) {
$language->default = ($form_state['values']['site_default'] == $langcode);
$language->weight = $form_state['values']['weight'][$langcode];
if ($language->default || $old_default->language == $langcode) {
// Automatically enable the default language and the language
// which was default previously (because we will not get the
// value from that disabled checkbox).
$form_state['values']['enabled'][$langcode] = 1;
}
$language->enabled = (int) !empty($form_state['values']['enabled'][$langcode]);
// If language URL prefixes are enabled we must clear language domains and
// assign a valid prefix to each non-default language.
if ($url_prefixes) {
$language->domain = '';
if (empty($language->prefix) && $form_state['values']['site_default'] != $langcode) {
if (empty($language->prefix) && !$language->default) {
$language->prefix = $langcode;
}
}
if ($form_state['values']['enabled'][$langcode]) {
$enabled_count++;
$language->enabled = 1;
}
else {
$language->enabled = 0;
}
$language->weight = $form_state['values']['weight'][$langcode];
db_update('languages')
->fields(array(
'enabled' => $language->enabled,
'weight' => $language->weight,
'prefix' => $language->prefix,
'domain' => $language->domain,
))
->condition('language', $langcode)
->execute();
$languages[$langcode] = $language;
locale_language_save($language);
}
variable_set('language_default', $languages[$form_state['values']['site_default']]);
variable_set('language_count', $enabled_count);
drupal_set_message(t('Configuration saved.'));
// Changing the language settings impacts the interface.
cache('page')->flush();
module_invoke_all('multilingual_settings_changed');
$form_state['redirect'] = 'admin/config/regional/language';
return;
}
/**
......@@ -350,14 +325,25 @@ function locale_languages_predefined_form_submit($form, &$form_state) {
$langcode = $form_state['values']['langcode'];
if (isset($form_state['values']['name'])) {
// Custom language form.
locale_add_language($langcode, $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['direction'], $form_state['values']['domain'], $form_state['values']['prefix']);
$language = (object) array(
'language' => $langcode,
'name' => $form_state['values']['name'],
'native' => $form_state['values']['native'],
'direction' => $form_state['values']['direction'],
'domain' => $form_state['values']['domain'],
'prefix' => $form_state['values']['prefix'],
);
locale_language_save($language);
drupal_set_message(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => t($form_state['values']['name']), '@locale-help' => url('admin/help/locale'))));
}
else {
// Predefined language selection.
include_once DRUPAL_ROOT . '/includes/standard.inc';
$predefined = standard_language_list();
locale_add_language($langcode);
$language = (object) array(
'language' => $langcode,
);
locale_language_save($language);
drupal_set_message(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => t($predefined[$langcode][0]), '@locale-help' => url('admin/help/locale'))));
}
......@@ -404,28 +390,17 @@ function locale_languages_edit_form_validate($form, &$form_state) {
* Process the language editing form submission.
*/
function locale_languages_edit_form_submit($form, &$form_state) {
db_update('languages')
->fields(array(
'name' => $form_state['values']['name'],
'native' => $form_state['values']['native'],
'domain' => $form_state['values']['domain'],
'prefix' => $form_state['values']['prefix'],
'direction' => $form_state['values']['direction'],
))
->condition('language', $form_state['values']['langcode'])
->execute();
$default = language_default();
if ($default->language == $form_state['values']['langcode']) {
$properties = array('name', 'native', 'direction', 'enabled', 'plurals', 'formula', 'domain', 'prefix', 'weight');
foreach ($properties as $keyname) {
if (isset($form_state['values'][$keyname])) {
$default->$keyname = $form_state['values'][$keyname];
}
}
variable_set('language_default', $default);
}
// Prepare a language object for saving
$languages = language_list();
$langcode = $form_state['values']['langcode'];
$language = $languages[$langcode];
$language->name = $form_state['values']['name'];
$language->native = $form_state['values']['native'];
$language->direction = $form_state['values']['direction'];
$language->domain = $form_state['values']['domain'];
$language->prefix = $form_state['values']['prefix'];
locale_language_save($language);
$form_state['redirect'] = 'admin/config/regional/language';
return;
}
/**
......
......@@ -165,6 +165,39 @@ function hook_language_fallback_candidates_alter(array &$fallback_candidates) {
$fallback_candidates = array_reverse($fallback_candidates);
}
/**
* React to a language about to be added or updated in the system.
*
* @param $language
* A language object.
*/
function hook_locale_language_presave($language) {
if ($language->default) {
// React to a new default language.
example_new_default_language($language);
}
}
/**
* React to a language that was just added to the system.
*
* @param $language
* A language object.
*/
function hook_locale_language_insert($language) {
example_refresh_permissions();
}
/**
* React to a language that was just updated in the system.
*
* @param $language
* A language object.
*/
function hook_locale_language_update($language) {
example_refresh_permissions();
}
/**
* Allow modules to react before the deletion of a language.
*
......
......@@ -70,7 +70,10 @@ function locale_translate_import_form_submit($form, &$form_state) {
if (!isset($languages[$langcode])) {
include_once DRUPAL_ROOT . '/includes/standard.inc';
$predefined = standard_language_list();
locale_add_language($langcode);
$language = (object) array(
'language' => $langcode,
);
locale_language_save($language);
drupal_set_message(t('The language %language has been created.', array('%language' => t($predefined[$langcode][0]))));
}
......
This diff is collapsed.
......@@ -1215,7 +1215,11 @@ class DateTimeFunctionalTest extends DrupalWebTestCase {
$this->assertFalse($format, 'Unlocalized date format resides not in localized table.');
// Enable German language
locale_add_language('de', NULL, NULL, LANGUAGE_LTR, '', '', TRUE, 'en');
$language = (object) array(
'language' => 'de',
'default' => TRUE,
);
locale_language_save($language);
$date_format = array(
'type' => 'short',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment