Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

adding language detection for the home page.

  • Loading branch information...
commit 70f3198441de8784092ed8c16537d997a0fe5d16 1 parent 60b2fcc
@yeah authored
25 README.rdoc
@@ -146,15 +146,38 @@ Of course, you want those nice little flags on your web site as soon as you have
+=== Language detection
+Alright, language selection is great, but you want your first time users to see the right page according to their <code>Accept-Language</code> headers, don't you? Perfect, because that's what Mulilingual Pages does out of the box. If the home page is accessed using the basic slug <code>/</code>, language detection will redirect the user to the right language version using the route suffix.
+If that's not what you like, you can always disable this feature by setting the <code>multilingual.use_language_detection</code> config option to <code>false</code>.
+== Configuration option reference
+Configuration options are stored in Radiant's <code>config</code> table and are prefixed with <code>multilingual.</code>, so they should be easy to spot. The following options are available:
+* <code>non_multilingual_route</code>: The first part of the route suffix which is used for non-multilingual pages. Can be set to <code>''</code> (the empty string) in order to just append the two-character language code to non-multilingual routes. But beware: if you have slugs with only two characters, you might get in trouble and you'd better set <code>non_multilingual_route</code> to something like <code>lang-</code>. (Default: <code>lang-</code>)
+* <code>default_language</code>: The default language that is used when nothing else is found. (Default: <code>en</code>)
+* <code>meta_part_name</code>: The name of the page parts that are added to multilingual pages and which are used to specify multilingual meta information like title, breadcrumbs, etc. (Default: <code>multilingual meta</code>)
+* <code>available_languages</code>: A comma separated list of available languages throughout your site, used for <code><r:language_selection>...</r:language_selection></code> and other things. Every time you add a multilingual slug to a multilingual page, its two character language code is automatically appended to this list for you. If you remove a language version, you'd have to remove it from this list as well, in order for it to disappear form language selection menus. You can also re-order the languages here, language selection will follow the order of this list. (Default: <code>en</code> and evolving with your multilingual slugs)
+* <code>use_language_detection</code>: Use this switch to turn language detection (see above) on or off. (Default: <code>true</code>)
== Version
== Licence
+== Credits
+The language detection feature borrows concept and code from the Language Redirect extension.
== Contributors
* Jan Schulz-Hofen
42 lib/non_multilingual_page_extensions.rb
@@ -2,6 +2,10 @@ module NonMultilingualPageExtensions
def self.included(base)
base.class_eval do
alias_method_chain :find_by_url, :language_param
+ alias_method_chain :render, :language_detection
+ alias_method_chain :headers, :language_detection
+ alias_method_chain :response_code, :language_detection
+ alias_method_chain :cache?, :language_detection
@@ -15,4 +19,42 @@ def find_by_url_with_language_param(url, live = true, clean = true)
+ def headers_with_language_detection
+ needs_language_detection? ? { 'Location' => location, 'Vary' => "Accept-Language" } : headers_without_language_detection
+ end
+ def render_with_language_detection
+ needs_language_detection? ? '<html><body>Redirecting...</body></html>' : render_without_language_detection
+ end
+ def response_code_with_language_detection
+ needs_language_detection? ? 302 : response_code_without_language_detection
+ end
+ def cache_with_language_detection?
+ needs_language_detection? ? false : cache_without_language_detection?
+ end
+ private
+ def needs_language_detection?
+ MultilingualPagesExtension::USE_LANGUAGE_DETECTION and (not parent?) and Thread.current[:requested_language].nil?
+ end
+ def languages
+ langs = (request.env["HTTP_ACCEPT_LANGUAGE"] || "").split(/[,\s]+/)
+ langs_with_weights = do |ele|
+ both = ele.split(/;q=/)
+ lang = both[0].split('-').first
+ weight = both[1] ? Float(both[1]) : 1
+ [-weight, lang]
+ end.sort_by(&:first).map(&:last)
+ end
+ def location
+ language = languages.detect{|l| MultilingualPagesExtension::AVAILABLE_LANGUAGES.include?(l)}
+ path = clean_url("#{request.request_uri}/#{MultilingualPagesExtension::NON_MULTILINGUAL_ROUTE}#{language}")
+ "#{request.protocol}#{request.host_with_port}#{path}"
+ end
9 multilingual_pages_extension.rb
@@ -10,9 +10,14 @@ def activate
# get config from database or initialize defaults
if Radiant::Config.table_exists?
- {:default_language => 'en', :non_multilingual_route => 'lang-', :meta_part_name => 'multilingual meta', :available_languages => 'en'}.each do |key,value|
+ {:default_language => 'en',
+ :non_multilingual_route => 'lang-',
+ :meta_part_name => 'multilingual meta',
+ :available_languages => 'en',
+ :use_language_detection => true}.each do |key,value|
Radiant::Config["multilingual.#{key}"] = value unless Radiant::Config["multilingual.#{key}"]
- MultilingualPagesExtension.const_set(key.to_s.upcase, Radiant::Config["multilingual.#{key}"])
+ value = Radiant::Config["multilingual.#{key}"].blank? ? Radiant::Config["multilingual.#{key}"] : YAML.load(Radiant::Config["multilingual.#{key}"])
+ MultilingualPagesExtension.const_set(key.to_s.upcase, value)

0 comments on commit 70f3198

Please sign in to comment.
Something went wrong with that request. Please try again.