Index: locale.module =================================================================== RCS file: /cvs/drupal/drupal/modules/locale/locale.module,v retrieving revision 1.236 diff -u -r1.236 locale.module --- locale.module 22 Jan 2009 03:11:53 -0000 1.236 +++ locale.module 4 Feb 2009 16:17:22 -0000 @@ -196,7 +196,18 @@ function locale_locale($op = 'groups') { switch ($op) { case 'groups': - return array('default' => t('Built-in interface')); + return array( + 'default' => array( + 'name' => t('Built-in interface'), + 'index' => 'source', + 'cache' => TRUE + ), + 'text' => array( + 'name' => t('Long texts from modules'), + 'index' => 'location', + 'cache' => TRUE, + ), + ); } } @@ -596,3 +607,116 @@ return $block; } } + +// --------------------------------------------------------------------------------- +// Locale data API + +/** + * Get information about text groups + */ +function locale_textgroup($group = NULL, $key = NULL) { + static $textgroups; + + if (!isset($textgroups)) { + $groups = module_invoke_all('locale', 'groups'); + } + + if ($group && $key) { + return isset($textgroups[$group][$key]) ? $textgroups[$group][$key] : NULL; + } + elseif ($group) { + return isset($textgroups[$group]) ? $textgroups[$group] : NULL; + } + else { + return $textgroups; + } +} + +/** + * Get localized text for other groups than default, search by location. + * + * This will search by location field instead of source string, and is used for long module texts + * + * @param $textgroup + * Text group to get strings for + * @param $key + * Value for the text group search key + * @param $langcode + * Optional language code to translate to a language other than what is used + */ +function locale_get_translation($textgroup, $key, $langcode) { + static $_cache = array(); + + if (!isset($_cache[$textgroup][$key][$langcode])) { + if ($field = locale_textgroup($textgroup, 'key')) { + $query = db_select('locales_target', 's'); + $query->fields('lid', 'translation', 'plural'); + $query->join('locales_source', 's', 's.lid = t.lid'); + $query->condition($field, $key); + $query->condition('language', $langcode); + $translation = $query->execute()->fetchObject(); + $_cache[$textgroup][$key] = $translation ? $translation : FALSE; + } + $_cache[$textgroup][$key][$langcode] = !empty($translation) ? $translation : FALSE; + } + + return $_cache[$textgroup][$key][$langcode]; +} + +/** + * Get source for a string, given a key and a value + */ +function locale_get_source($textgroup, $key) { + static $_cache = array(); + + if (!isset($_cache[$textgroup][$key])) { + if ($field = locale_textgroup($textgroup, 'key')) { + $query = db_select('locales_source', 's'); + $query->fields('lid', 'source', 'location', 'version'); + $query->condition('textgroup', $textgroup); + $query->condition($field, $key); + $source = $query->execute()->fetchObject(); + } + $_cache[$textgroup][$key] = !empty($translation) ? $translation : FALSE; + } + + return $_cache[$textgroup][$key]; +} + +/** + * Save / update a source string + * + * @param $string + * Object representing a source string + */ +function locale_save_source(&$string) { + // Invalidate cache if this text group is cacheable + if (locale_textgroup($string->textgroup, 'cache')) { + variable_set('locale_rebuild_' . $string->textgroup, 1); + } + if (!empty($string->lid)) { + return drupal_write_record('locale_source', $string, 'lid'); + } else { + return drupal_write_record('locale_source', $string); + } +} + +/** + * Save / update a translation + * + * @param $string + * Object representing a string translation + */ +function locale_save_translation(&$string) { + // Invalidate cache if this text group is cacheable + if (locale_textgroup($string->textgroup, 'cache')) { + variable_set('locale_rebuild_' . $source->textgroup, 1); + } + // If a current translation exists, just update the record + $key = locale_textgroup($string->textgroup, 'key'); + if ($key && !empty($string->key) && locale_get_translation($string->textgroup, $string->$key, $string->language)) { + return drupal_write_record('locale_target', $string, array('lid', $string->language)); + } else { + return drupal_write_record('locale_target', $string); + } +} \ No newline at end of file