To be rewritten
This module provides support for internationalization of Drupal sites:
- Translation of the user interface for anonymous users (combined with locale)
- Multi-language for content. Adds a language field for nodes and taxonomy terms
- Basic translation management
- Browser language detection
- Keeps the language setting accross consecutive requests, using URL rewriting
- Provides a block for language selection and two theme functions: i18n_flags and i18n_links
Module developed by freelance.reyero.net
' );
break;
case 'admin/modules#description' :
$output = t('Enables multilingual content. Requires locale module for interface translation' );
break;
}
return $output;
}
/**
* Implementation of hook_settings().
*/
function i18n_settings() {
global $db_prefix_i18n;
// Basic settings
$output .= form_radios(t('Supported languages'), 'i18n_supported_langs' , variable_get('i18n_supported_langs', 'i18n'),
array(
'i18n' => t('Defined in the configuration file ($i18n_languages)'),
'locale' => t('Defined by the locale module')
), t('Where to get the list of supported languages from' ));
/* Disabled
$output .= form_radios(t('Simple content translation"), "i18n_content", variable_get("i18n_content", 0), array(t("Disabled"), t("Enabled")), t(' If enabled, prepends language code to url and searches for translated content. Requires url aliasing with 'path' module."));
*/
$output .= form_radios(t('Browser language detection'), 'i18n_browser', variable_get('i18n_browser', 0), array(t('Disabled'), t('Enabled' )));
$output .= form_select(t('Front page'), 'i18n_frontpage', variable_get('i18n_frontpage', 0), array(t('Default'), t('Language dependent')), t(" If 'language dependent' is selected, default front page will be prepended with language code, i.e. 'en/node'"));
$output .= form_textfield(t('Language icons html tag'), 'i18n_flags', variable_get('i18n_flags', ''), 70, 180,
t('HTML tag for flags. Asterisk \'*\' is a placeholder for language code. It should be something like <img class="i18n-flag" src="modules/i18n/flags/*.png" width="16" height="12"/>'));
$output .= t('Multilingual content
' );
foreach (node_list() as $node) {
$subform.=form_checkbox(t($node), 'i18n_node_'.$node, 1, variable_get('i18n_node_'.$node, 0));
}
$output .= form_group(t('Nodes' ), $subform, t('Select node types to be translated.' ));
$output .= t('Advanced Features
' );
$output .= t('These are intended only for advanced users. Some changes to the database are required. Please, do read the INSTALL.txt and README.txt files before enabling these options
' );
// Advanced features
// Language dependent tables
if (is_array($db_prefix_i18n)) {
$multi=true;
$text = ''.t('Current language dependent tables are: '). implode(', ', array_keys($db_prefix_i18n)).'' ;
} else {
$text = t("Check the module's SETUP.txt file.");
}
$output .= form_radios(t('Language dependent tables'), 'i18n_multi', variable_get('i18n_multi', 0), array(t('Disabled'), t('Enabled')), t('If enabled, different tables for each language will be used. They must be defined in the configuration file.') . ' ' . $text);
return $output;
}
/**
* Implementation of hook_menu().
*/
function i18n_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'translation',
'title' => t('translation'),
'callback' => 'i18n_translation_page',
'access' => user_access('administer nodes'),
'type' => MENU_CALLBACK);
/* Disabled term translations
$items[] = array(
'path' => 'admin/taxonomy/i18n',
'title' => t('translation'),
'callback' => 'i18n_taxonomy_admin',
'access' => user_access('administer taxonomy'),
'type' => MENU_LOCAL_TASK);
*/
}
else {
if (arg(0) == 'node' && is_numeric(arg(1)) && variable_get('i18n_node_'.i18n_get_node_type(arg(1)), 0)) {
$access = user_access('administer nodes');
$type = MENU_LOCAL_TASK;
$items[] = array(
'path' => 'node/'. arg(1) .'/translation',
'title' => t('translation'),
'callback' => 'i18n_node_translation',
'access' => $access,
'type' => $type);
}
}
return $items;
} // End of i18n_menu
// translation/node/id/lang
// translation/term/id
function i18n_translation_page() {
$op = $_POST['op'] ? $_POST['op'] : arg(1);
$edit = $_POST['edit'];
switch ($op) {
case 'node':
print theme('page', i18n_translation_add(arg(2), arg(3)));
break;
case t('Preview'):
$edit = node_validate($edit);
print theme('page', node_preview($edit), t('Preview'));
break;
case t('Submit'):
drupal_set_title(t('Submit'));
print theme('page', node_submit($edit));
break;
case t('Delete'):
print theme('page', node_delete($edit), t('Delete'));
break;
default:
// print theme('page', node_page_default(), '');
}
}
function i18n_translation_add($nid, $lang) {
$type = i18n_get_node_type($nid);
return node_add($type);
}
/**
* Implementation of hook_link().
*/
function i18n_link($type, $node = NULL, $teaser = FALSE) {
$links = array();
if ($type == 'node' && !$teaser && variable_get('i18n_node_'.$node->type, 0)) {
$languages = i18n_supported_languages();
$translations = i18n_node_get_translations($node->nid);
foreach ($translations as $lang => $trnode) {
$links[]= theme('i18n_link_name_flag', $lang, 'node/'. $trnode->nid);
}
}
return $links;
}
function i18n_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('Languages');
}
else {
$blocks['subject'] = t('Languages');
$content = '' ;
$content .= theme('i18n_links', 1, 1, ' | ', ' |
' );
$content .= ' |
' ;
$blocks['content'] =$content;
}
return $blocks;
}
/**
* i18n api
*/
/**
* Gets language, checking in order:
*
* 1. Path language
* 2. User language
* 3. Browser language
* 4. Default language
*/
function _i18n_get_lang() {
global $user, $i18n_langpath;
static $i18n_lang;
//see if the language is already set.
if ($i18n_lang) {
return $i18n_lang;
}
$languages = i18n_supported_languages();
if ($i18n_langpath && array_key_exists($i18n_langpath,$languages)) {
$i18n_lang = $i18n_langpath;
}
elseif ($user->uid && $user->language && array_key_exists($user->language,$languages)) {
$i18n_lang = $user->language;
}
elseif (variable_get("i18n_browser",0) && $lang=i18n_get_browser_lang()) {
$i18n_lang=$lang;
}
else {
$i18n_lang=key($languages);
}
return $i18n_lang;
}
/**
* Get list of supported languages
*/
function i18n_supported_languages() {
global $i18n_languages;
static $languages;
if ($languages) {
return $languages;
}
elseif (variable_get('i18n_supported_langs', 'i18n') == 'locale') {
$languages = i18n_locale_supported_languages();
return $languages;
}
elseif (is_array($i18n_languages)) {
return $languages = $i18n_languages;
}
else {
return array();
}
}
/**
* Returns default language
*/
function i18n_default_language(){
return key(i18n_supported_languages());
}
/**
* Returns list of enabled languages from locale module
*
* * Some code borrowed from locale module
*/
function i18n_locale_supported_languages() {
$enabled = array();
$result = db_query('SELECT locale, name FROM {locales_meta} WHERE enabled = 1 ORDER BY isdefault DESC, name ASC');
while ($row = db_fetch_object($result)) {
$enabled[$row->locale] = $row->name;
}
return $enabled;
}
/**
* To get the original path.
* Cannot use $_GET["q"] cause it may have been already changed
*/
function i18n_get_original_path() {
return isset($_REQUEST["q"]) ? trim($_REQUEST["q"],"/") : '';
}
// Get language from browser settings, but only if it is in the $i18n_languages array
function i18n_get_browser_lang() {
$languages = i18n_supported_languages();
$accept=explode(',',array_shift( explode(";",$_SERVER["HTTP_ACCEPT_LANGUAGE"])));
foreach ($accept as $lang) {
$lang=substr($lang,0,2);
if ( !empty($lang) && array_key_exists($lang,$languages)) {
return $lang;
}
}
}
/**
* @name Theme functions
* @{
*/
/**
* Returns language links with optional flags
*
* @param $flags an integer, 1 to use language flags
* @param $names an integer, 1 to use language names
* @param $delim1 delimiter to place between language name and flag
* @param $delim2 delimiter to place between different languages
*
* @return a string containing the @a links output.
*/
function theme_i18n_links($flags = 1, $names = 1, $delim1 = ' ' , $delim2 = ' ' ) {
$i18n_lang = i18n_get_lang();
$languages = i18n_supported_languages();
foreach ($languages as $lang => $langname) {
$name = $names ? t($langname): '' ; // Should be localized??
$flag= $flags ? i18n_flag($lang) : '' ;
if ($lang == $i18n_lang) {
$links[] = "$name$delim1$flag";
}
else {
$links[] = i18n_l($name, $lang).$delim1.i18n_l($flag, $lang);
}
}
$output =implode($delim2, $links);
return $output;
}
function theme_i18n_flags() {
return theme_i18n_links(1, 0);
}
function theme_i18n_link_name_flag($lang, $path, $attributes = array()) {
static $languages;
if (!isset($languages)) {
$languages = i18n_supported_languages();
}
return ''. l(t($languages[$lang]), $path, $attributes) . ' ' . l(i18n_flag($lang) , $path, $attributes) .'';
}
/*
* Creates links for different languages
*/
function i18n_l($text, $lang , $url = '' , $attributes = array(), $query = NULL) {
global $i18n_langpath;
// If !url get from original request
if (!$url) {
$url = i18n_get_original_path();
}
// If url has lang_prefix, remove it
if (i18n_get_lang_prefix($url)) {
$url = substr($url, 3);
}
// $result= l($text, $url, $attributes, $query);
return ''. $text .'';
}
function i18n_url($url, $lang, $query = NULL) {
if ($url) {
return url($lang.'/'.$url, $query);
} else {
return url($lang,$query);
}
}
function i18n_flag($lang, $attribs = array()) {
if ($path = variable_get('i18n_flags', '')) {
$flag = str_replace('*' , $lang, $path);
if(!empty($attribs)){
$flag = str_replace('<','< '.drupal_attributes($attribs).' ',$flag);
}
return $flag;
}
}
/* @} */
/**
* Manage language dependent variables
*
* Requires a patch in bootstrap.inc
*/
function i18n_variable($name) {
global $i18n_variables;
if (is_array($i18n_variables) and in_array($name, $i18n_variables)) {
return i18n_get_lang().'_'.$name ;
} else {
return $name;
}
}
/**
* Get language code from path.
*
* It doesn't check anymore for valid languages, so any two letter code at the beginning of the path will be taken as a lang code
*/
function i18n_get_lang_prefix($path) {
if (preg_match("/^\w\w($|\/.*)/", $path)) {
return substr($path, 0, 2);
}
}
/**
* Sets db_prefix to given language
*/
function i18n_set_db_prefix($lang) {
global $db_prefix, $db_prefix_i18n;
if (is_array($db_prefix_i18n)) {
$db_prefix = array_merge($db_prefix, str_replace('**', $lang, $db_prefix_i18n));
}
}
/**
* Language dependent front page
*/
function i18n_frontpage() {
$path = _i18n_get_lang().'/'.variable_get('site_frontpage','node');
return i18n_get_normal_path($path);
}
/**
* This function is similar to drupal_get_normal_path, but language-aware
* Also removes language from path
*/
function i18n_get_normal_path($path) {
// First, check alias with lang
if (($map = drupal_get_path_map()) && isset($map[$path])) {
return $map[$path];
} elseif (i18n_get_lang_prefix($path)) {
// Check alias without lang
$path = trim(substr($path,3),'/');
if( isset($map[$path])) {
return $map[$path];
}
}
// We only get here when no alias is defined, with or without lang
return $path;
}
?>