Re: python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)

From: Date: Mon, 03 Sep 2001 12:18:19 +0000
Subject: Re: python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-64952@lists.php.net to get a copy of this message
Very nice of cause, but i would like to add something first: when handling multilanguage support you will of cause have an array of supported languages like $languages=array("en_GB","de_DE"...) and a default language $def_language="en_GB" thean you may have an array for each language used for the translations $language_en_GB=array(animal=>"cobra",...), $language_de_DE=array(...) or just an array of translation arrays: $translations = array( en_GB => array( "animal=>"cobra",...), de_DE=>array(...),...); so now to translate $text to $translated_text you could write: if ( !in_array($user_language,$languages)) $user_language=$defualt_language; $translated_text = saprintf( $text, $translations[$user_language]); perhaps this is a good addition like (s)tprintf are any other name for translation with the following parameters (s)tprintf( text, dictonary_arrays[, language]) where the language with index 0 is used when language is omitted. second: when handling translatione the MESSAGE should come out of the translation mechanism so one had to use: $text = "%(animal does something)s"; if ($lang == "it") $translations = array( "animal does something"=>.....) else $translations = array("animal does something"=>"the %(color)s %(animal)s is %(action)s",animal=>"cobra",color=>"green",action=>"eating",target =>"mouse"); $translated_text = saprintf( saprintf( $text, $translations), $translations); third: a mechanism for loading translations from files would be very helpfull: we use files that have a name appended with the locale (en_GB, de_DE,...) so we have filenames like: settings_en_GB.txt, settings_de_DE.txt,... values_en_GB.txt, settings_de_DE.txt,... so we have another implementation (s)tfprintf( text, dictionary_files[, language][, seperator]) where dictionary_files have there names with a special sign like '#' replaced by the locale or with dictionary_files being an array of files array( locale=>filename) at last seperator is the used to divide the lines of the translation files into input and output what makes a file like en_GB.txt: animal does something:the %(color)s %(animal)s is %(action)s animal:cobra color:green action:eating target:mouse the example code would now be: $text = "%(animal does something)s"; $translated_text = stfprintf( stfprintf( $text, "#.txt", $lang), "#.txt", $lang); forth: obvious is that handling translations by a database is also useful. In this scenario we may have a table structure like create table ( id int, locale char(5), input text, output text) but that would require an implementation in dbx as every database is different in handling text..... - marcus <---> <---> wrote in message news:20010828125826.91120.qmail@pb1.pair.com... > I have seen that in php there isn't nothing similar to dictionary > substitution in python. > (a dictionary is an array with string keys, like hash in perl) > > This change consist in adding two functions ("a" stay for "array"): > aprintf(string format, array dict) -- like printf, print the result > saprintf(string format, array dict) -- like sprintf, return the result > > It works like this (written in php-like language): > > format -> "my name is %(name)s and i'm %(age)s" > dict -> array( name=>"tom", age=> "eighteen" ); > > (in php, unlike python, is possible to make an array with both string and > number indices, so the format can be also %(2)s,...) > > aprintf(format,dict) -- print "my name is tom and i'm eighteen" > saprintf(format,dict) -- return "my name is tom and i'm eighteen" > > in python, these substitutions are very useful, especially in cgi > programming, for making templates from text files, in php could be > useful in, for example, language customisation, or message formatting, > etc... > > An example: > if ($lang == "it") > define("MESSAGE","il %(animal)s %(color)s sta %(action)s %(target)s"); > else > define("MESSAGE","the %(color)s %(animal)s is %(action)s"); > > aprintf(MESSAGE,array(animal=>"cobra",color=>"green",action=>"eating",target > =>"mouse")); > // if the %(target)s isn't found, is ignored. > > > (the "s" terminator could be substituted with other letters, like d for > numbers, etc...) > > This approach has several advantages over something like this: > "the $color $animal is $action" > because in this phrase, variables are substituted when the parser execute > it, and in this case: > "the %(color)s %(animal)s is %(action)s" > parameters are substituted only when the phrase is parsed with a specialized > function like aprintf > > > > I think that this is a good idea and could save a lot of time when the > program need to be as modular as possible. > > ---------------- > Federico Marani > flagzenNO-SPAM@libero.it > ---------------- > > >

« previous php.dev (#64952) next »