[Drupal/i18n] Automatic multilingual variables

Data dodania wpisu: 06-05-2011

EDIT: 17.12.2011 - Module is available on drupal.org right now: i18n Localized Variables

 

I got through the multilingual Drupal installation using i18n today. That's easy, right? Yeap, but where's the catch?

 

Primary requirement: multilanguage Drupal with localized configuration.

 

Expected results?

  • custom site name either site mission for each language,
  • localized date formats,
  • individual MollomAPI keys for each language, if based on separate domain,
  • and others... :)

Spare some time and read whole this post before you download a module.

 

Existing solution

Of course there is: Multilingual Variables. And here we've got another catch.

 

Using the solution described on drupal.org we need to insert all variables keys that we need to use as localized by putting them into the array $conf['i18n_variables'] in sites/default/settings.php file. While there is a small multilingual website to manage, there is no such a big problem to use this solution. However, if we've got a big site that has to be fully localizable configured for each enabled language, we would have to insert all necessary variables into this array. This task would be very time consuming:) In addition to this - some webservers Apache owns settings.php file and the only one possible option to edit this file is using a web-ftp client in web-sever user panel instead of local ftp client.

 

Moreover, if we have a dedicated app written on Drupal framework, that serves several own settings pages and has a lot of own system variables, the solution from drupal.org would be a killing task. Example:

DisCart - my Drupal-based online store - which owned a multilanguage version of itself today has a few hundreds of self configuration variables stored in 'variable' table. There is no [...] way to insert each custom variable into the settings.php file. This would kill me:) No [...] way! :D

What's going on? My module let you configure localized variable using Drupal administration site and avoid any files changes:)

Piece of code

2 lines of PHP-code news is a bit expired;) However u can read something about 1st idea.

The conclusion of this post is in 2 (TWO!) lines of PHP-code :) Yeap! The thing to do is to insert these ones into the hook_init() of custom module, to initiate i18n variable system for each configurable variable. That's the solution, to make whole Drupal-based website fully localizable on the configuration layer! :)

 

function MYMODULE_init(){
    global $conf; // global configuration variables table
    $conf['i18n_variables'] = array_keys($conf); // we set localized variables from std variables conf
    // AND THAT'S ALL FOLKS :)
}

 

Nothing cares more, cause each variable from 'variable' table, that Drupal utilizes, will become an internationalized variable that allows to configure itself independently on each enabled language. Each of this will get 'This is a multilingual variable' description on settings page forms. If this variable will not be configured for custom language, the default value that has been configured primarly will take place.

 

Something more again: using a page admin/settings/language/i18n/variables (Site configuration -> Languages -> Multilingual system -> Variables) we can check out which ones of all enabled variables has been utilized as localizable (green mark in 1st col);)

 

Performance

It would be a sin to grumble while using this solution;) On a Drupal-based website with more than 500 variables, memory consumption has increased by about 250kB on page cache refresh and about 50kB per each request while conf cache is ready; loading time increased by 10-20ms;) That's nothing:) To compare this numbers with something real, my online store (there's a link down here to the demonstration store)  loads in 150-300ms and utilizes about 16MB of memory with disabled cache.

 

Concluding all folks:

- download a small module to automatically set all Drupal variables localized: i18n_localized_variables

- take a tour on DisCart demonstration store, example localized variables are site name and currency for English i Polish language - www.dis-cart.designend.net,

- wish you well and successful weekend :)

 

i18n Localized Variables on drupal.org

It's ours first module we contribute on drupal.org, however it's still in sandbox: http://drupal.org/sandbox/designend.net/1149868

 

Edit, 07.05.2011

Module has been recently updated, it supports exclusion of certain modules from localized variables now. For example, Pathauto Automated alias settings is global rather than localized, so we can exclude Pathauto variables from localized variables.

 

Edit, 08.05.2011

I realized that previous settings weren't a good solution. Modules exclusion has been replaced by modules inclusion. In addition, there is another option, to select more granular variables. And that's the solution I wanted to code:)

i18n localized variables does also support some predefined variables. Just use "Restore defaults" button to load them.

Module configuration page has been moved to Site configuration -> Languages -> Multilingual system -> i18n localized variables.

Comments

This is informative article for us Because here you can describe us the right and easy way of Drupal configuration. I definably going to use it Thanks a lot....
i don\'t know about this concept . sorry to comment
sorry to bother you with this, but why isn't this module in drupal.org?
tx
Comments closed...

DesignEnd on Facebook

Inspiration