[Solar-talk] locale strings + replacements
Rodrigo Moraes
rodrigo.moraes at gmail.com
Wed Nov 29 03:49:23 PST 2006
On 11/29/06, Paul M Jones wrote:
> My opinion on this is: if you need sprintf() formatting, you can keep
> the format string as the locale string, then call sprintf() from the
> view (instead of it being used in the locale() method when it's not
> always needed). For example, say your locale key/value pair is this:
>
> 'TEXT_FOO' => 'The %2$s contains %1$d monkeys.'
>
> Then in your view, you can do this:
>
> echo sprintf(
> $this->getText('TEXT_FOO'),
> 7,
> 'house'
> );
>
> Then you get the benefits of sprintf() when you need them, and the
> benefits of not using sprintf() when you don't.
>
> Have I missed anything here?
I think this is a valid idea; I have implemented it in getTextRaw() as
a shortcut and it is used *when* a replacements array is passed.
Otherwise the locale string is returned as it is, without additional
processing. This is an important point: it is only used when there are
replacements to be done. This is the hack:
http://dev.tipos.org/browser/trunk/Tipos/View/Helper/GetTextRaw.php
The reason to implement it in Solar::locale() came from the initial
thought by Antti: to provide more useful info in validation messages.
An example from Solar_Sql_Table:
if (! $valid->maxLength($value, $max, Solar_Valid::OR_BLANK)) {
$err[$field][] = array(
'code' => 'VALID_MAXLENGTH',
'text' => $this->locale('VALID_MAXLENGTH', 1, $max), <- added
third parameter
'data' => $value,
'info' => array(
'max' => $max,
),
);
}
...and you could set a locale string like 'Use no máximo %d
caracteres' to be filled by Solar::locale().
It seems interesting to inform the validation rules in the messages,
and although I've never needed this, I think it is worth to consider.
Maybe there is another way to achieve this, and then we are done.
Anyway, I would add this at least to the getText helpers as a shortcut
to enhanced locale strings. There is an use and I found that it can be
pretty common, even for a English-only system.
rodrigo
More information about the solar-talk
mailing list