Commit 352a8e43 authored by Dries's avatar Dries

- Patch #473366 by Berdir: convert locale module to the new database abstraction layer.

parent 66108c34
......@@ -17,7 +17,17 @@ function locale_install() {
// Create tables.
drupal_install_schema('locale');
db_query("INSERT INTO {languages} (language, name, native, direction, enabled, weight, javascript) VALUES ('en', 'English', 'English', '0', '1', '0', '')");
db_insert('languages')
->fields(array(
'language' => 'en',
'name' => 'English',
'native' => 'English',
'direction' => 0,
'enabled' => 1,
'weight' => 0,
'javascript' => '',
))
->execute();
}
/**
......@@ -217,7 +227,7 @@ function locale_uninstall() {
// Delete all JavaScript translation files.
$locale_js_directory = file_create_path(variable_get('locale_js_directory', 'languages'));
$files = db_query('SELECT language, javascript FROM {languages}');
while ($file = db_fetch_object($files)) {
foreach ($files as $file) {
if (!empty($file->javascript)) {
file_unmanaged_delete(file_create_path($locale_js_directory . '/' . $file->language . '_' . $file->javascript . '.js'));
}
......
......@@ -377,8 +377,8 @@ function locale($string = NULL, $langcode = NULL, $reset = FALSE) {
// Refresh database stored cache of translations for given language.
// We only store short strings used in current version, to improve
// performance and consume less memory.
$result = db_query("SELECT s.source, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = 'default' AND s.version = '%s' AND LENGTH(s.source) < 75", $langcode, VERSION);
while ($data = db_fetch_object($result)) {
$result = db_query("SELECT s.source, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.textgroup = 'default' AND s.version = :version AND LENGTH(s.source) < 75", array(':language' => $langcode, ':version' => VERSION));
foreach ($result as $data) {
$locale_t[$langcode][$data->source] = (empty($data->translation) ? TRUE : $data->translation);
}
cache_set('locale:' . $langcode, $locale_t[$langcode]);
......@@ -390,7 +390,10 @@ function locale($string = NULL, $langcode = NULL, $reset = FALSE) {
if (!isset($locale_t[$langcode][$string])) {
// We do not have this translation cached, so get it from the DB.
$translation = db_fetch_object(db_query("SELECT s.lid, t.translation, s.version FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.source = '%s' AND s.textgroup = 'default'", $langcode, $string));
$translation = db_query("SELECT s.lid, t.translation, s.version FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.source = :source AND s.textgroup = 'default'", array(
':language' => $langcode,
':source' => $string,
))->fetchObject();
if ($translation) {
// We have the source string at least.
// Cache translation string or TRUE if no translation exists.
......@@ -400,13 +403,23 @@ function locale($string = NULL, $langcode = NULL, $reset = FALSE) {
// This is the first use of this string under current Drupal version. Save version
// and clear cache, to include the string into caching next time. Saved version is
// also a string-history information for later pruning of the tables.
db_query("UPDATE {locales_source} SET version = '%s' WHERE lid = %d", VERSION, $translation->lid);
db_update('locales_source')
->fields(array('version' => VERSION))
->condition('lid', $translation->lid)
->execute();
cache_clear_all('locale:', 'cache', TRUE);
}
}
else {
// We don't have the source string, cache this as untranslated.
db_query("INSERT INTO {locales_source} (location, source, textgroup, version) VALUES ('%s', '%s', 'default', '%s')", request_uri(), $string, VERSION);
db_insert('locales_source')
->fields(array(
'location' => request_uri(),
'source' => $string,
'textgroup' => 'default',
'version' => VERSION,
))
->execute();
$locale_t[$langcode][$string] = TRUE;
// Clear locale cache so this string can be added in a later request.
cache_clear_all('locale:', 'cache', TRUE);
......
......@@ -906,11 +906,14 @@ class LocaleUninstallFunctionalTest extends DrupalWebTestCase {
$user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
$this->drupalLogin($user);
$this->drupalGet('admin/international/translate/translate');
$string = db_fetch_object(db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE \'%.js%\' AND textgroup = \'default\''));
$string = db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE :location AND textgroup = :textgroup', array(
':location' => '%.js%',
':textgroup' => 'default',
))->fetchObject();
$edit = array('translations[fr]' => 'french translation');
$this->drupalPost('admin/international/translate/edit/' . $string->lid, $edit, t('Save translations'));
_locale_rebuild_js('fr');
$file = db_fetch_object(db_query('SELECT javascript FROM {languages} WHERE language = \'fr\''));
$file = db_query('SELECT javascript FROM {languages} WHERE language = :language', array(':language' => 'fr'))->fetchObject();
$js_file = file_create_path(variable_get('locale_js_directory', 'languages')) . '/fr_' . $file->javascript . '.js';
$this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('none'))));
......
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