summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2015-06-15 17:14:26 -0700
committerelijah <elijah@riseup.net>2015-06-15 17:14:26 -0700
commit83a9cadbb13bd2292c7d064d40721fa5f64119fb (patch)
tree294c13678a0514634a6817428d2c656738c14e52
parent12ab515ecd448f98ff006e30a93e43626183b6d0 (diff)
added CommonLanguages gem
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock7
-rw-r--r--app/assets/stylesheets/leap.scss12
-rw-r--r--app/controllers/application_controller.rb36
-rw-r--r--app/controllers/pages_controller.rb2
-rw-r--r--app/helpers/core_helper.rb12
-rw-r--r--app/views/layouts/_footer.html.haml3
-rw-r--r--config/routes.rb6
-rw-r--r--engines/billing/config/routes.rb2
-rw-r--r--engines/support/config/routes.rb2
-rw-r--r--vendor/gems/common_languages/LICENSE.txt22
-rw-r--r--vendor/gems/common_languages/README.md36
-rw-r--r--vendor/gems/common_languages/Rakefile5
-rw-r--r--vendor/gems/common_languages/common_languages.gemspec23
-rw-r--r--vendor/gems/common_languages/lib/common_languages.rb46
-rw-r--r--vendor/gems/common_languages/lib/common_languages/data.rb116
-rw-r--r--vendor/gems/common_languages/lib/common_languages/language.rb19
-rw-r--r--vendor/gems/common_languages/lib/common_languages/version.rb3
-rw-r--r--vendor/gems/common_languages/test/test_helper.rb3
-rw-r--r--vendor/gems/common_languages/test/usage_test.rb33
20 files changed, 366 insertions, 23 deletions
diff --git a/Gemfile b/Gemfile
index 419b870..226b2af 100644
--- a/Gemfile
+++ b/Gemfile
@@ -20,6 +20,7 @@ gem 'rails-i18n' # locale files for built-in validation messages and times
# https://github.com/svenfuchs/rails-i18n
# for a list of keys:
# https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
+gem 'common_languages', :path => 'vendor/gems/common_languages'
## VIEWS
gem 'kaminari', "0.13.0" # for pagination. trying 0.13.0 as there seem to be
diff --git a/Gemfile.lock b/Gemfile.lock
index e140303..5130347 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -17,6 +17,12 @@ PATH
specs:
leap_web_help (0.6.0)
+PATH
+ remote: vendor/gems/common_languages
+ specs:
+ common_languages (0.0.1)
+ i18n
+
GEM
remote: https://rubygems.org/
specs:
@@ -256,6 +262,7 @@ DEPENDENCIES
certificate_authority!
client_side_validations
client_side_validations-simple_form
+ common_languages!
couchrest (~> 1.1.3)
couchrest_model (~> 2.0.0)
couchrest_session_store (= 0.3.0)
diff --git a/app/assets/stylesheets/leap.scss b/app/assets/stylesheets/leap.scss
index 13560c0..35f0ed4 100644
--- a/app/assets/stylesheets/leap.scss
+++ b/app/assets/stylesheets/leap.scss
@@ -269,6 +269,18 @@ html, body {
#footer {
padding-top: $footer-gutter;
height: $footer-height - $footer-border-width;
+ .locales {
+ text-align: center;
+ margin-bottom: 15px;
+ a {
+ padding: 4px;
+ &.active {
+ font-weight: bold;
+ background-color: #eee;
+ border-radius: 4px;
+ }
+ }
+ }
.full-height {
text-align: center;
line-height: $footer-height - $footer-border-width;
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index ff291f2..58e6f83 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -38,41 +38,30 @@ class ApplicationController < ActionController::Base
##
#
- # URL paths for which we don't enforce the locale as the prefix of the path.
- #
- NON_LOCALE_PATHS = /^\/(assets|webfinger|.well-known|rails|key|[0-9]+)($|\/)/
-
- #
# Before filter to set the current locale. Possible outcomes:
#
# (a) do nothing for certain routes and requests.
# (b) if path already starts with locale, set I18n.locale and default_url_options.
# (c) otherwise, redirect so that path starts with locale.
#
- # If the locale happens to be the default local, no paths are prefixed with the locale.
- #
def set_locale
- if request.method == "GET" && request.format == 'text/html' && request.path !~ NON_LOCALE_PATHS
- if params[:locale] && LOCALES_STRING.include?(params[:locale])
+ if request_may_have_locale?(request)
+ if CommonLanguages::available_code?(params[:locale])
I18n.locale = params[:locale]
- update_default_url_options
else
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales) || I18n.default_locale
- update_default_url_options
if I18n.locale != I18n.default_locale
redirect_to url_for(params.merge(:locale => I18n.locale))
end
end
- else
- update_default_url_options
end
end
- def update_default_url_options
- if I18n.locale != I18n.default_locale
- self.default_url_options[:locale] = I18n.locale
+ def default_url_options(options={})
+ if request_may_have_locale?(request)
+ { :locale => I18n.locale }
else
- self.default_url_options[:locale] = nil
+ { :locale => nil }
end
end
@@ -104,4 +93,17 @@ class ApplicationController < ActionController::Base
response.headers["Expires"] = "0"
end
+ private
+
+ #
+ # URL paths for which we don't enforce the locale as the prefix of the path.
+ #
+ NON_LOCALE_PATHS = /^\/(assets|webfinger|.well-known|rails|key|[0-9]+)($|\/)/
+
+ #
+ # For some requests, we ignore locale determination.
+ #
+ def request_may_have_locale?(request)
+ request.method == "GET" && request.format == 'text/html' && request.path !~ NON_LOCALE_PATHS
+ end
end
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
index c477b7c..e0f39e3 100644
--- a/app/controllers/pages_controller.rb
+++ b/app/controllers/pages_controller.rb
@@ -12,7 +12,7 @@ class PagesController < ApplicationController
private
def page_name
- request.path.sub(/^\/(#{MATCH_LOCALE}\/)?/, '')
+ request.path.sub(/^\/(#{CommonLanguages.match_available}\/)?/, '')
end
end
diff --git a/app/helpers/core_helper.rb b/app/helpers/core_helper.rb
index 46e8fa4..f34d2f2 100644
--- a/app/helpers/core_helper.rb
+++ b/app/helpers/core_helper.rb
@@ -17,4 +17,16 @@ module CoreHelper
APP_CONFIG[:service_levels].present? && APP_CONFIG[:service_levels].detect{|k,v| v['rate'].present?}
end
+ #
+ # a bunch of links to the different languages that are available.
+ #
+ def locales_links
+ CommonLanguages.available.collect { |lang|
+ link_to(lang.name,
+ {:action => params[:action], :controller => params[:controller], :locale => lang.code},
+ {:class => (lang.code == I18n.locale ? 'locale active' : 'locale')}
+ )
+ }.join(" ").html_safe
+ end
+
end
diff --git a/app/views/layouts/_footer.html.haml b/app/views/layouts/_footer.html.haml
index de53667..f502ddb 100644
--- a/app/views/layouts/_footer.html.haml
+++ b/app/views/layouts/_footer.html.haml
@@ -1,3 +1,6 @@
+- if CommonLanguages.available.size > 1
+ .locales
+ = locales_links
.full-height
= link_to icon('home') + t(:home), home_path
= link_to icon('eye-close') + t(:privacy_policy), privacy_policy_path
diff --git a/config/routes.rb b/config/routes.rb
index 3936824..c420205 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -4,7 +4,7 @@ LeapWeb::Application.routes.draw do
# so that the path will be correctly prefixed with the locale.
#
root :to => "home#index"
- get '(:locale)' => 'home#index', :locale => MATCH_LOCALE, :as => 'home'
+ get '(:locale)' => 'home#index', :locale => CommonLanguages.match_available, :as => 'home'
#
# HTTP Error Handling
@@ -13,7 +13,7 @@ LeapWeb::Application.routes.draw do
match '/404' => 'errors#not_found'
match '/500' => 'errors#server_error'
- scope "(:locale)", :locale => MATCH_LOCALE, :controller => 'pages', :action => 'show' do
+ scope "(:locale)", :locale => CommonLanguages.match_available, :controller => 'pages', :action => 'show' do
get 'privacy-policy', :as => 'privacy_policy'
get 'terms-of-service', :as => 'terms_of_service'
get 'about', :as => 'about'
@@ -38,7 +38,7 @@ LeapWeb::Application.routes.draw do
resources :configs, :only => [:index, :show]
end
- scope "(:locale)", :locale => MATCH_LOCALE do
+ scope "(:locale)", :locale => CommonLanguages.match_available do
get "login" => "sessions#new", :as => "login"
delete "logout" => "sessions#destroy", :as => "logout"
diff --git a/engines/billing/config/routes.rb b/engines/billing/config/routes.rb
index 7263dff..6bbe501 100644
--- a/engines/billing/config/routes.rb
+++ b/engines/billing/config/routes.rb
@@ -1,6 +1,6 @@
Rails.application.routes.draw do
- scope "(:locale)", :locale => MATCH_LOCALE do
+ scope "(:locale)", :locale => CommonLanguages.match_available do
match 'payments/new' => 'payments#new', :as => :new_payment
match 'payments/confirm' => 'payments#confirm', :as => :confirm_payment
resources :users do
diff --git a/engines/support/config/routes.rb b/engines/support/config/routes.rb
index ca471b3..81bdf9a 100644
--- a/engines/support/config/routes.rb
+++ b/engines/support/config/routes.rb
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
- scope "(:locale)", locale: MATCH_LOCALE do
+ scope "(:locale)", locale: CommonLanguages.match_available do
resources :tickets, except: :edit do
member do
diff --git a/vendor/gems/common_languages/LICENSE.txt b/vendor/gems/common_languages/LICENSE.txt
new file mode 100644
index 0000000..f1423ff
--- /dev/null
+++ b/vendor/gems/common_languages/LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2015 LEAP Encryption Access Project
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/gems/common_languages/README.md b/vendor/gems/common_languages/README.md
new file mode 100644
index 0000000..264fe0a
--- /dev/null
+++ b/vendor/gems/common_languages/README.md
@@ -0,0 +1,36 @@
+# CommonLanguages
+
+A simple gem that provides the ability to display the list of
+I18n.available_locales in a localized and friendly way.
+
+There are many similar or related gems. For example:
+
+* https://github.com/davec/localized_language_select
+* https://github.com/teonimesic/language_switcher
+* https://github.com/grosser/i18n_data
+* https://github.com/scsmith/language_list
+
+I wanted something different than what these others provide:
+
+* Language names should be displayed in the native name for each language, not
+ a localized or anglicized version.
+* There should not be any gem dependencies.
+* Since there is no universal collation across all languages, they should be
+ sorted in order of popularity.
+* There should not be any need to parse large data files, 99% of which will
+ never be used.
+
+# Usage
+
+This code:
+
+ I18n.available_locales = [:de, :en, :pt]
+ CommonLanguages.available.each do |language|
+ p [language.code, language.name, language.english_name, language.rtl?]
+ end
+
+Produces:
+
+ [:en, "English", "English", false]
+ [:pt, "Português", "Portugues", false]
+ [:de, "Deutsch", "German", false] \ No newline at end of file
diff --git a/vendor/gems/common_languages/Rakefile b/vendor/gems/common_languages/Rakefile
new file mode 100644
index 0000000..b7af52d
--- /dev/null
+++ b/vendor/gems/common_languages/Rakefile
@@ -0,0 +1,5 @@
+require 'rake/testtask'
+Rake::TestTask.new do |t|
+ t.test_files = FileList['test/*_test.rb']
+end
+task :default => :test \ No newline at end of file
diff --git a/vendor/gems/common_languages/common_languages.gemspec b/vendor/gems/common_languages/common_languages.gemspec
new file mode 100644
index 0000000..cc67b5a
--- /dev/null
+++ b/vendor/gems/common_languages/common_languages.gemspec
@@ -0,0 +1,23 @@
+# coding: utf-8
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'common_languages/version'
+
+Gem::Specification.new do |spec|
+ spec.name = "common_languages"
+ spec.version = CommonLanguages::VERSION
+ spec.authors = ["elijah"]
+ spec.email = ["elijah@leap.se"]
+ spec.summary = %q{Information on the most common languages.}
+ spec.description = %q{Information on the most common languages, including native name and script direction.}
+ spec.homepage = ""
+ spec.license = "MIT"
+
+ spec.files = Dir['lib/*.rb', 'lib/*/*.rb'] + ['README.md', 'LICENSE.txt']
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
+ spec.require_paths = ["lib"]
+
+ spec.add_dependency "i18n"
+ spec.add_development_dependency "minitest"
+end
diff --git a/vendor/gems/common_languages/lib/common_languages.rb b/vendor/gems/common_languages/lib/common_languages.rb
new file mode 100644
index 0000000..0b2d9c2
--- /dev/null
+++ b/vendor/gems/common_languages/lib/common_languages.rb
@@ -0,0 +1,46 @@
+# encoding: utf-8
+
+require_relative "common_languages/version"
+require_relative "common_languages/data"
+require_relative "common_languages/language"
+
+module CommonLanguages
+ def self.available_codes
+ @available_codes ||= self.codes & I18n.available_locales.map(&:to_s)
+ end
+
+ def self.available
+ @available ||= self.available_codes.map {|lc| self.get(lc) }
+ end
+
+ def self.available_code?(code)
+ if !code.nil?
+ self.available_codes.include?(code.to_s)
+ else
+ false
+ end
+ end
+
+ def self.get(code)
+ if !code.nil?
+ self.languages[code.to_s]
+ else
+ false
+ end
+ end
+
+ # a regexp that will match the available codes
+ def self.match_available
+ @match_available ||= /(#{self.available_codes.join('|')})/
+ end
+
+ # clears caches, useful only when testing
+ def self.reset
+ @codes = nil
+ @available_codes = nil
+ @available = nil
+ @languages = nil
+ @match = nil
+ @match_available = nil
+ end
+end
diff --git a/vendor/gems/common_languages/lib/common_languages/data.rb b/vendor/gems/common_languages/lib/common_languages/data.rb
new file mode 100644
index 0000000..965e18b
--- /dev/null
+++ b/vendor/gems/common_languages/lib/common_languages/data.rb
@@ -0,0 +1,116 @@
+# encoding: utf-8
+
+module CommonLanguages
+
+ #
+ # Language data, sorted by number of native speakers
+ # https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers
+ #
+ # fields: code, name, english name, right to left?
+ #
+ DATA = [
+ ['zh', '中文', 'Chinese'],
+ ['es', 'Español', 'Spanish'],
+ ['en', 'English'],
+ ['hi', 'हिन्दी', 'Hindi'],
+ ['ar', 'العربية', 'Arabic', true],
+ ['pt', 'Português', 'Portugues'],
+ ['bn', 'বাংলা', 'Bengali'],
+ ['ru', 'Pyccĸий', 'Russian'],
+ ['ja', '日本語', 'Japanese'],
+ ['pa', 'ਪੰਜਾਬੀ', 'Punjabi'],
+ ['de', 'Deutsch', 'German'],
+ ['ms', 'بهاس ملايو', 'Malay'],
+ ['te', 'తెలుగు', 'Telugu'],
+ ['vi', 'Tiếng Việt', 'Vietnamese'],
+ ['ko', '한국어', 'Korean'],
+ ['fr', 'Français', 'French'],
+ ['mr', 'मराठी', 'Marathi'],
+ ['ta', 'தமிழ்', 'Tamil'],
+ ['ur', 'اُردُو', 'Urdu'],
+ ['fa', 'فارسی', 'Farsi'],
+ ['tr', 'Türkçe', 'Turkish'],
+ ['it', 'Italiano', 'Italian'],
+ ['th', 'ภาษาไทย', 'Thai'],
+ ['gu', 'Gujarati'],
+ ['pl', 'Polski', 'Polish'],
+ ['ml', 'Malayalam'],
+ ['uk', 'Ukrainian'],
+ ['sw', 'Swahili'],
+ ['uz', 'Uzbek'],
+ ['ro', 'Romanian'],
+ ['nl', 'Nederlands', 'Dutch'],
+ ['sr', 'Serbian'],
+ ['el', 'Ελληνικά', 'Greek'],
+ ['ca', 'Català', 'Catalan'],
+ ['he', 'עברית', 'Hebrew', true]
+ ]
+
+ # just the codes, in sorted order
+ def self.codes
+ @codes ||= DATA.map {|d| d[0]}
+ end
+
+ # a regexp that will match the possible codes
+ def self.match
+ @match ||= /(#{@codes.join('|')})/
+ end
+
+ # map of codes to Language objects
+ # e.g. languages['en'] => <Language>
+ def self.languages
+ @languages ||= Hash[
+ DATA.map {|data|
+ [data[0], Language.new(data)]
+ }
+ ]
+ end
+
+end
+
+#
+# TO BE ADDED
+#
+# [:bn, 'Bengali']
+# [:bo, 'Tibetan']
+# [:bg, 'Bulgarian']
+# [:ca, 'Catalan']
+# [:cs, 'Czech']
+# [:cy, 'Welsh']
+# [:da, 'Danish']
+# [:et, 'Estonian']
+# [:eu, 'Basque']
+# [:fj, 'Fijian']
+# [:fi, 'Finnish']
+# [:ga, 'Irish']
+# [:hr, 'Croatian']
+# [:hu, 'Hungarian']
+# [:hy, 'Armenian']
+# [:id, 'Indonesian']
+# [:is, 'Icelandic']
+# [:ka, 'Georgian']
+# [:km, 'Central Khmer']
+# [:lv, 'Latvian']
+# [:lt, 'Lithuanian']
+# [:mr, 'Marathi']
+# [:mk, 'Macedonian']
+# [:mt, 'Maltese']
+# [:mn, 'Mongolian']
+# [:mi, 'Maori']
+# [:ms, 'Malay']
+# [:ne, 'Nepali']
+# [:no, 'Norwegian']
+# [:pa, 'Panjabi']
+# [:qu, 'Quechua']
+# [:sk, 'Slovak']
+# [:sl, 'Slovenian']
+# [:sm, 'Samoan']
+# [:sq, 'Albanian']
+# [:sv, 'Swedish']
+# [:ta, 'Tamil']
+# [:tt, 'Tatar']
+# [:te, 'Telugu']
+# [:to, 'Tonga']
+# [:tr, 'Turkish']
+# [:ur, 'Urdu']
+# [:xh, 'Xhosa']
diff --git a/vendor/gems/common_languages/lib/common_languages/language.rb b/vendor/gems/common_languages/lib/common_languages/language.rb
new file mode 100644
index 0000000..a6a3521
--- /dev/null
+++ b/vendor/gems/common_languages/lib/common_languages/language.rb
@@ -0,0 +1,19 @@
+# encoding: utf-8
+
+module CommonLanguages
+ class Language
+ attr_accessor :code, :name, :english_name, :rtl
+ def initialize(data)
+ @code = data[0].to_sym
+ @name = data[1]
+ @english_name = data[2] || data[1]
+ @rtl = data[3] === true
+ end
+ def rtl?
+ @rtl
+ end
+ def ==(l)
+ @code == l.code && @name = l.name
+ end
+ end
+end
diff --git a/vendor/gems/common_languages/lib/common_languages/version.rb b/vendor/gems/common_languages/lib/common_languages/version.rb
new file mode 100644
index 0000000..bb42aff
--- /dev/null
+++ b/vendor/gems/common_languages/lib/common_languages/version.rb
@@ -0,0 +1,3 @@
+module CommonLanguages
+ VERSION = "0.0.1"
+end
diff --git a/vendor/gems/common_languages/test/test_helper.rb b/vendor/gems/common_languages/test/test_helper.rb
new file mode 100644
index 0000000..339e22d
--- /dev/null
+++ b/vendor/gems/common_languages/test/test_helper.rb
@@ -0,0 +1,3 @@
+gem 'minitest'
+require 'minitest/autorun'
+require_relative '../lib/common_languages'
diff --git a/vendor/gems/common_languages/test/usage_test.rb b/vendor/gems/common_languages/test/usage_test.rb
new file mode 100644
index 0000000..dc545f4
--- /dev/null
+++ b/vendor/gems/common_languages/test/usage_test.rb
@@ -0,0 +1,33 @@
+require_relative 'test_helper'
+require 'i18n'
+
+class UsageTest < MiniTest::Test
+
+ def setup
+ CommonLanguages.reset
+ end
+
+ def test_available_codes_are_sorted
+ I18n.available_locales = ['pt', 'en', :de, :es]
+ assert_equal ['es', 'en', 'pt', 'de'], CommonLanguages.available_codes
+ end
+
+ def test_available
+ I18n.available_locales = [:en]
+ english = CommonLanguages::Language.new(CommonLanguages::DATA[2])
+ assert_equal english, CommonLanguages.get(:en)
+ assert_equal [english], CommonLanguages.available
+ end
+
+ def test_unique_codes
+ assert_equal CommonLanguages::DATA.size, CommonLanguages::languages.size
+ end
+
+ #def test_data
+ # I18n.available_locales = [:en, :de, :pt]
+ # CommonLanguages.available.each do |language|
+ # p [language.code, language.name, language.english_name, language.rtl?]
+ # end
+ #end
+
+end