Toolserver:TSMessages.php
This page was moved from the Toolserver wiki.
Toolserver has been replaced by Toolforge. As such, the instructions here may no longer work, but may still be of historical interest.
Please help by updating examples, links, template links, etc. If a page is still relevant, move it to a normal title and leave a redirect.
TSMessages.php
ALPHA is a script to get messages from translatewiki.net or local i18n files.
class TSLanguageSelector
[edit]TSLanguageSelector([string $default [, string $varname [, string $cookiePrefix [, string $cookiePath [, string $cookieDomain ]]]]])
- $default is the default language of your script. Default:"en"
- $varname is the name of the GET key which bear the user selected language. Default:"lang"
- $cookiePrefix is the prefix of the cookie name. The cookie name is conglomerated with $cookiePrefix and $varname. Default:""
- $cookiePath is the path which is added to the cookie. Default:NULL
- $cookieDomain is the domain which is added to the cookie. Default:NULL
return: Language Object. This class should be called before any output because it sets the language cookie.
getSelectorFormHTML
[edit]getSelectorFormHTML(object $messages[, boolean $auto_submit[, string $name ]])
- $messages is the object from createDefaultMessageObject↓
- $auto_submit defines whether or not the language field is submitted automatically when the user chooses a language. It works only with Javascript enabled, otherwise the user sees an "ok" button. Default: true
- $name is the name and the id of the form field. Default: "languageselector"
Return: HTML form to select a language.
getSelectorListHTML
[edit]getSelectorListHTML(object $messages)
- $messages is the object from createDefaultMessageObject↓
Return: HTML list of languages to choose from, like zhis:
de | en | fr | gsw |...
class TSMessagesFactory
[edit]TSMessagesFactory( [string $file [, resource $connection [, string $prefix [, boolean $multilang [, string $memcachedserver [, int $memcachedport ]]]]]] )
- $file: the path to the folder if you have messages as i18n.php files. Default: NULL
- $connection: mysql resource. If you don't have already a connection you can set it to NULL, then the script parse your .my.cnf and self-connect. If you need the connection furthermore in your tool you can get it with db_connect ↓. Default: NULL
- $prefix: If your message keys starts all with the same prefix, you can set it there, then you can deliver the keys later without the prefix. Please beware that this prefix is used for all message keys! Default: NULL
- $multilang: set it to false if you have for every language a own file. if you have all messages in one file set it to true. Default: true
- $memcachedserver: memcached server address. Default: localhost
- $memcachedport: memcached server port. Default: 11211
Return: Object to get the messages.
db_connect
[edit]db_connect()
This is a static function!
Return: mysql connection resource.
memcached_connect
[edit]memcached_connect()
This is a static function!
Return: memcached object.
createDefaultMessageObject
[edit]createDefaultMessageObject( mixed $tool [, mixed $lang])
- $tool string or array with the needed tools. There are different variants:
Array #
( # include messages from the tools Mediawiki, Toolserver and FlaggedRevs.
[0] => "Mediawiki" # to avoid db overload it's limited to 200 messages, so if you request
[1] => "Toolserver" # mediawiki you have only the first 200 of 2500 messages.
[2] => "FlaggedRevs" #
["TidyTab"] => NULL # You can also put the name in the key, same result.
) #
Array #
( # If you put the tool name in the key and add a
["Mediawiki"] => Array # Array with message keys, only the needed messages are
( # loaded (200 limit ignored). This is the best way
[0] => "noarticletext" # if you have messages from big tools like mediawiki;
[1] => "skin" # for tools with only a few messages you can
[2] => "userrights" # include all messages.
[3] => "right-bot" #
[4] => "right-hideuser" #
) #
["Toolserver"] => Array #
( #
[0] => "message1" #
[1] => "message2" #
[2] => "message3" #
[3] => "message4" #
[4] => "message5" #
) #
)
Array #
( #
["Mediawiki"] => Array # You can also mix this variants, but
( # every tool should only one time listed.
[0] => "noarticletext" #
[1] => "skin" #
[2] => "userrights" #
[3] => "right-bot" #
[4] => "right-hideuser" #
) #
[2] => "FlaggedRevs" #
["TidyTab"] => NULL #
)
- $lang: string with the shortcut of the needed language or TSLanguageSelector↑ object. If a message is not available in the required language it fall back to English (en).
Return: TSMessages object.
class TSMessages
[edit]returned by createDefaultMessageObject↑
getMessage
[edit]getMessage(string $key [,array $replacers [, boolean $parse ]])
- $key: key of the message you request
- $replacers: If the message contains placeholders like $1, $2 etc you can add a array with the content to replace them: default: array()
Array #
( #
[0] => "text 1" # replaced with $1
[1] => "text 2" # with $2
[2] => "text 3" # with $3
) # etc...
- $parse: if you don't like to parse the text, set it to false. default: true
Parsed will be:
- $1, $2, $3 etc. replaced with declared text
- {{:Toolserver:Template:{{subst:uc:P}}LURAL:$1|there is a message|there are $1 messages}} --> dependent on $1 "there is a message" or "there are $1 messages"
- '''Bold''' --> <b>Bold</b>
- ''Italic'' --> <i>Italic</i>
Return: message as string
getMessageEscaped
[edit]getMessageEscaped (string $key [,array $replacers [, boolean $parse ]])
same output like getMessage↑ but escaped with htmlspecialchars.
getKeys
[edit]getKeys()
get all keys which are loaded in this object.
Return: array with loaded keys.
getAvailableLanguages
[edit]getAvailableLanguages()
return a array with all languages your tool is translated. for that it takes the first tool who is listed at createDefaultMessageObject $tool and checks it, because of that the first element in the array should be the tool who is translated at least. For files the whole directory is checked.
form:
Array
(
["en"] => "English"
["de"] => "Deutsch"
["bm"] => "Bamanankan"
)
Example Tool
[edit]<?php
include("TSMessages.php");
$langClass = new TSLanguageSelector("en","lang","bsptool","/~hugo/"); #open language class, set language cookie
$msgClass = new TSMessagesFactory(); #open messages class
$neededmessages =
array( #messages from mediawiki and commonist needed
"mediawiki" => array(
"Category_header",
"Hidden-categories",
"Otherlanguages"
),
"Commonist"
);
$messages = $msgClass->createDefaultMessageObject($messagesarray,$langClass); #get messages
print $messages->getMessage("Otherlanguages")."\n"; #In other languages
print $langClass->getSelectorFormHTML($messages)."<br />"; #selector list
?>
<!-- your tool etc-->
<?php
$random = rand(1,2);
print $messages->getMessage("Hidden-categories",array($random))."<br />\n"; #{{PLURAL:$1|Hidden category|Hidden categories}}
#random: plural or not?
print $messages->getMessage("image.upload")."\n"; #message from Commonist tool
?>