Actions

J2.5

Difference between revisions of "Making non-core language packs"

From Joomla! Documentation

m (Make it multi-language)
m (bolding link)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
To make a non-core language pack in 2.5 is quite different from making a Core pack.
+
{{Version/tutor|2.5}}
 
+
To make a non-core language pack in 2.5 is quite different from '''[[J2.5:Making_a_Language_Pack_for_Joomla|making a Core pack]]'''.
http://docs.joomla.org/Making_a_Language_Pack_for_Joomla_1.6
+
  
 
The type of package is a "file" type.
 
The type of package is a "file" type.
  
Here is a typical xml including installing ini files into the administrator and site CORE default language folders for one language and one extension only. Many languages and many extensions can be included.
+
Here is a typical xml including installing ini files into the administrator and site CORE default language folders for one language and one extension only. Many languages and many extensions can be included in the same .zip.
  
  
Line 48: Line 47:
 
lang_extensionname_fr-FR.xml
 
lang_extensionname_fr-FR.xml
  
site/fr-FR/ + all ini files
+
site/fr-FR/ + all ini files + index.html
  
admin/fr-FR/ + all ini files
+
admin/fr-FR/ + all ini files + index.html
  
 
The result is the installation of the ini files in an existing fr-FR folder or creating one if does not exist.
 
The result is the installation of the ini files in an existing fr-FR folder or creating one if does not exist.
Line 100: Line 99:
 
jimport( 'joomla.installer.installer' );
 
jimport( 'joomla.installer.installer' );
  
 +
// FIXME: remember to change class name
 
class pkg_kunena_languagesInstallerScript {
 
class pkg_kunena_languagesInstallerScript {
 +
 +
// FIXME: remember to change language package name
 +
protected $name = 'com_kunena';
  
 
public function uninstall($parent) {
 
public function uninstall($parent) {
Line 120: Line 123:
 
$source = $parent->getParent()->getPath('source').'/language';
 
$source = $parent->getParent()->getPath('source').'/language';
 
$languages = JFactory::getLanguage()->getKnownLanguages();
 
$languages = JFactory::getLanguage()->getKnownLanguages();
 +
 
$files = $parent->manifest->files;
 
$files = $parent->manifest->files;
 
foreach ($languages as $language) {
 
foreach ($languages as $language) {
 
$search = JFolder::files($source, $language['tag']);
 
$search = JFolder::files($source, $language['tag']);
 
if (empty($search)) continue;
 
if (empty($search)) continue;
 +
 
// Generate something like <file type="file" client="site" id="com_kunena_fi-FI">com_kunena_fi-FI_v2.0.0.zip</file>
 
// Generate something like <file type="file" client="site" id="com_kunena_fi-FI">com_kunena_fi-FI_v2.0.0.zip</file>
 
$file = $files->addChild('file', array_pop($search));
 
$file = $files->addChild('file', array_pop($search));
 
$file->addAttribute('type', 'file');
 
$file->addAttribute('type', 'file');
 
$file->addAttribute('client', 'site');
 
$file->addAttribute('client', 'site');
$file->addAttribute('id', preg_replace('#('.$language['tag'].').*#', '\1', $file););
+
$file->addAttribute('id', $this->name.'_'.$language['tag']);
 
echo sprintf('Installing language %s - %s ...', $language['tag'], $language['name']) . '<br />';
 
echo sprintf('Installing language %s - %s ...', $language['tag'], $language['name']) . '<br />';
 
}
 
}
 +
 
if (empty($files)) {
 
if (empty($files)) {
 
// No packages to install: replace failure message with something that's more descriptive.
 
// No packages to install: replace failure message with something that's more descriptive.
Line 142: Line 148:
 
public function uninstallLanguage($tag, $name) {
 
public function uninstallLanguage($tag, $name) {
 
$table = JTable::getInstance('extension');
 
$table = JTable::getInstance('extension');
$id = $table->find(array('type'=>'file', 'element'=>"com_kunena_{$tag}"));
+
$id = $table->find(array('type'=>'file', 'element'=>"{$this->name}_{$tag}"));
 
if (!$id) return;
 
if (!$id) return;
  
Line 151: Line 157:
 
</source>
 
</source>
  
Then just have all the zip files (or installable directories) under '''language''' folder.
+
Then just copy all the language zip files (or installable directories) under '''language''' folder. Just make sure that they all use the same naming convention before creating the installation pack.
 +
 
 +
The nicest thing in above installer pack is that there's no need to change anything if languages for your extension get added, removed or updated.
  
[[Category:Joomla! 1.6]]
+
[[Category:Joomla! 2.5]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
[[Category:Language Development]]
 
[[Category:Language Development]]

Latest revision as of 09:48, 16 April 2013

To make a non-core language pack in 2.5 is quite different from making a Core pack.

The type of package is a "file" type.

Here is a typical xml including installing ini files into the administrator and site CORE default language folders for one language and one extension only. Many languages and many extensions can be included in the same .zip.


<?xml version="1.0" encoding="utf-8"?>
<extension type="file" version="2.5" method="upgrade">
 
        <!-- Please use a unique name to make uninstall to work. Should be identical to your manifest filename. -->
        <name>lang_extensionname_fr-FR</name>
        <author>French Translation Team</author>
        <creationDate>24 Janvier 2012</creationDate>
        <copyright>(C) 2005-2012 Whoever</copyright>
        <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
        <authorEmail>info@whoever</authorEmail>
        <authorUrl>http://www.myextension.org</authorUrl>
        <version>1.0.0</version>
        <description>This fr-FR lang pack for myextension has been installed successfully</description>
 
        <!-- Fileset definition -->
        <fileset>
 
                <!-- back-end -->
                <files folder="admin/fr-FR" target="administrator/language/fr-FR">
                        <filename>fr-FR.com_myextension.ini</filename>
                        <filename>fr-FR.com_myextension.sys.ini</filename>
                        <filename>fr-FR.plg_system_myextension.ini</filename>
                        <filename>fr-FR.plg_system_myextension.sys.ini</filename>
                </files>
 
                <!-- front-end -->
                <files folder="site/fr-FR" target="language/fr-FR">
                        <filename>fr-FR.com_myextension.ini</filename>
                        <filename>fr-FR.com_myextension.sys.ini</filename>
                        <filename>fr-FR.mod_myextension.ini</filename>
                        <filename>fr-FR.mod_myextension.sys.ini</filename>
                </files>
        </fileset>
</extension>

The .zip in this case is composed of the xml above and 2 folders: site and admin, each of them containing a fr-FR folder with the ini files.

lang_extensionname_fr-FR.xml

site/fr-FR/ + all ini files + index.html

admin/fr-FR/ + all ini files + index.html

The result is the installation of the ini files in an existing fr-FR folder or creating one if does not exist.

It has no impact on existing files. Uninstalling the "File" pack (Extensions Manager=>Manage=>Filter by "file") will only uninstall the ini files listed.

NOTE: In 2.5, except if one uses a specific script, the language xx-XX folders will always be created if they do not already exist.

Make it multi-language

If you want to pack all the available languages into one installable file, but want to be able to use separate file extensions (as explained above) to be able to uninstall or install them separately, a package with a small script is needed.

Following code will only install those languages which exist in your Joomla installation. If new languages are added, package needs to be installed again.

pkg_kunena_languages.xml (in our example):

<?xml version="1.0" encoding="utf-8"?>
<extension type="package" version="2.5" method="upgrade">
 
        <!-- Put anything into here, name is only shown in extension manager. -->
        <name>Kunena Language Pack</name>
 
        <!-- Filename without "pkg_" prefix. Must be unique or uninstall breaks. -->
        <packagename>kunena_languages</packagename>
 
        <version>2.0.0</version>
        <creationDate>2012-05-09</creationDate>
        <author>Kunena Team</author>
        <authorEmail>kunena@kunena.org</authorEmail>
        <authorUrl>http://www.kunena.org</authorUrl>
        <copyright>(C) 2008 - 2012 Kunena Team. All rights reserved.</copyright>
        <license>GNU/GPL</license>
        <description>Language pack for Kunena forum component.</description>
 
        <scriptfile>install.script.php</scriptfile>
        <files folder="language">
                <!-- Content will be added by our installer script -->
        </files>
</extension>

install.script.php:

<?php
defined( '_JEXEC' ) or die();
 
// Needed in Joomla < 2.5:
jimport( 'joomla.filesystem.folder' );
jimport( 'joomla.installer.installer' );
 
// FIXME: remember to change class name
class pkg_kunena_languagesInstallerScript {
 
        // FIXME: remember to change language package name
        protected $name = 'com_kunena';
 
        public function uninstall($parent) {
                // Remove languages.
                $languages = JFactory::getLanguage()->getKnownLanguages();
                foreach ($languages as $language) {
                        echo $this->uninstallLanguage($language['tag'], $language['name']);
                }
        }
 
        public function preflight($type, $parent) {
                if (!in_array($type, array('install', 'update'))) return true;
 
                $app = JFactory::getApplication();
 
                // If you want, you can detect your extension in here and fail with a message if it is not installed.
 
                // Get list of languages to be installed. Only installs languages that are found in your system.
                $source = $parent->getParent()->getPath('source').'/language';
                $languages = JFactory::getLanguage()->getKnownLanguages();
 
                $files = $parent->manifest->files;
                foreach ($languages as $language) {
                        $search = JFolder::files($source, $language['tag']);
                        if (empty($search)) continue;
 
                        // Generate something like <file type="file" client="site" id="com_kunena_fi-FI">com_kunena_fi-FI_v2.0.0.zip</file>
                        $file = $files->addChild('file', array_pop($search));
                        $file->addAttribute('type', 'file');
                        $file->addAttribute('client', 'site');
                        $file->addAttribute('id', $this->name.'_'.$language['tag']);
                        echo sprintf('Installing language %s - %s ...', $language['tag'], $language['name']) . '<br />';
                }
 
                if (empty($files)) {
                        // No packages to install: replace failure message with something that's more descriptive.
                        $app->enqueueMessage(sprintf ( 'Your site is English only. There\'s no need to install language pack.' ), 'notice');
                        return false;
                }
 
                return true;
        }
 
        public function uninstallLanguage($tag, $name) {
                $table = JTable::getInstance('extension');
                $id = $table->find(array('type'=>'file', 'element'=>"{$this->name}_{$tag}"));
                if (!$id) return;
 
                $installer = new JInstaller();
                $installer->uninstall ( 'file', $id );
        }
}

Then just copy all the language zip files (or installable directories) under language folder. Just make sure that they all use the same naming convention before creating the installation pack.

The nicest thing in above installer pack is that there's no need to change anything if languages for your extension get added, removed or updated.