Skip to content

Commit

Permalink
a note on translation function usage with dates
Browse files Browse the repository at this point in the history
  • Loading branch information
oxpa committed Sep 29, 2015
1 parent 4d41709 commit f81b7be
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ Options is a proplist possibly containing:
when Locale::string(), Context::string().
```
Please, keep in mind, that if your templates where not specially
designed, you probably still need render time translations.
See description of the `translation_fun` render option for more
details on the translation `context`.
details on the translation `context`.
Notice, you may instead pass a `fun/0`, `{Module, Function}` or
`{Module, Function, Args}` which will be called recursively until it
Expand Down Expand Up @@ -412,9 +414,43 @@ Same as `render/1`, but with the following options:
{% endblocktrans %}
```
Notice, the translation fun can also be a `fun/0` or a MFA-tuple to
setup the translation prior to rendering. See the `translation_fun`
compile option for more details.
Render time translation function is also used to translate dates.
Date tokens mimics those used in django, so you may reuse django translations.
Tokens may appear in a date are:
1. Full months names, capitalized ("January" .. "December");
2. 3 letters months names ("jan" .. "dec");
3. Associated Press style months
("Jan." .. "Dec." with "abbrev.month" context);
4. Alternative month name, for "E" option
("January" .. "December" with "alt. month" context);
5. Full week day names, capitalized ("Monday" .. "Sunday");
6. Abbreviated week day names, capitalized ("Mon" .. "Sun");
7. day time tokens ("AM", "PM", "a.m.", "p.m.", "noon", "midnight");
While date token values may be passed as lists, consider using binaries
as a default string format.
Here is a simple but robust translation function stub:
```erlang
translation_placeholder({Val,{Plural, Count}}, {L, C}) when is_list(Val) ->
translation_placeholder({list_to_binary(Val),{Plural, Count}}, LC);
translation_placeholder(Val, {L, C}) when is_list(Val) ->
translation_placeholder(list_to_binary(Val), LC);
translation_placeholder(Val, {L, C}) when is_list(C) ->
translation_placeholder(Val, list_to_binary(C));
translation_placeholder(Val, {L, C}) when is_list(C) ->
io:format("Translating ~p into ~p with context ~p~n", [Val, L, C]),
%% do nothing and return original value
Val.
```
The translation fun can also be a `fun/0`, `{Module, Function}` or
`{Module, Function, Args}` which will be called recursively until it
yields a valid translation function, at which time any needed
translation setup actions can be carried out prior to returning the
next step (either another setup function/tuple, or the translation
function).
* `lists_0_based` - If the compile option `lists_0_based` was set to
`defer`, pass this option (or set it to true, `{lists_0_based,
Expand Down

0 comments on commit f81b7be

Please sign in to comment.