PHP 8.5.0 Alpha 2 available for testing

MessageFormatter::setPattern

msgfmt_set_pattern

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

MessageFormatter::setPattern -- msgfmt_set_patternConfigura el patrón utilizado por el formateador

Descripción

Estilo orientado a objetos

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

Estilo procedimental

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

Configura el patrón utilizado por el formateador.

Parámetros

formatter

Un objeto de formateador de mensajes MessageFormatter

pattern

La cadena de patrón utilizada por el formateador de mensajes. El patrón utiliza una sintaxis que acepta comillas; Ver » Quoting/Escaping para más detalles.

Valores devueltos

Esta función retorna true en caso de éxito o false si ocurre un error.

Ejemplos

Ejemplo #1 Ejemplo con msgfmt_set_pattern(), estilo procedimental

<?php
$fmt
= msgfmt_create( "en_US", "{0, number} singes sur {1, number} arbres" );
echo
"Patrón por omisión : '" . msgfmt_get_pattern( $fmt ) . "'\n";
echo
"Resultado formateado : " . msgfmt_format( $fmt, array(123, 456) ) . "\n";

msgfmt_set_pattern( $fmt, "{0, number} arbres accueillant {1, number} singes" );
echo
"Nuevo patrón :'" . msgfmt_get_pattern( $fmt ) . "'\n";
echo
"Número formateado : " . msgfmt_format( $fmt, array(123, 456) ) . "\n";
?>

Ejemplo #2 Ejemplo con msgfmt_set_pattern(), estilo POO

<?php
$fmt
= new MessageFormatter( "en_US", "{0, number} singes sur {1, number} arbres" );
echo
"Patrón por omisión : '" . $fmt->getPattern() . "'\n";
echo
"Resultado formateado : " . $fmt->format(array(123, 456)) . "\n";

$fmt->setPattern("{0, number} arbres accueillant {1, number} singes" );
echo
"Nuevo patrón :'" . $fmt->getPattern() . "'\n";
echo
"Número formateado : " . $fmt->format(array(123, 456)) . "\n";
?>

El ejemplo anterior mostrará :

Patrón por omisión : '{0,number} singes sur {1,number} arbres'
Resultado formateado : 123 singes sur 456 arbres
Nuevo patrón :'{0,number} arbres accueillant {1,number} singes'
Número formateado : 123 arbres accueillant 456 singes

Ver también

add a note

User Contributed Notes 1 note

up
0
neil dot smith at vouchercloud dot com
10 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