Product SiteDocumentation Site

4.2.6. Language and Localization

MantisBT has a very advanced set of localization tools, which allow all parts of of the application to be localized to the user's preferred language. This feature has been extended for use by plugins as well, so that a plugin can be localized in much the same method as used for the core system. Localizing a plugin involves creating a language file for each localization available, and using a special API call to retrieve the appropriate string for the user's language.
All language files for plugins follow the same format used in the core of MantisBT, should be placed in the plugin's lang/ directory, and named the same as the core language files. Strings specific to the plugin should be "namespaced" in a way that will minimize any risk of collision. Translating the plugin to other languages already supported by MantisBT is then as simple as creating a new strings file with the localized content; the MantisBT core will find and use the new language strings automatically.
We'll use the pages from the previous examples, and dress them up with localized language strings. First we need to create a language file for English, MantisBT's default language which is also used as fallback in case some strings have not been localized to the user's language:
Example/lang/strings_english.txt

<?php
$s_plugin_Example_title = 'Example Plugin';
$s_plugin_Example_description = 'Example Plugin from MantisBT Developers Guide';
$s_plugin_Example_configuration = 'Configuration';
$s_plugin_Example_foo_or_bar = 'Foo or Bar?';
$s_plugin_Example_reset = 'Reset Value';
We can now use these strings to localize our code using the plugin_lang_get() API function, replacing the hardcoded text and adding page title:
Example/Example.php

	function register() {
		...
		$this->description = plugin_lang_get( 'title' );
Example/pages/foo.php
				
<?php
layout_page_header( plugin_lang_get( 'title' ) );
layout_page_begin();
?>
...

Example/pages/config.php

<?php
layout_page_header( plugin_lang_get( 'title' ) );
layout_page_begin();

$t_foo_or_bar = plugin_config_get( 'foo_or_bar' );
?>

<form action="<?php echo plugin_page( 'config_update' )?>" method="post">
    <?php echo form_security_field( 'plugin_Example_config_update' ) ?>
    <label>
        <?php echo plugin_lang_get( 'foo_or_bar' ) ?>
        <input name="foo_or_bar" value="<?php echo string_attribute( $t_foo_or_bar ) ?>" />
    </label>
    <br>
    <label>
        <?php echo plugin_lang_get( 'reset' ) ?>
        <input type="checkbox" name="reset" />
    </label>
    <br>
    <button type="submit"><?php echo lang_get( 'change_configuration' )?></button>
</form>