230 lines
11 KiB
HTML
230 lines
11 KiB
HTML
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
|
||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||
|
<head>
|
||
|
<title>Module: ActionController::Filters::ClassMethods</title>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
||
|
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
||
|
<link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
|
||
|
<script type="text/javascript">
|
||
|
// <![CDATA[
|
||
|
|
||
|
function popupCode( url ) {
|
||
|
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
||
|
}
|
||
|
|
||
|
function toggleCode( id ) {
|
||
|
if ( document.getElementById )
|
||
|
elem = document.getElementById( id );
|
||
|
else if ( document.all )
|
||
|
elem = eval( "document.all." + id );
|
||
|
else
|
||
|
return false;
|
||
|
|
||
|
elemStyle = elem.style;
|
||
|
|
||
|
if ( elemStyle.display != "block" ) {
|
||
|
elemStyle.display = "block"
|
||
|
} else {
|
||
|
elemStyle.display = "none"
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// Make codeblocks hidden by default
|
||
|
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
||
|
|
||
|
// ]]>
|
||
|
</script>
|
||
|
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
|
||
|
|
||
|
<div id="classHeader">
|
||
|
<table class="header-table">
|
||
|
<tr class="top-aligned-row">
|
||
|
<td><strong>Module</strong></td>
|
||
|
<td class="class-name-in-header">ActionController::Filters::ClassMethods</td>
|
||
|
</tr>
|
||
|
<tr class="top-aligned-row">
|
||
|
<td><strong>In:</strong></td>
|
||
|
<td>
|
||
|
<a href="../../../files/lib/gloc-rails_rb.html">
|
||
|
lib/gloc-rails.rb
|
||
|
</a>
|
||
|
<br />
|
||
|
</td>
|
||
|
</tr>
|
||
|
|
||
|
</table>
|
||
|
</div>
|
||
|
<!-- banner header -->
|
||
|
|
||
|
<div id="bodyContent">
|
||
|
|
||
|
|
||
|
|
||
|
<div id="contextContent">
|
||
|
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
<div id="method-list">
|
||
|
<h3 class="section-bar">Methods</h3>
|
||
|
|
||
|
<div class="name-list">
|
||
|
<a href="#M000001">autodetect_language_filter</a>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
<!-- if includes -->
|
||
|
|
||
|
<div id="section">
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
<!-- if method_list -->
|
||
|
<div id="methods">
|
||
|
<h3 class="section-bar">Public Instance methods</h3>
|
||
|
|
||
|
<div id="method-M000001" class="method-detail">
|
||
|
<a name="M000001"></a>
|
||
|
|
||
|
<div class="method-heading">
|
||
|
<a href="#M000001" class="method-signature">
|
||
|
<span class="method-name">autodetect_language_filter</span><span class="method-args">(*args)</span>
|
||
|
</a>
|
||
|
</div>
|
||
|
|
||
|
<div class="method-description">
|
||
|
<p>
|
||
|
This filter attempts to auto-detect the clients desired language. It first
|
||
|
checks the params, then a cookie and then the HTTP_ACCEPT_LANGUAGE request
|
||
|
header. If a language is found to match or be similar to a currently valid
|
||
|
language, then it sets the current_language of the controller.
|
||
|
</p>
|
||
|
<pre>
|
||
|
class ExampleController < ApplicationController
|
||
|
set_language :en
|
||
|
autodetect_language_filter :except => 'monkey', :on_no_lang => :lang_not_autodetected_callback
|
||
|
autodetect_language_filter :only => 'monkey', :check_cookie => 'monkey_lang', :check_accept_header => false
|
||
|
...
|
||
|
def lang_not_autodetected_callback
|
||
|
redirect_to somewhere
|
||
|
end
|
||
|
end
|
||
|
</pre>
|
||
|
<p>
|
||
|
The <tt>args</tt> for this filter are exactly the same the arguments of
|
||
|
<tt>before_filter</tt> with the following exceptions:
|
||
|
</p>
|
||
|
<ul>
|
||
|
<li><tt>:check_params</tt> — If false, then params will not be checked
|
||
|
for a language. If a String, then this will value will be used as the name
|
||
|
of the param.
|
||
|
|
||
|
</li>
|
||
|
<li><tt>:check_cookie</tt> — If false, then the cookie will not be
|
||
|
checked for a language. If a String, then this will value will be used as
|
||
|
the name of the cookie.
|
||
|
|
||
|
</li>
|
||
|
<li><tt>:check_accept_header</tt> — If false, then HTTP_ACCEPT_LANGUAGE
|
||
|
will not be checked for a language.
|
||
|
|
||
|
</li>
|
||
|
<li><tt>:on_set_lang</tt> — You can specify the name of a callback
|
||
|
function to be called when the language is successfully detected and set.
|
||
|
The param must be a Symbol or a String which is the name of the function.
|
||
|
The callback function must accept one argument (the language) and must be
|
||
|
instance level.
|
||
|
|
||
|
</li>
|
||
|
<li><tt>:on_no_lang</tt> — You can specify the name of a callback
|
||
|
function to be called when the language couldn’t be detected
|
||
|
automatically. The param must be a Symbol or a String which is the name of
|
||
|
the function. The callback function must be instance level.
|
||
|
|
||
|
</li>
|
||
|
</ul>
|
||
|
<p>
|
||
|
You override the default names of the param or cookie by calling <tt><a
|
||
|
href="../../GLoc.html#M000014">GLoc.set_config</a> :default_param_name
|
||
|
=> ‘new_param_name‘</tt> and <tt><a
|
||
|
href="../../GLoc.html#M000014">GLoc.set_config</a> :default_cookie_name
|
||
|
=> ‘new_cookie_name‘</tt>.
|
||
|
</p>
|
||
|
<p><a class="source-toggle" href="#"
|
||
|
onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
|
||
|
<div class="method-source-code" id="M000001-source">
|
||
|
<pre>
|
||
|
<span class="ruby-comment cmt"># File lib/gloc-rails.rb, line 43</span>
|
||
|
43: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">autodetect_language_filter</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
|
||
|
44: <span class="ruby-identifier">options</span>= <span class="ruby-identifier">args</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">last</span> <span class="ruby-operator">:</span> {}
|
||
|
45: <span class="ruby-identifier">x</span>= <span class="ruby-value str">'Proc.new { |c| l= nil;'</span>
|
||
|
46: <span class="ruby-comment cmt"># :check_params</span>
|
||
|
47: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">v</span>= <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:check_params</span>)) <span class="ruby-operator">==</span> <span class="ruby-keyword kw">false</span>
|
||
|
48: <span class="ruby-identifier">name</span>= <span class="ruby-identifier">v</span> <span class="ruby-value">? </span><span class="ruby-node">":#{v}"</span> <span class="ruby-operator">:</span> <span class="ruby-value str">'GLoc.get_config(:default_param_name)'</span>
|
||
|
49: <span class="ruby-identifier">x</span> <span class="ruby-operator"><<</span> <span class="ruby-node">"l ||= GLoc.similar_language(c.params[#{name}]);"</span>
|
||
|
50: <span class="ruby-keyword kw">end</span>
|
||
|
51: <span class="ruby-comment cmt"># :check_cookie</span>
|
||
|
52: <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">v</span>= <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:check_cookie</span>)) <span class="ruby-operator">==</span> <span class="ruby-keyword kw">false</span>
|
||
|
53: <span class="ruby-identifier">name</span>= <span class="ruby-identifier">v</span> <span class="ruby-value">? </span><span class="ruby-node">":#{v}"</span> <span class="ruby-operator">:</span> <span class="ruby-value str">'GLoc.get_config(:default_cookie_name)'</span>
|
||
|
54: <span class="ruby-identifier">x</span> <span class="ruby-operator"><<</span> <span class="ruby-node">"l ||= GLoc.similar_language(c.send(:cookies)[#{name}]);"</span>
|
||
|
55: <span class="ruby-keyword kw">end</span>
|
||
|
56: <span class="ruby-comment cmt"># :check_accept_header</span>
|
||
|
57: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:check_accept_header</span>) <span class="ruby-operator">==</span> <span class="ruby-keyword kw">false</span>
|
||
|
58: <span class="ruby-identifier">x</span> <span class="ruby-operator"><<</span> <span class="ruby-value str">%<
|
||
|
59: unless l
|
||
|
60: a= c.request.env['HTTP_ACCEPT_LANGUAGE'].split(/,|;/) rescue nil
|
||
|
61: a.each {|x| l ||= GLoc.similar_language(x)} if a
|
||
|
62: end; ></span>
|
||
|
63: <span class="ruby-keyword kw">end</span>
|
||
|
64: <span class="ruby-comment cmt"># Set language</span>
|
||
|
65: <span class="ruby-identifier">x</span> <span class="ruby-operator"><<</span> <span class="ruby-value str">'ret= true;'</span>
|
||
|
66: <span class="ruby-identifier">x</span> <span class="ruby-operator"><<</span> <span class="ruby-value str">'if l; c.set_language(l); c.headers[\'Content-Language\']= l.to_s; '</span>
|
||
|
67: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:on_set_lang</span>)
|
||
|
68: <span class="ruby-identifier">x</span> <span class="ruby-operator"><<</span> <span class="ruby-node">"ret= c.#{options.delete(:on_set_lang)}(l);"</span>
|
||
|
69: <span class="ruby-keyword kw">end</span>
|
||
|
70: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:on_no_lang</span>)
|
||
|
71: <span class="ruby-identifier">x</span> <span class="ruby-operator"><<</span> <span class="ruby-node">"else; ret= c.#{options.delete(:on_no_lang)};"</span>
|
||
|
72: <span class="ruby-keyword kw">end</span>
|
||
|
73: <span class="ruby-identifier">x</span> <span class="ruby-operator"><<</span> <span class="ruby-value str">'end; ret }'</span>
|
||
|
74:
|
||
|
75: <span class="ruby-comment cmt"># Create filter</span>
|
||
|
76: <span class="ruby-identifier">block</span>= <span class="ruby-identifier">eval</span> <span class="ruby-identifier">x</span>
|
||
|
77: <span class="ruby-identifier">before_filter</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
||
|
78: <span class="ruby-keyword kw">end</span>
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
<div id="validator-badges">
|
||
|
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
||
|
</div>
|
||
|
|
||
|
</body>
|
||
|
</html>
|