Add language and import translations through profile in Drupal 6
Andreas — Sat, 2009-01-03 01:27
At work were developing sites almost exclusively in Drupal 6, except for our own self developed E-business solution. Most of our site are in swedish, so I've found a way to add swedish and import all the available .PO-files. I do this in our own installation profile.
Drupal has a built-in localization import for profiles, but it overwrites english as the only language and I didn't know how to get english back. When developing; I mostly use english to follow guides on the net and therefore created an import myself.
Before we start
As I mentioned before, the company I work for mostly makes sites in swedish and we put out between 10 and 20 drupal sites a month. Shaving off a minute or two from the manual labor really helps the workflow. If you do a few sites a month or half-year in other languages than english, specially if they're not the same language, this is probably not the way to go.
You should have some knowledge with Drupal and how to make you own installation profile. I will briefly touch Drupal's Batch API.
I used these 4 functions:
- locale_add_language - http://api.drupal.org/api/function/locale_add_language/6
- locale_batch_by_language - http://api.drupal.org/api/function/locale_batch_by_language/6
- batch_set - http://api.drupal.org/api/function/batch_set/6
- batch_process - http://api.drupal.org/api/function/batch_process/6
Check the links out, all parameters, defaults and returns you need are there. I'm not going to explain them.
Dig into the profile
My installation profile I copied from the default profile that came with Drupal. I've made a lot of changes to it but the only thing you need to know to this is to find the function called default_profile_tasks. I put my code near the end of the function, just before menu_rebuild at line 137.
OK, now the code:
<?php
// Just to be sure that the functions exists.
require_once 'includes/locale.inc';
require_once 'includes/form.inc';
// Adding swedish and set as active and default language.
locale_add_language('sv', 'Swedish', 'Svenska', LANGUAGE_LTR, '', 'sv', TRUE, TRUE);
// Create a batch for importing .PO-files and process it.
$batch = locale_batch_by_language('sv');
batch_set($batch);
batch_process();
?>Breaking it down
- locale_add_language
- If you just want to add a locale you only need the set the first parameter in
locale_add_language, but I want to make it the default locale so I need to the set all parameters. The last parameter, a boolean, controls if this locale shall be the default. - locale_batch_by_language
- Returns an array of batch instructions.
- batch_set
- Adds the array to the batch.
- batch_process
- Executes the batch.
This code will be executed in the end of the installation, after you entered your account information, and it gives you a nice progress bar - almost for free (I think).
Post new comment