Browse files

New "lang_" function for setting Content-Language

  • Loading branch information...
Evan Miller
Evan Miller committed Mar 15, 2012
1 parent bb6d5e5 commit 0606855de3d0176e57f6bfa24895e9ff517c2199
Showing with 29 additions and 1 deletion.
  1. +16 −0 doc-src/api-controller.html
  2. +13 −1 src/boss/boss_web_controller.erl
@@ -4,6 +4,7 @@
&nbsp; <a href="#routes">Routes</a>
&nbsp; <a href="#auth">Authorization</a>
&nbsp; <a href="#return_values">Return values</a>
+&nbsp; <a href="#lang">Content-Language</a>
&nbsp; <a href="#filter">Post-processing</a>
&nbsp; <a href="#simplebridge">SimpleBridge request object</a></p>
<p>Chicago Boss associates each URL with a function of a controller.
@@ -200,6 +201,21 @@ <h3>Return values</h3>
<p>Invoke the 404 File Not Found handler.</p>
<br />
+<a name="lang"></a>
+<p>CB application views can be multi-lingual. By default, the language served to the client is chosen by comparing the incoming Accept-Language header to the available translations in a given view (see <a href="">"How Chicago Boss Chooses Which Language To Serve"</a>. This can be overridden in two ways:</p>
+ <li>Returning [{"Content-Language", Lang}] from each action in your controller
+ <li>Defining a <code>lang_</code> function in your controller which returns the chosen language
+<p>The <code>lang_</code> function will be passed the name of the current action, and optionally the result of the <code>before_</code> filter. This function should return one of:</p>
+ <li><code>auto</code> - automatically choose a language based on the Accept-Language header
+ <li>A string indicating the language choice ("en", "fr", etc.)
+<br />
<a name="filter"></a>
<p>If it exists, a function called <code>after_</code> in your controller will be passed the result that is about to be returned to the client. The 'after_' function takes two or three arguments:</p>
@@ -616,7 +616,19 @@ execute_action({Controller, Action, Tokens} = Location, AppInfo, Req, SessionID,
Result = case ActionResult of
undefined ->
- render_view(Location, AppInfo, Req, SessionID, [{"_before", Info}]);
+ LangResult = case proplists:get_value("lang_", ExportStrings) of
+ 2 ->
+ ControllerInstance:lang_(Action);
+ 3 ->
+ ControllerInstance:lang_(Action, Info);
+ _ ->
+ auto
+ end,
+ Headers = case LangResult of
+ auto -> [];
+ _ -> [{"Content-Language", LangResult}]
+ end,
+ render_view(Location, AppInfo, Req, SessionID, [{"_before", Info}], Headers);
ActionResult ->
process_action_result({Location, Req, SessionID, [Location|LocationTrail]},
ActionResult, AppInfo, Info)

0 comments on commit 0606855

Please sign in to comment.