User Tools

Site Tools


plugins:i18n

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
plugins:i18n [2011/03/16 18:22]
mvlcek
plugins:i18n [2016/06/30 19:29] (current)
mvlcek [Multi-language Front-end]
Line 3: Line 3:
 If you display texts in your plugin, the easiest way is hardcode the texts in your plugin, e.g.: If you display texts in your plugin, the easiest way is hardcode the texts in your plugin, e.g.:
  
-<​code>​+<​code ​php>
 <input type="​submit"​ name="​save"​ value="​Save Settings"/>​ <input type="​submit"​ name="​save"​ value="​Save Settings"/>​
 </​code>​ </​code>​
Line 11: Line 11:
 The easiest way is to reuse the translations of GetSimple itself. They can be found in the directory ''/​path_to_getsimple/​admin/​lang''​ and downloaded from [[http://​get-simple.info/​extend/​|Extend]]. In our example you could use the key BTN_SAVESETTINGS:​ The easiest way is to reuse the translations of GetSimple itself. They can be found in the directory ''/​path_to_getsimple/​admin/​lang''​ and downloaded from [[http://​get-simple.info/​extend/​|Extend]]. In our example you could use the key BTN_SAVESETTINGS:​
  
-<​code>​+<​code ​php>
 <input type="​submit"​ name="​save"​ value="<?​php i18n("​BTN_SAVESETTINGS"​);​ ?>"/>​ <input type="​submit"​ name="​save"​ value="<?​php i18n("​BTN_SAVESETTINGS"​);​ ?>"/>​
 </​code>​ </​code>​
Line 17: Line 17:
 If your plugin also supports GetSimple 2.03, you would rather have to use: If your plugin also supports GetSimple 2.03, you would rather have to use:
  
-<​code>​+<​code ​php>
 <?php global $i18n; ?> <?php global $i18n; ?>
 <input type="​submit"​ name="​save"​ value="<?​php $i18n["​BTN_SAVESETTINGS"​];​ ?>"/>​ <input type="​submit"​ name="​save"​ value="<?​php $i18n["​BTN_SAVESETTINGS"​];​ ?>"/>​
 </​code>​ </​code>​
 +
 +A (not necessarily up-to-date) list of the built in i18n hashes can be found [[i18nhashes|on this page]]. ​ Always check the source for the latest.
  
 If you need additional texts not found in the standard GetSimple translation files, you need to create translation files yourself. They should look like the GetSimple translation files, e.g. de_DE.php: If you need additional texts not found in the standard GetSimple translation files, you need to create translation files yourself. They should look like the GetSimple translation files, e.g. de_DE.php:
  
-<​code>​+<​code ​php>
 $i18n = array( $i18n = array(
-  '​BTN_SAVEMYSETTINGS'​ = 'Meine Einstellungen speichern',​ +  '​BTN_SAVEMYSETTINGS'​ ='Meine Einstellungen speichern',​ 
-  '​MYHEADER'​ = 'Meine Überschrift'​+  '​MYHEADER'​ ='Meine Überschrift'​
 ); );
 </​code>​ </​code>​
Line 46: Line 48:
 GetSimple 3.0 makes it easy to load these texts in your plugin, e.g.: GetSimple 3.0 makes it easy to load these texts in your plugin, e.g.:
  
-<​code>​+<​code ​php>
 i18n_merge('​my_plugin'​) || i18n_merge('​my_plugin','​en_US'​);​ i18n_merge('​my_plugin'​) || i18n_merge('​my_plugin','​en_US'​);​
 </​code>​ </​code>​
Line 54: Line 56:
 You then access them in the same way as standard GetSimple texts, but add your plugin name as prefix to the key, e.g. You then access them in the same way as standard GetSimple texts, but add your plugin name as prefix to the key, e.g.
  
-<​code>​+<​code ​php>
 <input type="​submit"​ name="​save"​ value="<?​php i18n("​my_plugin/​BTN_SAVEMYSETTINGS"​);​ ?>"/>​ <input type="​submit"​ name="​save"​ value="<?​php i18n("​my_plugin/​BTN_SAVEMYSETTINGS"​);​ ?>"/>​
 +</​code>​
 +
 +Note that //i18n()// echos the translation. ​ If you need to use the translated text in php code you will have to use //​i18n_r()//​ to return the string rather than echoing it, e.g.
 +
 +<code php>
 +$msg = '<​div style="​display:​ block;"​ class="​updated">'​ . i18n_r("​my_plugin/​MSG_UPDATED"​) . '​.</​div>';​
 +echo $msg;
 </​code>​ </​code>​
  
Line 63: Line 72:
 ===== Language files without country ===== ===== Language files without country =====
  
-If you want to provide language files en.php, de.php, etc. (without country code), ​than use the following lines to load your texts:+If you want to provide language files en.php, de.php, etc. (without country code), ​then use the following lines to load your texts:
  
-<​code>​+<​code ​php>
 global $LANG; global $LANG;
 i18n_merge('​my_plugin',​ substr($LANG,​0,​2)) || i18n_merge('​my_plugin','​en'​);​ i18n_merge('​my_plugin',​ substr($LANG,​0,​2)) || i18n_merge('​my_plugin','​en'​);​
Line 74: Line 83:
 If your plugin has to display texts on the front-end (the internet site itself) and you just support typical GetSimple installations,​ where the content is displayed in one language and the admin back-end is set to this language, you do it the same way as described above. If your plugin has to display texts on the front-end (the internet site itself) and you just support typical GetSimple installations,​ where the content is displayed in one language and the admin back-end is set to this language, you do it the same way as described above.
  
-The only difference is that you must provide language support, because where it might be acceptable for administrators and editors to see some information in english instead of their own language, it is unacceptable on the front-end. Without language support you basically limit your plugin to use in countries with the same language as your's.+The only difference is that you must provide language support, because where it might be acceptable for administrators and editors to see some information in english instead of their own language, it is unacceptable on the front-end. Without language support you basically limit your plugin to use in countries with the same language as yours. 
 + 
 +**GetSimple 3.0:** 
 + 
 +In the frontend the variable $LANG is always en_US (due to the multi-user capabilities introduced with 3.0). The user should be encouraged to enter a line  
 +<​code>​ 
 +$LANG = 'de_DE';​ 
 +</​code>​ 
 +or similar into gsconfig.php. 
 +As an alternative the plugin can offer a functionality to set the frontend language.
  
 ===== Multi-language Front-end ===== ===== Multi-language Front-end =====
Line 82: Line 100:
 As multi-language sites are not supported natively by GetSimple, there currently exists one plugin, [[http://​get-simple.info/​extend/​plugin/​i18n/​69/​|I18N]]. As multi-language sites are not supported natively by GetSimple, there currently exists one plugin, [[http://​get-simple.info/​extend/​plugin/​i18n/​69/​|I18N]].
  
-If your plugin should support multi-language sites using the I18N plugin, load the texts with+If your plugin should support multi-language sites using the I18N plugin, ​you must load the texts **only after the I18N initialization** (in the pretemplate hook) - in your function(s) that output text on the front end, with
  
-<​code>​+<​code ​php>
 global $LANG; global $LANG;
 if (function_exists('​i18n_load_texts'​)) { if (function_exists('​i18n_load_texts'​)) {
Line 91: Line 109:
   i18n_merge('​my_plugin',​ substr($LANG,​0,​2)) || i18n_merge('​my_plugin',​ '​en'​);​   i18n_merge('​my_plugin',​ substr($LANG,​0,​2)) || i18n_merge('​my_plugin',​ '​en'​);​
 } }
 +</​code>​
 +
 +If you need back end template texts directly when loading the plugin, you need to check, if the plugin is loaded in the backend:
 +
 +<code php>
 +if (basename($_SERVER['​PHP_SELF'​]) != '​index.php'​) { // back end only - or do you know a better condition?
 +  i18n_merge('​my_plugin',​ substr($LANG,​0,​2)) || i18n_merge('​my_plugin',​ '​en'​);​
 +}
 +add_action('​plugins-sidebar',​ '​createSideMenu',​ array($thisfile,​ i18n_r('​my_plugin/​CONFIGURE'​))); ​
 </​code>​ </​code>​
  
 This code will work (with GetSimple 3.0+), whether the I18N plugin is present or not. This code will work (with GetSimple 3.0+), whether the I18N plugin is present or not.
  
-Note: this function ​of the I18N plugin ​is not yet available, but will be shortly.+Note: the function ​i18n_load_texts is available in the I18N plugin ​1.0+.
  
 Note: The I18N plugin currently only supports language codes without country, so you must have language files like ''​en.php'',​ ''​de.php''​ etc. instead of ''​en_US.php'',​ etc. Note: The I18N plugin currently only supports language codes without country, so you must have language files like ''​en.php'',​ ''​de.php''​ etc. instead of ''​en_US.php'',​ etc.
  
 +NOte: The I18N plugin'​s current i18n_load_texts function might not be perfect yet. For a complete guide to using language files in the frontend, check out http://​get-simple.info/​forums/​showthread.php?​tid=1221&​pid=56863#​pid56863
plugins/i18n.1300299767.txt.gz · Last modified: 2013/04/19 14:57 (external edit)