CakeFest 2024: The Official CakePHP Conference

MessageFormatter::setPattern

msgfmt_set_pattern

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

MessageFormatter::setPattern -- msgfmt_set_patternEstablecer el patrón usado por el formateador

Descripción

Estilo orientado a objetos

public MessageFormatter::setPattern(string $pattern): bool

Estilo por procedimientos

msgfmt_set_pattern(MessageFormatter $fmt, string $pattern): bool

Establece el patrón usado por el formateador

Parámetros

fmt

El formateador de mensajes

pattern

El patrón de tipo string para utilizarlo en el formateador de mensajes. El patrón utiliza una sintaxis 'amigable con apóstrofes'; se ejecuta a través de » umsg_autoQuoteApostrophe antes de ser interpretado.

Valores devueltos

Devuelve true en caso de éxito o false en caso de error.

Ejemplos

Ejemplo #1 Ejemplo de msgfmt_set_pattern()

<?php
$fmt
= msgfmt_create( "es_ES", "{0, number} monos en {1, number} árboles" );
echo
"Patrón predeterminado: '" . msgfmt_get_pattern( $fmt ) . "'\n";
echo
"Resultado del formateo: " . msgfmt_format( $fmt, array(123, 456) ) . "\n";

msgfmt_set_pattern( $fmt, "{0, number} árboles alojan {1, number} monos" );
echo
"Patrón nuevo: '" . msgfmt_get_pattern( $fmt ) . "'\n";
echo
"Número formateado: " . msgfmt_format( $fmt, array(123, 456) ) . "\n";
?>

Ejemplo #2 Ejemplo orientado a objetos

<?php
$fmt
= new MessageFormatter( "es_ES", "{0, number} monos en {1, number} árboles" );
echo
"Patrón predeterminado: '" . $fmt->getPattern() . "'\n";
echo
"Resultado del formateo: " . $fmt->format(array(123, 456)) . "\n";

$fmt->setPattern( "{0, number} árboles alojan {1, number} monos" );
echo
"Patrón nuevo: '" . $fmt->getPattern() . "'\n";
echo
"Número formateado: " . $fmt->format(array(123, 456)) . "\n";
?>

El resultado del ejemplo sería:

Patrón predeterminado: '{0, number} monos en {1, number} árboles' 
Resultado del formateo: 123 monos en 456 árboles 
Patrón nuevo: '{0, number} árboles alojan {1, number} monos' 
Número formateado: 123 árboles alojan 456 monos

Ver también

add a note

User Contributed Notes 1 note

up
0
neil dot smith at vouchercloud dot com
9 years ago
A correct example would be to transpose the placeholders in pattern 2.

This is typical where changing messages from one language to another changes the word order, to result in a sensible translation.

Let's imagine it's localised as Spanish or Russian instead of English.
So really you want to have pattern 2 provided by your human translator to read like this :

New pattern: '{1,number} trees hosting {0,number} monkeys'
Formatted number: 456 trees hosting 123 monkeys

That is - the order of arguments is always the same, but your translated message content has the placeholders transposed for non English languages.
To Top