Skip to content

Commit 7668e61

Browse files
committed
Config locale via I18n.locale
1 parent 8a7ed2f commit 7668e61

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

app.rb

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'sinatra'
22
require 'digest/md5'
33
require 'erector'
4+
require 'i18n'
5+
require 'i18n/backend/fallbacks'
46

57
#require 'wrong'
68
#include Wrong::D
@@ -22,15 +24,28 @@
2224
class InstallFest < Sinatra::Application # todo: use Sinatra::Base instead, with more explicit config
2325
include Erector::Mixin
2426

27+
# Set available locales in Array of Strings; this is also used when
28+
# checking availability in dynamic locale assigment, so must be as Strings.
29+
AVAILABLE_LOCALES = %w(en es)
30+
31+
configure do
32+
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
33+
I18n.load_path = Dir[File.join(settings.root, 'locales', '*.yml')]
34+
I18n.backend.load_translations
35+
36+
I18n.available_locales = AVAILABLE_LOCALES
37+
I18n.enforce_available_locales = true
38+
I18n.default_locale = :en
39+
end
40+
2541
def initialize
2642
super
2743
@here = File.expand_path(File.dirname(__FILE__))
2844
@default_sites = {en: "docs", es: "hola"}
29-
@default_locale = "en"
3045
end
3146

32-
attr_reader :here, :default_locale
33-
attr_writer :default_site, :default_locale
47+
attr_reader :here
48+
attr_writer :default_site
3449

3550
# todo: test
3651
# returns the most-specific hostname component, e.g. "foo" for "foo.example.com"
@@ -42,7 +57,7 @@ def default_site
4257
if host && sites.include?(site = subdomain)
4358
site
4459
else
45-
@default_sites[locale.to_sym]
60+
@default_sites[I18n.locale.to_sym] # no symbol DoS because it's whitelisted
4661
end
4762
end
4863

@@ -55,7 +70,7 @@ def site_dir
5570
end
5671

5772
def sites_dir
58-
Site.sites_dir(locale)
73+
Site.sites_dir(I18n.locale)
5974
end
6075

6176
def sites
@@ -68,11 +83,18 @@ def redirect_sites
6883
}
6984
end
7085

71-
def locale
86+
before do
87+
begin
88+
I18n.locale = dynamic_locale
89+
rescue I18n::InvalidLocale
90+
I18n.locale = I18n.default_locale
91+
end
92+
end
93+
94+
def dynamic_locale
7295
(params && (params[:locale] or params[:l])) or
73-
(host && subdomain =~ /^..$/ && subdomain) or # note: only allows 2-char locales for now -- should check against a list of locales
74-
(ENV['SITE_LOCALE']) or
75-
default_locale
96+
(host && AVAILABLE_LOCALES.include?(subdomain) && subdomain) or
97+
(ENV['SITE_LOCALE'])
7698
end
7799

78100
def src
@@ -103,7 +125,7 @@ def render_page
103125
doc_path: doc_path,
104126
back: params[:back],
105127
src: src,
106-
locale: locale,
128+
locale: I18n.locale,
107129
}
108130

109131
case ext

lib/site.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Site
33
@@project_root = File.dirname(@@here)
44

55
def self.sites_dir locale = "en"
6-
sites_dir = File.join(["sites", locale].compact)
6+
sites_dir = File.join(["sites", locale.to_s].compact)
77
File.expand_path(sites_dir, @@project_root)
88
end
99

spec/app_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ def get! *args
6868
describe "learns the locale from" do
6969
it "the locale parameter" do
7070
true_app.params = {locale: 'es'}
71-
assert { true_app.locale == 'es' }
71+
assert { true_app.dynamic_locale == 'es' }
7272
end
7373

7474
it "the l parameter" do
7575
true_app.params = {l: 'es'}
76-
assert { true_app.locale == 'es' }
76+
assert { true_app.dynamic_locale == 'es' }
7777
end
7878

7979
it "the subdomain" do
8080
true_app.request = Rack::Request.new({"HTTP_HOST" => "es.example.com"})
81-
assert { true_app.locale == 'es' }
81+
assert { true_app.dynamic_locale == 'es' }
8282
end
8383

8484
it "the SITE_LOCALE environment var" do
8585
begin
8686
ENV["SITE_LOCALE"] = "es"
87-
assert { true_app.locale == 'es' }
87+
assert { true_app.dynamic_locale == 'es' }
8888
ensure
8989
ENV["SITE_LOCALE"] = nil
9090
end

spec/site_syntax_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def app
1818
describe "#{site.name} pages..." do
1919
site.docs.each do |doc|
2020
it "renders #{doc.filename}" do
21-
path = "/#{site.name}/#{doc.name}"
21+
path = URI.escape "/#{site.name}/#{doc.name}"
2222
get path, locale: locale
2323
if (last_response.status != 200)
2424
errors = last_response.errors

spec/step_page_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "step_page"
44

55
describe StepPage do
6+
before { I18n.locale = :en }
67

78
# functional test -- brittle
89
it "renders a step file" do

spec/step_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "step_page"
44

55
describe Step do
6+
before { I18n.locale = :en }
67

78
def to_html nokogiri_node
89
nokogiri_node.serialize(:save_with => 0).chomp

0 commit comments

Comments
 (0)