Skip to content

Commit

Permalink
made localization available to the users
Browse files Browse the repository at this point in the history
* Setup the I18n system
* Detect user language
* Add locale switcher to footer
  • Loading branch information
jhass committed Aug 16, 2013
1 parent 113124b commit 89876d1
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 70 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ gem 'mysql2'
gem 'devise', '~> 3.0.0'
gem 'rails_admin'

gem 'rails_locale_detection', '~> 2.0.0.pre'

gem 'sass-rails', '~> 4.0.0'
gem 'bootstrap-sass', '~> 2.3.2.1'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ GEM
haml (4.0.3)
tilt
hike (1.2.3)
http_accept_language (1.0.2)
i18n (0.6.5)
jquery-rails (3.0.4)
railties (>= 3.0, < 5.0)
Expand Down Expand Up @@ -115,6 +116,9 @@ GEM
remotipart (~> 1.0)
safe_yaml (~> 0.9)
sass-rails (~> 4.0)
rails_locale_detection (2.0.0.pre4)
http_accept_language (~> 1.0.2)
rails (>= 3.2.0)
railties (4.0.0)
actionpack (= 4.0.0)
activesupport (= 4.0.0)
Expand Down Expand Up @@ -181,6 +185,7 @@ PLATFORMS
ruby

DEPENDENCIES
bootstrap-sass (~> 2.3.2.1)
capybara (~> 2.1.0)
coffee-rails (~> 4.0.0)
devise (~> 3.0.0)
Expand All @@ -189,6 +194,7 @@ DEPENDENCIES
mysql2
rails (= 4.0.0)
rails_admin
rails_locale_detection (~> 2.0.0.pre)
rspec-instafail (~> 0.2.4)
rspec-rails (~> 2.14.0)
sass-rails (~> 4.0.0)
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-dropdown
//= require_tree .
72 changes: 64 additions & 8 deletions app/assets/stylesheets/main.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@

#mainnav.pull-right {
margin-top: 12px;

ul.nav {
text-transform: lowercase;
text-transform: lowercase;
}

a.btn {
Expand Down Expand Up @@ -378,13 +378,69 @@ section {
color: #666;
text-shadow: 0 0px 3px #fff; }

#locale-switcher {
text-align: right;
border-left: 1px solid #eee;
height: 5em;

ul {
text-align: left;

.current a {
font-weight: bold;
}
}

// Hacks to make it work with the f*cked up bootstrap
.dropdown-toggle:after {
content:'';
border-top-color: black;
}

.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
display: none;
float: left;
min-width: 160px;
padding: 5px 0;
margin: 2px 0 0;
list-style: none;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
}

.btn-group {
position: relative;
display: inline-block;
}
.open {
& > .dropdown-menu {
display: block;
}
}
}

#CC-licence {
margin: 4em 0;
margin: 12em 0 3em 0;
text-align: center;
font-size: 10px; }
#CC-licence span {
font-size: 10px;

img {
border: 0;
margin: 0 10px;
vertical-align: middle;
}

span {
font-size: 10px;
color: #666; }
#CC-licence a {
color: #666;
}

a {
font-size: 10px;
color: #666; }
color: #666;
}
}
6 changes: 6 additions & 0 deletions app/controllers/i18n_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class I18nController < ApplicationController
def switch_locale
request.env["HTTP_REFERER"] ||= root_path
redirect_to :back
end
end
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module ApplicationHelper
include I18nHelper
def page_title title, suffix = t(:title)
if title.blank? || title =~ /home/i
suffix
Expand Down
40 changes: 23 additions & 17 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
<footer>
<div class="container">
<div class="row">
<h4>
Useful Resources
</h4>
<div id="links">
<div class="span4">
<div class="span10" id="links">
<h4>
Useful Resources
</h4>
<div class="span3">
<ul>
<li>
<a href="http://github.com/diaspora/diaspora/" title="Git repository">
Expand All @@ -61,19 +61,15 @@
Documentation
</a>
</li>
</ul>
</div>

<div class="span4">
<ul>
<li>
<a href="http://github.com/diaspora/diaspora/issues" title="Bug tracker">
Find &amp; report bugs
</a>
</li>
</ul>
</div>
<div class="span4">

<div class="span3">
<ul>
<li>
<a href="http://webchat.freenode.net/?channels=diaspora" title="#diaspora">
Expand All @@ -89,7 +85,7 @@
</ul>
</div>

<div class="span4">
<div class="span3">
<ul>
<li>
<a href="http://groups.google.com/group/diaspora-discuss"
Expand All @@ -107,16 +103,26 @@
</div>
</div>

<div id="locale-switcher" class="span4">
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<%= t ".choose_language" %>
</a>

<ul class="dropdown-menu">
<%= locale_switcher_entries %>
</ul>
</div>
</div>

<div class="row" id="CC-licence">
<div class="span16">
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/">
<%= image_tag '80x15.png', alt: 'Creative Commons License', style: 'border-width: 0;' %>
<%= image_tag '80x15.png', alt: 'Creative Commons License' %>
</a>

<br />
<a href="http://diasporafoundation.org/" property="cc:attributionName"
<a href="https://diasporafoundation.org/" property="cc:attributionName"
rel="cc:attributionURL">
http://DiasporaFoundation.org
diasporafoundation.org
</a> is licensed under a
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/">
Creative Commons Attribution 3.0 Unported License
Expand Down
21 changes: 21 additions & 0 deletions config/initializers/i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
AVAILABLE_LOCALES = {
en: "English",
de: "Deutsch"
}

I18n.default_locale = :en
I18n.available_locales = AVAILABLE_LOCALES.keys


I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
AVAILABLE_LOCALES.keys.each do |locale|
I18n.fallbacks[locale] = [locale, I18n.default_locale]
end

RailsLocaleDetection.config do |config|
config.locale_expiry = 3.months
config.set_default_url_option = :explicity # valid values are true, false, :always, :never and :explicity
config.detection_order = [:param, :user, :cookie, :request]
config.automatically_detect = true
config.locale_key = :locale
end
13 changes: 13 additions & 0 deletions config/initializers/i18n_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module I18nHelper
def locale_switcher_entries
AVAILABLE_LOCALES.map { |key, name|
locale_switcher_entry key, name
}.join("\n").html_safe
end

def locale_switcher_entry key, name
content_tag(:li, class: current_locale == key ? 'current' : nil) do
link_to name, switch_locale_path(locale: key)
end
end
end
Loading

0 comments on commit 89876d1

Please sign in to comment.