Archived

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

From Joomla! Documentation

 
(16 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.
  
  
 
<source lang="xml"><?xml version="1.0" encoding="utf-8"?>
 
<source lang="xml"><?xml version="1.0" encoding="utf-8"?>
 
<extension type="file" version="2.5" method="upgrade">
 
<extension type="file" version="2.5" method="upgrade">
<name>lang_extension_name_fr-FR</name>
+
 
 +
<!-- 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>
 
<author>French Translation Team</author>
 
<creationDate>24 Janvier 2012</creationDate>
 
<creationDate>24 Janvier 2012</creationDate>
Line 18: Line 19:
 
<authorUrl>http://www.myextension.org</authorUrl>
 
<authorUrl>http://www.myextension.org</authorUrl>
 
<version>1.0.0</version>
 
<version>1.0.0</version>
<description>MYEXTENSION_LANG_INSTALL_DESCRIPTION</description>
+
<description>This fr-FR lang pack for myextension has been installed successfully</description>
  
 
<!-- Fileset definition -->
 
<!-- Fileset definition -->
 
<fileset>
 
<fileset>
<files folder="admin/fr-FR" target="administrator/language/fr-FR">  <!-- back-end -->
+
 
 +
<!-- back-end -->
 +
<files folder="admin/fr-FR" target="administrator/language/fr-FR">
 
<filename>fr-FR.com_myextension.ini</filename>
 
<filename>fr-FR.com_myextension.ini</filename>
 
<filename>fr-FR.com_myextension.sys.ini</filename>
 
<filename>fr-FR.com_myextension.sys.ini</filename>
Line 28: Line 31:
 
<filename>fr-FR.plg_system_myextension.sys.ini</filename>
 
<filename>fr-FR.plg_system_myextension.sys.ini</filename>
 
</files>
 
</files>
<files folder="site/fr-FR" target="language/fr-FR">  <!-- front-end -->
+
 
 +
<!-- front-end -->
 +
<files folder="site/fr-FR" target="language/fr-FR">
 
<filename>fr-FR.com_myextension.ini</filename>
 
<filename>fr-FR.com_myextension.ini</filename>
 
<filename>fr-FR.com_myextension.sys.ini</filename>
 
<filename>fr-FR.com_myextension.sys.ini</filename>
Line 40: Line 45:
 
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.
 
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.
  
myextension_fr-FR_install.xml //name can be anything.
+
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 51: Line 56:
  
 
'''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.'''
 
'''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.'''
[[Category:Joomla! 1.6]]
+
 
 +
== 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):
 +
 
 +
<source lang="xml"><?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>
 +
</source>
 +
 
 +
''install.script.php'':
 +
 
 +
<source lang="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 );
 +
}
 +
}
 +
</source>
 +
 
 +
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! 2.5]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
[[Category:Language Development]]
 
[[Category:Language Development]]

Latest revision as of 17:25, 26 April 2022

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

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[edit]

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.