Skip to content

Commit 900f3c7

Browse files
alexchdshafik
authored andcommitted
use subdomain, param, or environment variable to specify locale
1 parent 34d5fe9 commit 900f3c7

8 files changed

+59
-45
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PHPBridge by standing on their shoulders and building up.
1212

1313
This is a Sinatra app, deployed at <http://docs.railsbridge.org>. The PHPBridge documentation project is home to a few subprojects, including the PHPBridge installfest instructions, which leads students through the various complicated setup instructions for getting PHP, MySQL, Git, etc. installed on their computer (whatever combination of computer, OS, and version they happened to bring to the workshop!), as well as the PHPBridge workshop "Suggestotron" curriculum.
1414

15-
Each subproject (a "site") comprises files stored under the "sites" directory; for instance, the installfest instructions are located at ROOT/sites/installfest, while the intro PHP curriculum can be found under ROOT/sites/intro-to-php.
15+
Each subproject (a "site") comprises files stored under the "sites" directory; for instance, the installfest instructions are located at ROOT/sites/en/installfest, while the intro PHP curriculum can be found under ROOT/sites/en/intro-to-php.
1616

1717
These files can be in any of these formats:
1818

@@ -31,9 +31,18 @@ These files can be in any of these formats:
3131
If the above fails (say, because `rerun` doesn't work on your system), try
3232

3333
rackup
34-
34+
3535
Then open <http://localhost:9292> in a web browser, and verify that you can navigate the installfest slides.
3636

37+
## Locales
38+
39+
To serve sites from "sites/en", use `rake run` or a vanilla deploy.
40+
41+
To server sites from another locale (say, "es" or Spanish)...
42+
* Locally, use the SITE_LOCALE environment variable: `SITE_LOCALE=es rake run`
43+
* On a server, make the server respond to a locale subdomain: `http://es.railsbridge.org`
44+
* Or to temporarily test, use a `locale` or `l` parameter: `http://docs.railsbridge.org/?l=es` (note that in this mode, links are not rewritten, so if they fail you will have to manually add the parameter again)
45+
3746
## Contributing
3847

3948
Check out [CONTRIBUTING.md](CONTRIBUTING.md) to see how to join our [list of contributors](https://github.com/railsbridge/docs/contributors)!

app.rb

+14-18
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require 'digest/md5'
33
require 'erector'
44

5-
# require 'wrong'
6-
# include Wrong::D
5+
#require 'wrong'
6+
#include Wrong::D
77

88
here = File.expand_path File.dirname(__FILE__)
99
lib = File.expand_path "#{here}/lib"
@@ -25,11 +25,11 @@ class InstallFest < Sinatra::Application # todo: use Sinatra::Base instead, wi
2525
def initialize
2626
super
2727
@here = File.expand_path(File.dirname(__FILE__))
28-
@default_site = "docs"
28+
@default_sites = {en: "docs", es: "hola"}
2929
@default_locale = "en" # nil for English # todo: make a cleaner way to switch default locales
3030
end
3131

32-
attr_reader :here
32+
attr_reader :here, :default_locale
3333
attr_writer :default_site, :default_locale
3434

3535
# todo: test
@@ -42,7 +42,7 @@ def default_site
4242
if host && sites.include?(site = subdomain)
4343
site
4444
else
45-
@default_site
45+
@default_sites[locale.to_sym]
4646
end
4747
end
4848

@@ -67,11 +67,12 @@ def redirect_sites
6767
'curriculum' => 'intro-to-php'
6868
}
6969
end
70-
70+
7171
def locale
72-
(params && params[:locale]) or
72+
(params && (params[:locale] or params[:l])) or
7373
(host && subdomain =~ /^..$/ && subdomain) or # note: only allows 2-char locales for now -- should check against a list of locales
74-
@default_locale
74+
(ENV['SITE_LOCALE']) or
75+
default_locale
7576
end
7677

7778
def src
@@ -102,6 +103,7 @@ def render_page
102103
doc_path: doc_path,
103104
back: params[:back],
104105
src: src,
106+
locale: locale,
105107
}
106108

107109
case ext
@@ -135,13 +137,6 @@ def render_page
135137

136138
before do
137139
expires 3600, :public
138-
139-
if request.path =~ /^\/es(.*)/
140-
params[:locale] = "es"
141-
request.env["PATH_INFO"] = $1
142-
# p request.env["PATH_INFO"]
143-
# p request.params[:locale]
144-
end
145140
end
146141

147142
get '/favicon.ico' do
@@ -151,15 +146,16 @@ def render_page
151146
get "/" do
152147
redirect "/#{default_site}/"
153148
end
154-
149+
155150
get "/:site/:name/src" do
156151
begin
157152
RawPage.new(
158153
site_name: params[:site],
159154
page_name: params[:name],
160155
doc_title: doc_path.split('/').last,
161156
doc_path: doc_path,
162-
src: src
157+
src: src,
158+
locale: locale,
163159
).to_html
164160
rescue Errno::ENOENT => e
165161
p e
@@ -184,7 +180,7 @@ def render_page
184180
# remove any extraneous slash from otherwise well-formed page URLs
185181
redirect request.fullpath.chomp('/')
186182
end
187-
183+
188184
get "/:site/:name" do
189185
site_name = params[:site]
190186
if redirect_sites[site_name]

lib/doc_page.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
require 'html5_page'
77

88
class DocPage < Html5Page
9-
needs :site_name, :doc_title, :doc_path, :page_name, :src
9+
needs :site_name, :doc_title, :doc_path, :page_name, :src, :locale
1010
needs :back => nil
1111
attr_reader :site_name, :doc_title, :page_name, :src
1212

@@ -62,7 +62,7 @@ def file_name
6262
end
6363

6464
def git_url
65-
"https://github.com/phpbridge/docs/blob/master/sites/#{@site_name}/#{file_name}"
65+
"https://github.com/phpbridge/docs/blob/master/sites/#{@locale}/#{@site_name}/#{file_name}"
6666
end
6767

6868
def src_url
@@ -98,7 +98,7 @@ def body_content
9898

9999
li(class: "dropdown") {
100100
a("curriculum", href: "#", class: "dropdown-toggle", "data-toggle" => "dropdown")
101-
widget SiteIndex, site_name: site_title
101+
widget SiteIndex, site_name: site_name, locale: @locale
102102
}
103103

104104
top_links.each do |top_link|

lib/site_index.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
class SiteIndex < Erector::Widget
2-
needs :site_name
2+
needs :site_name, :locale
33
attr_accessor :site_name
44

5-
def initialize(options)
6-
self.site_name = options[:site_name]
7-
end
8-
95
def sites
106
return @sites if @sites
11-
@sites = Dir.glob("#{Site.sites_dir}/**").map { |filename| File.basename(filename) }.sort
7+
@sites = Dir.glob("#{Site.sites_dir(@locale)}/**").map { |filename| File.basename(filename) }.sort
128
end
139

1410
def site_link site

spec/app_spec.rb

+23-12
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ class ::InstallFest
1111
def dup *args
1212
@@latest_instance = super
1313
end
14+
1415
def self.latest_instance
1516
@@latest_instance
1617
end
1718
end
1819

1920
describe InstallFest do
20-
include Rack::Test::Methods
21+
include Rack::Test::Methods # see http://www.sinatrarb.com/testing.html
2122

2223
def app
2324
InstallFest
2425
end
2526

2627
# find the actual InstallFest app, discarding Rack middleware
27-
def true_app
28+
def true_app
2829
InstallFest.latest_instance
2930
end
3031

@@ -41,7 +42,7 @@ def get! *args
4142
end
4243

4344
it "redirects / to the default site" do
44-
get! "/"
45+
get! "/"
4546
assert { last_request.path == "/docs/" }
4647
end
4748

@@ -63,22 +64,31 @@ def get! *args
6364
# note: I'd rather pass settings into the constructor, but Sinatra uses that interface (for a downstream app)
6465

6566
before { get '/' }
66-
67-
it "accepts default_site via setter" do
68-
true_app.default_site = "intro-to-rails"
69-
assert { true_app.default_site == "intro-to-rails" }
70-
end
71-
67+
7268
describe "learns the locale from" do
7369
it "the locale parameter" do
7470
true_app.params = {locale: 'es'}
7571
assert { true_app.locale == 'es' }
7672
end
7773

74+
it "the l parameter" do
75+
true_app.params = {l: 'es'}
76+
assert { true_app.locale == 'es' }
77+
end
78+
7879
it "the subdomain" do
7980
true_app.request = Rack::Request.new({"HTTP_HOST" => "es.example.com"})
8081
assert { true_app.locale == 'es' }
8182
end
83+
84+
it "the SITE_LOCALE environment var" do
85+
begin
86+
ENV["SITE_LOCALE"] = "es"
87+
assert { true_app.locale == 'es' }
88+
ensure
89+
ENV["SITE_LOCALE"] = nil
90+
end
91+
end
8292
end
8393
end
8494

@@ -88,14 +98,15 @@ def get! *args
8898
follow_redirect! while last_response.redirect?
8999
assert { last_request.path == "/docs/" }
90100
end
91-
101+
92102
describe "in the 'es' locale" do
93-
it "uses the 'es' subdir as the sites_dir" do
103+
it "uses the 'es' subdir as the sites_dir" do
94104
get "/", locale: "es"
95-
105+
96106
es_dir = File.expand_path(File.join(__FILE__, "..", "..", "sites", "es"))
97107
assert { true_app.sites_dir == es_dir }
98108
end
109+
99110
end
100111

101112
describe "page headers" do

spec/markdown_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
site_name: "greetings",
2222
page_name: 'hello',
2323
doc_title: "Hello",
24-
doc_path: "/tmp/hello.step"
24+
doc_path: "/tmp/hello.step",
25+
locale: "en"
2526
)
2627
html_doc = Nokogiri.parse(page.to_html)
2728
main_html = html_doc.css("main").first.serialize(:save_with => 0).chomp

spec/site_index_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
describe SiteIndex do
77
before :all do
8-
@site_index = SiteIndex.new(site_name: 'frontend')
8+
@site_index = SiteIndex.new(site_name: 'frontend', locale: 'en')
99
end
1010

1111
it "lists all sites in the /sites/ directory" do

spec/step_page_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
# functional test -- brittle
88
it "renders a step file" do
99
BigCheckbox.number = 1
10-
10+
1111
src = "step 'hello'"
1212
page = StepPage.new(src: src,
1313
site_name: "greetings",
1414
page_name: 'hello',
1515
doc_title: "Hello",
16-
doc_path: "/tmp/hello.step"
16+
doc_path: "/tmp/hello.step",
17+
locale: "en"
1718
)
1819
html_doc = Nokogiri.parse(page.to_html)
1920
main_html = html_doc.css("main").first.serialize(:save_with => 0).chomp

0 commit comments

Comments
 (0)