locale.module

You are here

  1. 7 modules/locale/locale.module
  2. 4.6 modules/locale.module
  3. 4.7 modules/locale.module
  4. 5 modules/locale/locale.module
  5. 6 modules/locale/locale.module
  6. 8 core/modules/locale/locale.module

Add language handling functionality and enables the translation of the user interface to languages other than English.

When enabled, multiple languages can be set up. The site interface can be displayed in different languages, as well as nodes can have languages assigned. The setup of languages and translations is completely web based. Gettext portable object files are supported.

Functions

Namesort descending Description
locale Provides interface translation services.
locale_block Implementation of hook_block(). Displays a language switcher. Translation links may be provided by other modules.
locale_form_alter Implementation of hook_form_alter(). Adds language fields to forms.
locale_get_plural Returns plural form index for a specific number.
locale_help Implementation of hook_help().
locale_inc_callback Wrapper function to be able to set callbacks in locale.inc
locale_language_list Returns array of language names
locale_language_name Returns a language name
locale_locale Implementation of hook_locale().
locale_menu Implementation of hook_menu().
locale_node_type Implementation of hook_node_type().
locale_perm Implementation of hook_perm().
locale_system_update Imports translations when new modules or themes are installed or enabled.
locale_theme Implementation of hook_theme()
locale_update_js_files Update JavaScript translation file, if required, and add it to the page.
locale_user Implementation of hook_user().

File

modules/locale/locale.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Add language handling functionality and enables the translation of the
  5. * user interface to languages other than English.
  6. *
  7. * When enabled, multiple languages can be set up. The site interface
  8. * can be displayed in different languages, as well as nodes can have languages
  9. * assigned. The setup of languages and translations is completely web based.
  10. * Gettext portable object files are supported.
  11. */
  12. // ---------------------------------------------------------------------------------
  13. // Hook implementations
  14. /**
  15. * Implementation of hook_help().
  16. */
  17. function locale_help($path, $arg) {
  18. switch ($path) {
  19. case 'admin/help#locale':
  20. $output = '<p>'. t('The locale module allows your Drupal site to be presented in languages other than the default English, a defining feature of multi-lingual websites. The locale module works by examining text as it is about to be displayed: when a translation of the text is available in the language to be displayed, the translation is displayed rather than the original text. When a translation is unavailable, the original text is displayed, and then stored for later review by a translator.') .'</p>';
  21. $output .= '<p>'. t('Beyond translation of the Drupal interface, the locale module provides a feature set tailored to the needs of a multi-lingual site. Language negotiation allows your site to automatically change language based on the domain or path used for each request. Users may (optionally) select their preferred language on their <em>My account</em> page, and your site can be configured to honor a web browser\'s preferred language settings. Your site content can be created in (and translated to) any enabled language, and each post may have a language-appropriate alias for each of its translations. The locale module works in concert with the <a href="@content-help">content translation module</a> to manage translated content.', array('@content-help' => url('admin/help/translation'))) .'</p>';
  22. $output .= '<p>'. t('Translations may be provided by:') .'</p>';
  23. $output .= '<ul><li>'. t("translating the original text via the locale module's integrated web interface, or") .'</li>';
  24. $output .= '<li>'. t('importing files from a set of existing translations, known as a translation package. A translation package enables the display of a specific version of Drupal in a specific language, and contain files in the Gettext Portable Object (<em>.po</em>) format. Although not all languages are available for every version of Drupal, translation packages for many languages are available for download from the <a href="@translations">Drupal translation page</a>.', array('@translations' => 'http://localize.drupal.org')) .'</li></ul>';
  25. $output .= '<p>'. t('If an existing translation package does not meet your needs, the Gettext Portable Object (<em>.po</em>) files within a package may be modified, or new <em>.po</em> files may be created, using a desktop Gettext editor. The locale module\'s <a href="@import">import</a> feature allows the translated strings from a new or modified <em>.po</em> file to be added to your site. The locale module\'s <a href="@export">export</a> feature generates files from your site\'s translated strings, that can either be shared with others or edited offline by a Gettext translation editor.', array('@import' => url('admin/build/translate/import'), '@export' => url('admin/build/translate/export'))) .'</p>';
  26. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@locale">Locale module</a>.', array('@locale' => 'http://drupal.org/handbook/modules/locale/')) .'</p>';
  27. return $output;
  28. case 'admin/settings/language':
  29. $output = '<p>'. t("This page provides an overview of your site's enabled languages. If multiple languages are available and enabled, the text on your site interface may be translated, registered users may select their preferred language on the <em>My account</em> page, and site authors may indicate a specific language when creating posts. The site's default language is used for anonymous visitors and for users who have not selected a preferred language.") .'</p>';
  30. $output .= '<p>'. t('For each language available on the site, use the <em>edit</em> link to configure language details, including name, an optional language-specific path or domain, and whether the language is natively presented either left-to-right or right-to-left. These languages also appear in the <em>Language</em> selection when creating a post of a content type with multilingual support.') .'</p>';
  31. $output .= '<p>'. t('Use the <a href="@add-language">add language page</a> to enable additional languages (and automatically import files from a translation package, if available), the <a href="@search">translate interface page</a> to locate strings for manual translation, or the <a href="@import">import page</a> to add translations from individual <em>.po</em> files. A number of contributed translation packages containing <em>.po</em> files are available on the <a href="@translations">Drupal.org translations page</a>.', array('@add-language' => url('admin/settings/language/add'), '@search' => url('admin/build/translate/search'), '@import' => url('admin/build/translate/import'), '@translations' => 'http://localize.drupal.org')) .'</p>';
  32. return $output;
  33. case 'admin/settings/language/add':
  34. return '<p>'. t('Add all languages to be supported by your site. If your desired language is not available in the <em>Language name</em> drop-down, click <em>Custom language</em> and provide a language code and other details manually. When providing a language code manually, be sure to enter a standardized language code, since this code may be used by browsers to determine an appropriate display language.') .'</p>';
  35. case 'admin/settings/language/configure':
  36. $output = '<p>'. t("Language negotiation settings determine the site's presentation language. Available options include:") .'</p>';
  37. $output .= '<ul><li>'. t('<strong>None.</strong> The default language is used for site presentation, though users may (optionally) select a preferred language on the <em>My Account</em> page. (User language preferences will be used for site e-mails, if available.)') .'</li>';
  38. $output .= '<li>'. t('<strong>Path prefix only.</strong> The presentation language is determined by examining the path for a language code or other custom string that matches the path prefix (if any) specified for each language. If a suitable prefix is not identified, the default language is used. <em>Example: "example.com/de/contact" sets presentation language to German based on the use of "de" within the path.</em>') .'</li>';
  39. $output .= '<li>'. t("<strong>Path prefix with language fallback.</strong> The presentation language is determined by examining the path for a language code or other custom string that matches the path prefix (if any) specified for each language. If a suitable prefix is not identified, the display language is determined by the user's language preferences from the <em>My Account</em> page, or by the browser's language settings. If a presentation language cannot be determined, the default language is used.") .'</li>';
  40. $output .= '<li>'. t('<strong>Domain name only.</strong> The presentation language is determined by examining the domain used to access the site, and comparing it to the language domain (if any) specified for each language. If a match is not identified, the default language is used. <em>Example: "http://de.example.com/contact" sets presentation language to German based on the use of "http://de.example.com" in the domain.</em>') .'</li></ul>';
  41. $output .= '<p>'. t('The path prefix or domain name for a language may be set by editing the <a href="@languages">available languages</a>. In the absence of an appropriate match, the site is displayed in the <a href="@languages">default language</a>.', array('@languages' => url('admin/settings/language'))) .'</p>';
  42. return $output;
  43. case 'admin/build/translate':
  44. $output = '<p>'. t('This page provides an overview of available translatable strings. Drupal displays translatable strings in text groups; modules may define additional text groups containing other translatable strings. Because text groups provide a method of grouping related strings, they are often used to focus translation efforts on specific areas of the Drupal interface.') .'</p>';
  45. $output .= '<p>'. t('Review the <a href="@languages">languages page</a> for more information on adding support for additional languages.', array('@languages' => url('admin/settings/language'))) .'</p>';
  46. return $output;
  47. case 'admin/build/translate/import':
  48. $output = '<p>'. t('This page imports the translated strings contained in an individual Gettext Portable Object (<em>.po</em>) file. Normally distributed as part of a translation package (each translation package may contain several <em>.po</em> files), a <em>.po</em> file may need to be imported after off-line editing in a Gettext translation editor. Importing an individual <em>.po</em> file may be a lengthy process.') .'</p>';
  49. $output .= '<p>'. t('Note that the <em>.po</em> files within a translation package are imported automatically (if available) when new modules or themes are enabled, or as new languages are added. Since this page only allows the import of one <em>.po</em> file at a time, it may be simpler to download and extract a translation package into your Drupal installation directory and <a href="@language-add">add the language</a> (which automatically imports all <em>.po</em> files within the package). Translation packages are available for download on the <a href="@translations">Drupal translation page</a>.', array('@language-add' => url('admin/settings/language/add'), '@translations' => 'http://localize.drupal.org')) .'</p>';
  50. return $output;
  51. case 'admin/build/translate/export':
  52. return '<p>'. t('This page exports the translated strings used by your site. An export file may be in Gettext Portable Object (<em>.po</em>) form, which includes both the original string and the translation (used to share translations with others), or in Gettext Portable Object Template (<em>.pot</em>) form, which includes the original strings only (used to create new translations with a Gettext translation editor).') .'</p>';
  53. case 'admin/build/translate/search':
  54. return '<p>'. t('This page allows a translator to search for specific translated and untranslated strings, and is used when creating or editing translations. (Note: For translation tasks involving many strings, it may be more convenient to <a href="@export">export</a> strings for off-line editing in a desktop Gettext translation editor.) Searches may be limited to strings found within a specific text group or in a specific language.', array('@export' => url('admin/build/translate/export'))) .'</p>';
  55. case 'admin/build/block/configure':
  56. if ($arg[4] == 'locale' && $arg[5] == 0) {
  57. return '<p>'. t('This block is only shown if <a href="@languages">at least two languages are enabled</a> and <a href="@configuration">language negotiation</a> is set to something other than <em>None</em>.', array('@languages' => url('admin/settings/language'), '@configuration' => url('admin/settings/language/configure'))) .'</p>';
  58. }
  59. break;
  60. }
  61. }
  62. /**
  63. * Implementation of hook_menu().
  64. *
  65. * Locale module only provides administrative menu items, so all
  66. * menu items are invoked through locale_inc_callback().
  67. */
  68. function locale_menu() {
  69. // Manage languages
  70. $items['admin/settings/language'] = array(
  71. 'title' => 'Languages',
  72. 'description' => 'Configure languages for content and the user interface.',
  73. 'page callback' => 'locale_inc_callback',
  74. 'page arguments' => array('drupal_get_form', 'locale_languages_overview_form'),
  75. 'access arguments' => array('administer languages'),
  76. );
  77. $items['admin/settings/language/overview'] = array(
  78. 'title' => 'List',
  79. 'weight' => 0,
  80. 'type' => MENU_DEFAULT_LOCAL_TASK,
  81. );
  82. $items['admin/settings/language/add'] = array(
  83. 'title' => 'Add language',
  84. 'page callback' => 'locale_inc_callback',
  85. 'page arguments' => array('locale_languages_add_screen'), // two forms concatenated
  86. 'access arguments' => array('administer languages'),
  87. 'weight' => 5,
  88. 'type' => MENU_LOCAL_TASK,
  89. );
  90. $items['admin/settings/language/configure'] = array(
  91. 'title' => 'Configure',
  92. 'page callback' => 'locale_inc_callback',
  93. 'page arguments' => array('drupal_get_form', 'locale_languages_configure_form'),
  94. 'access arguments' => array('administer languages'),
  95. 'weight' => 10,
  96. 'type' => MENU_LOCAL_TASK,
  97. );
  98. $items['admin/settings/language/edit/%'] = array(
  99. 'title' => 'Edit language',
  100. 'page callback' => 'locale_inc_callback',
  101. 'page arguments' => array('drupal_get_form', 'locale_languages_edit_form', 4),
  102. 'access arguments' => array('administer languages'),
  103. 'type' => MENU_CALLBACK,
  104. );
  105. $items['admin/settings/language/delete/%'] = array(
  106. 'title' => 'Confirm',
  107. 'page callback' => 'locale_inc_callback',
  108. 'page arguments' => array('drupal_get_form', 'locale_languages_delete_form', 4),
  109. 'access arguments' => array('administer languages'),
  110. 'type' => MENU_CALLBACK,
  111. );
  112. // Translation functionality
  113. $items['admin/build/translate'] = array(
  114. 'title' => 'Translate interface',
  115. 'description' => 'Translate the built in interface and optionally other text.',
  116. 'page callback' => 'locale_inc_callback',
  117. 'page arguments' => array('locale_translate_overview_screen'), // not a form, just a table
  118. 'access arguments' => array('translate interface'),
  119. );
  120. $items['admin/build/translate/overview'] = array(
  121. 'title' => 'Overview',
  122. 'weight' => 0,
  123. 'type' => MENU_DEFAULT_LOCAL_TASK,
  124. );
  125. $items['admin/build/translate/search'] = array(
  126. 'title' => 'Search',
  127. 'weight' => 10,
  128. 'type' => MENU_LOCAL_TASK,
  129. 'page callback' => 'locale_inc_callback',
  130. 'page arguments' => array('locale_translate_seek_screen'), // search results and form concatenated
  131. 'access arguments' => array('translate interface'),
  132. );
  133. $items['admin/build/translate/import'] = array(
  134. 'title' => 'Import',
  135. 'page callback' => 'locale_inc_callback',
  136. 'page arguments' => array('drupal_get_form', 'locale_translate_import_form'),
  137. 'access arguments' => array('translate interface'),
  138. 'weight' => 20,
  139. 'type' => MENU_LOCAL_TASK,
  140. );
  141. $items['admin/build/translate/export'] = array(
  142. 'title' => 'Export',
  143. 'page callback' => 'locale_inc_callback',
  144. 'page arguments' => array('locale_translate_export_screen'), // possibly multiple forms concatenated
  145. 'access arguments' => array('translate interface'),
  146. 'weight' => 30,
  147. 'type' => MENU_LOCAL_TASK,
  148. );
  149. $items['admin/build/translate/edit/%'] = array(
  150. 'title' => 'Edit string',
  151. 'page callback' => 'locale_inc_callback',
  152. 'page arguments' => array('drupal_get_form', 'locale_translate_edit_form', 4),
  153. 'access arguments' => array('translate interface'),
  154. 'type' => MENU_CALLBACK,
  155. );
  156. $items['admin/build/translate/delete/%'] = array(
  157. 'title' => 'Delete string',
  158. 'page callback' => 'locale_inc_callback',
  159. 'page arguments' => array('locale_translate_delete_page', 4),
  160. 'access arguments' => array('translate interface'),
  161. 'type' => MENU_CALLBACK,
  162. );
  163. return $items;
  164. }
  165. /**
  166. * Wrapper function to be able to set callbacks in locale.inc
  167. */
  168. function locale_inc_callback() {
  169. $args = func_get_args();
  170. $function = array_shift($args);
  171. include_once './includes/locale.inc';
  172. return call_user_func_array($function, $args);
  173. }
  174. /**
  175. * Implementation of hook_perm().
  176. */
  177. function locale_perm() {
  178. return array('administer languages', 'translate interface');
  179. }
  180. /**
  181. * Implementation of hook_locale().
  182. */
  183. function locale_locale($op = 'groups') {
  184. switch ($op) {
  185. case 'groups':
  186. return array('default' => t('Built-in interface'));
  187. }
  188. }
  189. /**
  190. * Implementation of hook_user().
  191. */
  192. function locale_user($type, $edit, &$user, $category = NULL) {
  193. global $language;
  194. // If we have more then one language and either creating a user on the
  195. // admin interface or edit the user, show the language selector.
  196. if (variable_get('language_count', 1) > 1 && ($type == 'register' && user_access('administer users') || $type == 'form' && $category == 'account' )) {
  197. $languages = language_list('enabled');
  198. $languages = $languages[1];
  199. // If the user is being created, we set the user language to the page language.
  200. $user_preferred_language = $user ? user_preferred_language($user) : $language;
  201. $names = array();
  202. foreach ($languages as $langcode => $item) {
  203. $name = t($item->name);
  204. $names[check_plain($langcode)] = check_plain($name . ($item->native != $name ? ' ('. $item->native .')' : ''));
  205. }
  206. $form['locale'] = array(
  207. '#type' => 'fieldset',
  208. '#title' => t('Language settings'),
  209. '#weight' => 1,
  210. );
  211. // Get language negotiation settings.
  212. $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
  213. $form['locale']['language'] = array(
  214. '#type' => (count($names) <= 5 ? 'radios' : 'select'),
  215. '#title' => t('Language'),
  216. '#default_value' => check_plain($user_preferred_language->language),
  217. '#options' => $names,
  218. '#description' => ($mode == LANGUAGE_NEGOTIATION_PATH) ? t("This account's default language for e-mails, and preferred language for site presentation.") : t("This account's default language for e-mails."),
  219. );
  220. return $form;
  221. }
  222. }
  223. /**
  224. * Implementation of hook_form_alter(). Adds language fields to forms.
  225. */
  226. function locale_form_alter(&$form, $form_state, $form_id) {
  227. switch ($form_id) {
  228. // Language field for paths
  229. case 'path_admin_form':
  230. $form['language'] = array(
  231. '#type' => 'select',
  232. '#title' => t('Language'),
  233. '#options' => array('' => t('All languages')) + locale_language_list('name'),
  234. '#default_value' => $form['language']['#value'],
  235. '#weight' => -10,
  236. '#description' => t('A path alias set for a specific language will always be used when displaying this page in that language, and takes precedence over path aliases set for <em>All languages</em>.'),
  237. );
  238. break;
  239. // Language setting for content types
  240. case 'node_type_form':
  241. if (isset($form['identity']['type'])) {
  242. $form['workflow']['language_content_type'] = array(
  243. '#type' => 'radios',
  244. '#title' => t('Multilingual support'),
  245. '#default_value' => variable_get('language_content_type_'. $form['#node_type']->type, 0),
  246. '#options' => array(t('Disabled'), t('Enabled')),
  247. '#description' => t('Enable multilingual support for this content type. If enabled, a language selection field will be added to the editing form, allowing you to select from one of the <a href="!languages">enabled languages</a>. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('!languages' => url('admin/settings/language'))),
  248. );
  249. }
  250. break;
  251. // Language field for nodes
  252. default:
  253. if (isset($form['#id']) && $form['#id'] == 'node-form') {
  254. if (isset($form['#node']->type) && variable_get('language_content_type_'. $form['#node']->type, 0)) {
  255. $form['language'] = array(
  256. '#type' => 'select',
  257. '#title' => t('Language'),
  258. '#default_value' => (isset($form['#node']->language) ? $form['#node']->language : ''),
  259. '#options' => array('' => t('Language neutral')) + locale_language_list('name'),
  260. );
  261. }
  262. // Node type without language selector: assign the default for new nodes
  263. elseif (!isset($form['#node']->nid)) {
  264. $default = language_default();
  265. $form['language'] = array(
  266. '#type' => 'value',
  267. '#value' => $default->language
  268. );
  269. }
  270. }
  271. }
  272. }
  273. /**
  274. * Implementation of hook_theme()
  275. */
  276. function locale_theme() {
  277. return array(
  278. 'locale_languages_overview_form' => array(
  279. 'arguments' => array('form' => array()),
  280. ),
  281. );
  282. }
  283. /**
  284. * Implementation of hook_node_type().
  285. */
  286. function locale_node_type($op, $info) {
  287. if ($op == 'delete') {
  288. variable_del('language_content_type_'. $info->type);
  289. }
  290. }
  291. // ---------------------------------------------------------------------------------
  292. // Locale core functionality
  293. /**
  294. * Provides interface translation services.
  295. *
  296. * This function is called from t() to translate a string if needed.
  297. *
  298. * @param $string
  299. * A string to look up translation for. If omitted, all the
  300. * cached strings will be returned in all languages already
  301. * used on the page.
  302. * @param $langcode
  303. * Language code to use for the lookup.
  304. * @param $reset
  305. * Set to TRUE to reset the in-memory cache.
  306. */
  307. function locale($string = NULL, $langcode = NULL, $reset = FALSE) {
  308. global $language;
  309. static $locale_t;
  310. if ($reset) {
  311. // Reset in-memory cache.
  312. $locale_t = NULL;
  313. }
  314. if (!isset($string)) {
  315. // Return all cached strings if no string was specified
  316. return $locale_t;
  317. }
  318. $langcode = isset($langcode) ? $langcode : $language->language;
  319. // Store database cached translations in a static var.
  320. if (!isset($locale_t[$langcode])) {
  321. $locale_t[$langcode] = array();
  322. // Disabling the usage of string caching allows a module to watch for
  323. // the exact list of strings used on a page. From a performance
  324. // perspective that is a really bad idea, so we have no user
  325. // interface for this. Be careful when turning this option off!
  326. if (variable_get('locale_cache_strings', 1) == 1) {
  327. if ($cache = cache_get('locale:'. $langcode, 'cache')) {
  328. $locale_t[$langcode] = $cache->data;
  329. }
  330. elseif (lock_acquire('locale_cache_' . $langcode)) {
  331. // Refresh database stored cache of translations for given language.
  332. // We only store short strings used in current version, to improve
  333. // performance and consume less memory.
  334. $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);
  335. while ($data = db_fetch_object($result)) {
  336. $locale_t[$langcode][$data->source] = (empty($data->translation) ? TRUE : $data->translation);
  337. }
  338. cache_set('locale:'. $langcode, $locale_t[$langcode]);
  339. lock_release('locale_cache_' . $langcode);
  340. }
  341. }
  342. }
  343. // If we have the translation cached, skip checking the database
  344. if (!isset($locale_t[$langcode][$string])) {
  345. // We do not have this translation cached, so get it from the DB.
  346. $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));
  347. if ($translation) {
  348. // We have the source string at least.
  349. // Cache translation string or TRUE if no translation exists.
  350. $locale_t[$langcode][$string] = (empty($translation->translation) ? TRUE : $translation->translation);
  351. if ($translation->version != VERSION) {
  352. // This is the first use of this string under current Drupal version. Save version
  353. // and clear cache, to include the string into caching next time. Saved version is
  354. // also a string-history information for later pruning of the tables.
  355. db_query("UPDATE {locales_source} SET version = '%s' WHERE lid = %d", VERSION, $translation->lid);
  356. cache_clear_all('locale:', 'cache', TRUE);
  357. }
  358. }
  359. else {
  360. // We don't have the source string, cache this as untranslated.
  361. db_query("INSERT INTO {locales_source} (location, source, textgroup, version) VALUES ('%s', '%s', 'default', '%s')", request_uri(), $string, VERSION);
  362. $locale_t[$langcode][$string] = TRUE;
  363. // Clear locale cache so this string can be added in a later request.
  364. cache_clear_all('locale:', 'cache', TRUE);
  365. }
  366. }
  367. return ($locale_t[$langcode][$string] === TRUE ? $string : $locale_t[$langcode][$string]);
  368. }
  369. /**
  370. * Returns plural form index for a specific number.
  371. *
  372. * The index is computed from the formula of this language.
  373. *
  374. * @param $count
  375. * Number to return plural for.
  376. * @param $langcode
  377. * Optional language code to translate to a language other than
  378. * what is used to display the page.
  379. */
  380. function locale_get_plural($count, $langcode = NULL) {
  381. global $language;
  382. static $locale_formula, $plurals = array();
  383. $langcode = $langcode ? $langcode : $language->language;
  384. if (!isset($plurals[$langcode][$count])) {
  385. if (!isset($locale_formula)) {
  386. $language_list = language_list();
  387. $locale_formula[$langcode] = $language_list[$langcode]->formula;
  388. }
  389. if ($locale_formula[$langcode]) {
  390. $n = $count;
  391. $plurals[$langcode][$count] = @eval('return intval('. $locale_formula[$langcode] .');');
  392. return $plurals[$langcode][$count];
  393. }
  394. else {
  395. $plurals[$langcode][$count] = -1;
  396. return -1;
  397. }
  398. }
  399. return $plurals[$langcode][$count];
  400. }
  401. /**
  402. * Returns a language name
  403. */
  404. function locale_language_name($lang) {
  405. static $list = NULL;
  406. if (!isset($list)) {
  407. $list = locale_language_list();
  408. }
  409. return ($lang && isset($list[$lang])) ? $list[$lang] : t('All');
  410. }
  411. /**
  412. * Returns array of language names
  413. *
  414. * @param $field
  415. * 'name' => names in current language, localized
  416. * 'native' => native names
  417. * @param $all
  418. * Boolean to return all languages or only enabled ones
  419. */
  420. function locale_language_list($field = 'name', $all = FALSE) {
  421. if ($all) {
  422. $languages = language_list();
  423. }
  424. else {
  425. $languages = language_list('enabled');
  426. $languages = $languages[1];
  427. }
  428. $list = array();
  429. foreach ($languages as $language) {
  430. $list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field;
  431. }
  432. return $list;
  433. }
  434. /**
  435. * Imports translations when new modules or themes are installed or enabled.
  436. *
  437. * This function will either import translation for the component change
  438. * right away, or start a batch if more files need to be imported.
  439. *
  440. * @param $components
  441. * An array of component (theme and/or module) names to import
  442. * translations for.
  443. */
  444. function locale_system_update($components) {
  445. include_once 'includes/locale.inc';
  446. if ($batch = locale_batch_by_component($components)) {
  447. batch_set($batch);
  448. }
  449. }
  450. /**
  451. * Update JavaScript translation file, if required, and add it to the page.
  452. *
  453. * This function checks all JavaScript files currently added via drupal_add_js()
  454. * and invokes parsing if they have not yet been parsed for Drupal.t()
  455. * and Drupal.formatPlural() calls. Also refreshes the JavaScript translation
  456. * file if necessary, and adds it to the page.
  457. */
  458. function locale_update_js_files() {
  459. global $language;
  460. $dir = file_create_path(variable_get('locale_js_directory', 'languages'));
  461. $parsed = variable_get('javascript_parsed', array());
  462. // The first three parameters are NULL in order to get an array with all
  463. // scopes. This is necessary to prevent recreation of JS translation files
  464. // when new files are added for example in the footer.
  465. $javascript = drupal_add_js(NULL, NULL, NULL);
  466. $files = $new_files = FALSE;
  467. foreach ($javascript as $scope) {
  468. foreach ($scope as $type => $data) {
  469. if ($type != 'setting' && $type != 'inline') {
  470. foreach ($data as $filepath => $info) {
  471. $files = TRUE;
  472. if (!in_array($filepath, $parsed)) {
  473. // Don't parse our own translations files.
  474. if (substr($filepath, 0, strlen($dir)) != $dir) {
  475. locale_inc_callback('_locale_parse_js_file', $filepath);
  476. $parsed[] = $filepath;
  477. $new_files = TRUE;
  478. }
  479. }
  480. }
  481. }
  482. }
  483. }
  484. // If there are any new source files we parsed, invalidate existing
  485. // JavaScript translation files for all languages, adding the refresh
  486. // flags into the existing array.
  487. if ($new_files) {
  488. $parsed += locale_inc_callback('_locale_invalidate_js');
  489. }
  490. // If necessary, rebuild the translation file for the current language.
  491. if (!empty($parsed['refresh:'. $language->language])) {
  492. // Don't clear the refresh flag on failure, so that another try will
  493. // be performed later.
  494. if (locale_inc_callback('_locale_rebuild_js')) {
  495. unset($parsed['refresh:'. $language->language]);
  496. }
  497. // Store any changes after refresh was attempted.
  498. variable_set('javascript_parsed', $parsed);
  499. }
  500. // If no refresh was attempted, but we have new source files, we need
  501. // to store them too. This occurs if current page is in English.
  502. else if ($new_files) {
  503. variable_set('javascript_parsed', $parsed);
  504. }
  505. // Add the translation JavaScript file to the page.
  506. if ($files && !empty($language->javascript)) {
  507. drupal_add_js($dir .'/'. $language->language .'_'. $language->javascript .'.js', 'core');
  508. }
  509. }
  510. // ---------------------------------------------------------------------------------
  511. // Language switcher block
  512. /**
  513. * Implementation of hook_block().
  514. * Displays a language switcher. Translation links may be provided by other modules.
  515. */
  516. function locale_block($op = 'list', $delta = 0) {
  517. if ($op == 'list') {
  518. $block[0]['info'] = t('Language switcher');
  519. // Not worth caching.
  520. $block[0]['cache'] = BLOCK_NO_CACHE;
  521. return $block;
  522. }
  523. // Only show if we have at least two languages and language dependent
  524. // web addresses, so we can actually link to other language versions.
  525. elseif ($op == 'view' && variable_get('language_count', 1) > 1 && variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) != LANGUAGE_NEGOTIATION_NONE) {
  526. $path = drupal_is_front_page() ? '<front>' : $_GET['q'];
  527. $languages = language_list('enabled');
  528. $links = array();
  529. foreach ($languages[1] as $language) {
  530. $links[$language->language] = array(
  531. 'href' => $path,
  532. 'title' => $language->native,
  533. 'language' => $language,
  534. 'attributes' => array('class' => 'language-link'),
  535. );
  536. }
  537. // Allow modules to provide translations for specific links.
  538. // A translation link may need to point to a different path or use
  539. // a translated link text before going through l(), which will just
  540. // handle the path aliases.
  541. drupal_alter('translation_link', $links, $path);
  542. $block['subject'] = t('Languages');
  543. $block['content'] = theme('links', $links, array());
  544. return $block;
  545. }
  546. }