translating in different languages

From: Date: Sun, 04 Mar 2001 20:24:56 +0000
Subject: translating in different languages
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-291@lists.php.net to get a copy of this message
Hi, I'm in the process of adding multi-language support to Smarty. I have written a preliminary design text on this, which I have attached. I also have code to implement the described functionality, but I haven't attached it yet as I want to refine it a bit first before releasing. I was hoping someone would have some time to read this text, and give some thought/remarks about it. I think it is configurable enough for pretty much every situation, but if you think it can be made more generic, let me know. I hope to be ready with the rest of the code tomorrow or the day after, I just wanted to get this text out to get some feedback on it. cheers, roel

Translation capabilities for Smarty - overview ---------------------------------------------- Roel Vanhout - roel@2e-systems.com Revision history: -1st draft (20010204) 1. Abstract ----------- This document describes the way the Smarty template engine handles translation of texts in different languages. It describes the issues involved from the viewpoints of template developers, programmers, translators and a description of how this is implemented in Smarty itself. 2. General overview ------------------- The texts can be stored in either text files, or in a database. Where the texts are read from is described by the configuration variable $text_method. (eg $smarty->text_method). Using translation support is turned on by setting the 'translate' configuration variable to 'true'. If this is 'false' (the default), Smarty works exactly as it would without translation support. If it is turned on, a slightly different caching directory structure is used: for every language, a subdirectory is made with the two-letter code of that language (eg. templates_c/en). In this directory, the 'compiled' version of the script is stored with all translatable texts in that language. 3. Template writers ------------------- All translators have to do, is use the {translate string} tag. This way, the template is easy to read (no need for cryptic unique tags for every block of text you want to use). All the rest is handled tranparently. 4. Translators -------------- The language files are located in a directory called 'translations'. The extension of the file is the two-letter code of the language (eg. texts.pl for Polish). 5. Developers ------------- The language you want to use for the Smarty object you are processing is set in a property called 'language'. ($smarty->language). So in your code, you have to set this value to the language you want your page to be in: $smarty = Smarty; $smarty->language = 'nl'; Translation is turned off by default; make sure to set it to true in Smarty.class.php. 5. Implementation ----------------- 'translate' is just another tag. TODO: write more here 6. Storing translations ----------------------- To allow maximum flexibility, there are two ways to store translated texts: in a database, or in plain text files. Where the translations are read from is set with the $text_method variable (TODO: better name for this variable?) To allow multiple databases to be used, we provide a small abstraction layer. This can be very small and simple as we only need a very limited subset of database functionality. Currently only a mysql 'driver' is provided, but it should be trivial to write others for other databases. The database layout: table translations ( textnr int; lang varchar(2); translation blob; translation_of int; module varchar(3); date_entered date; date_changed date; ) (module is for future enhancements to Smarty, not used now but planned) TODO: check that these data types are universal, these are all the mysql names. Maybe write a .sql file for every database? The format of text based files is the same, one line for every entry, seperated by '||' (without the quotes). TODO: is this the best way? Maybe use comma-delimited but put text in quotes? Would be harder to parse. Every entry in the table will get a unique number, but this number is never seen by translators, programmers or template writers. There is one entry for every sentence that template writers put in their templates. These lines all have textnr 0. Then, for every translated sentence, there is an indicator that says of which text this is a translation. For example: 1 || en || Welcome to this site! || 0 || * || 2001-03-04 || 2001-03-04 2 || nl || Welkom op deze site! || 1 || * || 2001-03-04 || 2001-03-04 3 || fr || Bienvenu sur cette site! || 1 || * || 2001-03-04 || 2001-03-04 4 || de || Willkomm auf diese site! || 1 || * || 2001-03-04 || 2001-03-04 Note: the french and german translations are probably wrong, but you see my point :-) Now, when we ask for a translation of 'Welcome to this site!' to Dutch, we first do a SELECT textnr FROM translations WHERE translation = '$text'; Then, we do SELECT translation FROM translations WHERE translation_of='$translationnr' AND lang='$lang' The result is the translated string. (This can probably be done in one sql statement, but for clarity I split it up.) It is the responsability of the template writer to update the references when the text in the template is changed. This can be automated with a short php or perl script. Maybe even a program that makes entries for every sentence in a template (see point 8). The easiest and most performant way to do this with a text file is probably by reading the whole file into a hash and then extracting the data from that hash. TODO: is there need for an xml based storage format? The advantage of normal text files is that they can easily be edited with a normal texteditor, even live on a server. xml can be edited too, but normal text files are easier to read. xml based would be more work, but probably it's nicer to write tools to edit these files. Need some discussion on this. 7. Database abstraction spec ---------------------------- TODO: where do Every database abstraction file has to provide the following functions: - get_translation_from_database($text, $lang) -> returns the translated string for this text in the required language. For now, the connection configuration (host, user, password,...) are stored in the database 'driver' file, because they can vary between databases. Maybe if there is a way to make this more generic, this can be moved to one place. The naming convention is 'db_function_<dbname>.php'. 8. Tools -------- A nice web-based tool to translate strings would be cool. Also, everyone who want to write other tools (perl/gtk or (shudder) vb come to mind for quick and dirty small apps). The webbased tool should best be implemented as a smarty template and a file that does the 'real' work, so that the tool can easily be customized. Another tool would be to make database entries for every string in a template. 9. TODO ------- * Make default language (the language used in the templates) configurable.
« previous php.smarty.general (#291) next »