erlang-unicode / i18n

icu nif: international components for unicode from Erlang (unicode, date, string, number, format, locale, localization, transliteration, icu4e)
65 stars 20 forks source link

How can I use named args in i18n_message:format? #14

Closed brigadier closed 6 years ago

brigadier commented 8 years ago

How can I use named args in i18n_message:format?

It works, non-named integer argument with argtype and argstyle:

1> M1 = i18n_message:open(i18n:from(<<"{ 0, number, integer }"/utf8>>)).
<<>>
2> io:format("~n~ts~n", [i18n_string:to_utf8(i18n_message:format(M1, [123]))]).

123
ok

It does not, named argument with argtype and argstyle:

3> M2 = i18n_message:open(i18n:from(<<"{ a0, number, integer }"/utf8>>)).      
<<>>
4> io:format("~n~ts~n", [i18n_string:to_utf8(i18n_message:format(M2, [{a0, 123}]))]).
** exception error: {i18n_error,{bad_element,'U_ZERO_ERROR',
                                             {list,[{a0,123}]},
                                             {index,0}},
                                {line,867},
                                {file,'c_src/i18n_message.cpp'}}
     in function  i18n_message:format/2 (src/i18n_message.erl, line 53)

It again works, named string argument without argtype and argstyle:

5> M3 = i18n_message:open(i18n:from(<<"{ a0 }"/utf8>>)).                             
<<>>
6> io:format("~n~ts~n", [i18n_string:to_utf8(i18n_message:format(M3, [{a0, i18n:from(<<"123">>)}]))]).

123
ok

According to http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html all three examples should be valid.

arcusfelis commented 8 years ago

It expects float:

io:format("~n~ts~n", [i18n_string:to_utf8(i18n_message:format(M2, [{a0, 123.0}]))]).

Looks like it's a good idea to convert automatically from integers inside i18n.