Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added i18n to the merb book

  • Loading branch information...
commit 331383c66c1aa2da7eea455f2de9fb72e6f1e793 1 parent f924deb
@mattetti authored
Showing with 849 additions and 15 deletions.
  1. 0  .gitignore
  2. +8 −2 README.markdown
  3. +3 −0  app/controllers/application.rb
  4. +10 −2 app/controllers/pages.rb
  5. +6 −0 app/helpers/global_helpers.rb
  6. +8 −2 app/views/exceptions/not_found.html.erb
  7. +1 −1  app/views/layout/application.html.erb
  8. 0  book-content/{ → en}/0-front-matter/0-foreword.markdown
  9. 0  book-content/{ → en}/0-front-matter/1-preface.markdown
  10. 0  book-content/{ → en}/0-front-matter/2-contributors.markdown
  11. 0  book-content/{ → en}/1-introduction/1-ruby.markdown
  12. 0  book-content/{ → en}/1-introduction/2-merb.markdown
  13. 0  book-content/{ → en}/1-introduction/3-datamapper.markdown
  14. 0  book-content/{ → en}/1-introduction/4-rspec.markdown
  15. +1 −1  app/views/pages/index.html.erb → book-content/en/toc.markdown
  16. +28 −0 book-content/fr/toc.markdown
  17. +7 −3 config/init.rb
  18. +3 −3 config/router.rb
  19. BIN  gems/cache/merb_babel-0.1.0.gem
  20. +20 −0 gems/gems/merb_babel-0.1.0/LICENSE
  21. +1 −0  gems/gems/merb_babel-0.1.0/README
  22. +61 −0 gems/gems/merb_babel-0.1.0/Rakefile
  23. +5 −0 gems/gems/merb_babel-0.1.0/TODO
  24. +49 −0 gems/gems/merb_babel-0.1.0/lib/merb_babel.rb
  25. +29 −0 gems/gems/merb_babel-0.1.0/lib/merb_babel/core_ext.rb
  26. +29 −0 gems/gems/merb_babel-0.1.0/lib/merb_babel/m_i18n.rb
  27. +118 −0 gems/gems/merb_babel-0.1.0/lib/merb_babel/m_l10n.rb
  28. +76 −0 gems/gems/merb_babel-0.1.0/lib/merb_babel/m_locale.rb
  29. +6 −0 gems/gems/merb_babel-0.1.0/lib/merb_babel/merbtasks.rb
  30. +13 −0 gems/gems/merb_babel-0.1.0/spec/lang/en-UK.yml
  31. +13 −0 gems/gems/merb_babel-0.1.0/spec/lang/en-US.yml
  32. +16 −0 gems/gems/merb_babel-0.1.0/spec/lang/en.yml
  33. +49 −0 gems/gems/merb_babel-0.1.0/spec/m_i18n_spec.rb
  34. +51 −0 gems/gems/merb_babel-0.1.0/spec/m_l10n_spec.rb
  35. +127 −0 gems/gems/merb_babel-0.1.0/spec/merb_babel_spec.rb
  36. +14 −0 gems/gems/merb_babel-0.1.0/spec/other_lang_dir/fr.yml
  37. +49 −0 gems/gems/merb_babel-0.1.0/spec/spec_helper.rb
  38. +33 −0 gems/specifications/merb_babel-0.1.0.gemspec
  39. +0 −1  log/merb.4000.pid
  40. +15 −0 public/stylesheets/master.css
View
0  .gitignore
No changes.
View
10 README.markdown
@@ -16,11 +16,17 @@ A static HTML and a PDF version will soon be available online.
* Modify, fix, add content
* send me a pull request
-For the moment, the content of the book is located inside ./book-content/ and is organized by chapters and pages.
+For the moment, the content of the book is located inside ./book-content/ and is organized by languages, chapters and pages.
Chapters and pages must be lowercase and start by a digit followed by a dash and the chapter/page title without spaces or any non ascii characters.
-All content files are markdown files and the merb app uses [maruku](http://maruku.rubyforge.org) to render the markdown files. Check the [supported syntax](http://maruku.rubyforge.org/markdown_syntax.html) or check ./book-content/1-introduction/1-ruby.markdown for a good example of how to use images, quotes and css selectors.
+All content files are markdown files and the merb app uses [maruku](http://maruku.rubyforge.org) to render the markdown files. Check the [supported syntax](http://maruku.rubyforge.org/markdown_syntax.html) or check ./book-content/en/1-introduction/1-ruby.markdown for a good example of how to use images, quotes and css selectors.
+
+The book supports multiple languages, each language needs to replicate the English folder structure with the same file names.
+
+If you are copying a page or chapter to your language folder and want to mark a section of your text as requiring translation, just add the to-translate class to the css selector of your content as shown below:
+
+ {: .to-translate}
## Legal
View
3  app/controllers/application.rb
@@ -1,2 +1,5 @@
class Application < Merb::Controller
+
+ before :set_language
+
end
View
12 app/controllers/pages.rb
@@ -1,7 +1,11 @@
class Pages < Application
def index
- render
+ page_file = find_toc
+ raise NotFound unless page_file
+
+ text = File.open(page_file).read
+ render Maruku::new(text).to_html
end
def show
@@ -18,7 +22,11 @@ def show
private
# If no page name is being passed, the first page is being returned
def find_page_file(chapter, page_name, format="markdown")
- Dir["#{Merb.root}/book-content/*-#{chapter}/*-#{page_name||"*"}.#{format}"].entries.first
+ Dir["#{Merb.root}/book-content/#{language}/*-#{chapter}/*-#{page_name||"*"}.#{format}"].entries.first
+ end
+
+ def find_toc(format="markdown")
+ Dir["#{Merb.root}/book-content/#{language}/toc.#{format}"].entries.first
end
end
View
6 app/helpers/global_helpers.rb
@@ -1,5 +1,11 @@
module Merb
module GlobalHelpers
# helpers defined here available to all views.
+
+ def same_url_but_in_english
+ named_route = params[:chapter] ? :page : :toc
+ url(named_route, :language => 'en', :chapter => params[:chapter], :page_name => params[:page_name])
+ end
+
end
end
View
10 app/views/exceptions/not_found.html.erb
@@ -1,3 +1,9 @@
-<div class="error">we couldn't find the page you requested, you might want to check on the index</div>
+<div class="error">The page you requested doesn't seem to exist</div>
-<%= link_to "Table of Content", url(:toc) %>
+<% if language != 'en' -%>
+ <p>You required a page with the language code <strong><%= language %></strong>, it might not be available yet, click on the following link to see the requested page in English.</p>
+
+ <p><%= link_to "Same page but in English", same_url_but_in_english %></p>
+<% end -%>
+
+Otherwise, check the <%= link_to "Table of Contents", url(:toc, :language => language) %>
View
2  app/views/layout/application.html.erb
@@ -11,7 +11,7 @@
<div id="book-title"><a href="/">The Merb Open Source Book</a></div>
<div id="book">
- <div class="toc-link"><a href="/">table of contents</a></div>
+ <div class="toc-link"><a href="<%= url(:toc, :language => language) %>">table of contents</a></div>
<div id="table-of-contents">
<%= catch_content :toc %>
View
0  book-content/0-front-matter/0-foreword.markdown → book-content/en/0-front-matter/0-foreword.markdown
File renamed without changes
View
0  book-content/0-front-matter/1-preface.markdown → book-content/en/0-front-matter/1-preface.markdown
File renamed without changes
View
0  book-content/0-front-matter/2-contributors.markdown → ...content/en/0-front-matter/2-contributors.markdown
File renamed without changes
View
0  book-content/1-introduction/1-ruby.markdown → book-content/en/1-introduction/1-ruby.markdown
File renamed without changes
View
0  book-content/1-introduction/2-merb.markdown → book-content/en/1-introduction/2-merb.markdown
File renamed without changes
View
0  book-content/1-introduction/3-datamapper.markdown → book-content/en/1-introduction/3-datamapper.markdown
File renamed without changes
View
0  book-content/1-introduction/4-rspec.markdown → book-content/en/1-introduction/4-rspec.markdown
File renamed without changes
View
2  app/views/pages/index.html.erb → book-content/en/toc.markdown
@@ -1,4 +1,4 @@
-<h1>Table of Contents</h1>
+#Table of Contents
<div id="intro">
<p>Welcome to the Merb Open Source Book. The purpose of this book is simple: guide beginner and advance users through the Merb Framework.</p>
View
28 book-content/fr/toc.markdown
@@ -0,0 +1,28 @@
+#Table des matières
+
+<div id="intro" class="to-translate">
+ <p>Welcome to the Merb Open Source Book. The purpose of this book is simple: guide beginner and advance users through the Merb Framework.</p>
+ <p>In all honesty, This book won't answer all your questions about Merb, Ruby and unfortunately won't cover all the use case. However will the help of the Ruby Community, we will be able to cover most of what you need to use Merb on a daily basis.</p>
+ <p>If you are learning through this material, or if already use Merb frequently, <strong>please consider helping improve the content of this book</strong></p>
+</div>
+
+<ul id="toc" class="to-translate">
+ <li>
+ <a href="/front-matter">Front Matter</a>
+ <ul>
+ <li><a href="/front-matter/foreword">Foreword by Yehuda Katz</a></li>
+ <li><a href="/front-matter/preface">Preface par Matt Aimonetti</a></li>
+ <li><a href="/front-matter/contributors">Contributeurs</a></li>
+ </ul>
+ </li>
+ <li>
+ <a href="/introduction">Introduction</a>
+ <ul>
+ <li><a href="/introduction/ruby">Ruby</a></li>
+ <li><a href="/introduction/merb">Merb web framework</a></li>
+ <li><a href="/introduction/datamapper">DataMapper ORM</a></li>
+ <li><a href="/introduction/rspec">RSpec testing framework</a></li>
+ </ul>
+ </li>
+
+</ul>
View
10 config/init.rb
@@ -5,6 +5,7 @@
dependency "maruku", ">= 0.5.9"
dependency "merb-assets", "~> 1.0"
+dependency "merb_babel", ">= 0.1.0"
# use_orm :none
use_test :rspec
@@ -12,15 +13,18 @@
Merb::Config.use do |c|
c[:use_mutex] = false
- #c[:session_store] = 'cookie' # can also be 'memory', 'memcache', 'container', 'datamapper
+ c[:session_store] = 'cookie' # can also be 'memory', 'memcache', 'container', 'datamapper
# cookie session store configuration
- #c[:session_secret_key] = '8cea7e76dcb6c428521f22c4943b4e69430a85fa' # required for cookie session store
- #c[:session_id_key] = '_merb-book_session_id' # cookie session id key, defaults to "_session_id"
+ c[:session_secret_key] = '8cea7e76dcb6c428521f22c4943b4e69430a85fa' # required for cookie session store
+ c[:session_id_key] = '_merb-book_session_id' # cookie session id key, defaults to "_session_id"
end
Merb::BootLoader.before_app_loads do
# This will get executed after dependencies have been loaded but before your app's classes have loaded.
+ Merb::Plugins.config[:Merb_babel] = {
+ :default_language => 'en'
+ }
end
Merb::BootLoader.after_app_loads do
View
6 config/router.rb
@@ -1,9 +1,9 @@
Merb.logger.info("Compiling routes...")
Merb::Router.prepare do
- match('/table-of-content').to(:controller => 'pages', :action => 'index').name(:toc)
- match("/:chapter(/:page_name)").to(:controller => 'pages', :action => 'show').name(:page)
+ match('(/:language)/table-of-contents', :language => /^[a-z]{2}$/).to(:controller => 'pages', :action => 'index').name(:toc)
+ match("(/:language)/:chapter(/:page_name)", :language => /^[a-z]{2}$/, :chapter => /.{3,}/).to(:controller => 'pages', :action => 'show').name(:page)
#default_routes
- match('/').to(:controller => 'pages', :action => 'index')
+ match('/(:language)', :language => /^[a-z]{2}$/).to(:controller => 'pages', :action => 'index')
end
View
BIN  gems/cache/merb_babel-0.1.0.gem
Binary file not shown
View
20 gems/gems/merb_babel-0.1.0/LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Matt Aimonetti
+
+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.
View
1  gems/gems/merb_babel-0.1.0/README
@@ -0,0 +1 @@
+see README.markdown
View
61 gems/gems/merb_babel-0.1.0/Rakefile
@@ -0,0 +1,61 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+require 'rubygems/specification'
+require 'date'
+require "spec/rake/spectask"
+
+PLUGIN = "merb_babel"
+NAME = "merb_babel"
+GEM_VERSION = "0.1.0"
+AUTHOR = "Matt Aimonetti"
+EMAIL = "mattaimonetti@gmail.com"
+HOMEPAGE = "http://github.com/mattetti/merb_babel/"
+SUMMARY = "Merb plugin that provides simple localization/internationalisation"
+
+spec = Gem::Specification.new do |s|
+ s.name = NAME
+ s.version = GEM_VERSION
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
+ s.summary = SUMMARY
+ s.description = s.summary
+ s.author = AUTHOR
+ s.email = EMAIL
+ s.homepage = HOMEPAGE
+ s.add_dependency('merb-core', '~> 1.0.0')
+ s.require_path = 'lib'
+ s.autorequire = PLUGIN
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+desc "install the plugin locally"
+task :install => [:package] do
+ sh %{sudo gem install pkg/#{NAME}-#{VERSION} --no-update-sources}
+end
+
+desc "create a gemspec file"
+task :make_spec do
+ File.open("#{GEM}.gemspec", "w") do |file|
+ file.puts spec.to_ruby
+ end
+end
+
+namespace :jruby do
+
+ desc "Run :package and install the resulting .gem with jruby"
+ task :install => :package do
+ sh %{#{SUDO} jruby -S gem install pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
+ end
+
+end
+
+desc "Run the specs"
+Spec::Rake::SpecTask.new("specs") do |t|
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.spec_files = Dir["spec/**/*_spec.rb"].sort
+end
View
5 gems/gems/merb_babel-0.1.0/TODO
@@ -0,0 +1,5 @@
+TODO:
+Fix LICENSE with your name
+Fix Rakefile with your name and contact info
+Add your code to lib/merb_abel.rb
+Add your Merb rake tasks to lib/merb_abel/merbtasks.rb
View
49 gems/gems/merb_babel-0.1.0/lib/merb_babel.rb
@@ -0,0 +1,49 @@
+require File.join(File.dirname(__FILE__) / "merb_babel" / "core_ext")
+
+# make sure we're running inside Merb
+if defined?(Merb::Plugins)
+
+ # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
+ Merb::Plugins.config[:merb_babel] = {
+ :default_locale => 'en-US',
+ :default_language => 'en',
+ :default_country => 'US',
+ :localization_dirs => ["#{Merb.root}/lang"]
+ }
+
+ require File.join(File.dirname(__FILE__) / "merb_babel" / "m_locale")
+ require File.join(File.dirname(__FILE__) / "merb_babel" / "m_l10n")
+ require File.join(File.dirname(__FILE__) / "merb_babel" / "m_i18n")
+
+ Merb::BootLoader.before_app_loads do
+ # require code that must be loaded before the application
+ module Merb
+ module GlobalHelpers
+ include ML10n
+
+ # Used to translate words using localizations
+ def babelize(key, *args)
+ options = args.first
+ options ||= {}
+ options.merge!(:key => key)
+ options.merge!(:language => language) unless options.has_key?(:language)
+ options.merge!(:country => country) unless options.has_key?(:country)
+ MI18n.lookup(options)
+ end
+ alias :translate :babelize
+ alias :t :babelize
+ alias :_ :babelize
+
+ end
+ end
+
+ Merb::Controller.send(:include, MLocale)
+ Merb::Controller.send(:include, ML10n)
+ end
+
+ Merb::BootLoader.after_app_loads do
+ # code that can be required after the application loads
+ end
+
+ Merb::Plugins.add_rakefiles "merb_babel/merbtasks"
+end
View
29 gems/gems/merb_babel-0.1.0/lib/merb_babel/core_ext.rb
@@ -0,0 +1,29 @@
+module MerbBabel
+
+ module HashExtension
+
+ def symbolize_keys
+ inject({}) do |options, (key, value)|
+ options[key.to_sym || key] = value
+ options
+ end
+ end
+
+ def symbolize_keys!
+ self.replace(self.symbolize_keys)
+ end
+
+ def valid_localization_hash?
+ self.has_key?(:mloc_language_code)
+ end
+
+ def localization_hash_with_locale?
+ self.has_key?(:mloc_country_code)
+ end
+
+ end
+
+end
+
+
+Hash.send :include, MerbBabel::HashExtension
View
29 gems/gems/merb_babel-0.1.0/lib/merb_babel/m_i18n.rb
@@ -0,0 +1,29 @@
+module MI18n
+
+ def self.lookup(options)
+ key = options[:key]
+ language = options[:language]
+ country = options[:country]
+ raise ArgumentError, "You need to pass a language reference" unless language
+ raise ArgumentError, "You need to pass a localization key" unless key
+ raise ArgumentError, "language: #{language} not found" unless ML10n.localizations[language]
+
+ full_location = nil
+ full_location = lookup_with_full_locale(key, language, country) if country
+
+ if full_location
+ return full_location
+ else
+ return ML10n.localizations[language][key]
+ end
+ end
+
+ def self.lookup_with_full_locale(key, language, country)
+ if ML10n.localizations.has_key?(language)
+ ML10n.localizations[language].has_key?(country) ? ML10n.localizations[language][country][key] : nil
+ else
+ nil
+ end
+ end
+
+end
View
118 gems/gems/merb_babel-0.1.0/lib/merb_babel/m_l10n.rb
@@ -0,0 +1,118 @@
+module ML10n
+
+ # TODO add a mutex for when we load the localizations, in case people want to load the localizations
+ # during runtime
+
+ def self.localizations
+ @@localizations ||= {}
+ end
+
+ # all localizations are saved in this class variable
+ # localizations are namespaced using the language or locale they belong to
+ #
+ # for instance:
+ # localizations['en'][:right] => 'right'
+ # localizations['en'][:left] => 'left'
+ # localizations['en']['US'][:greeting] => 'Howdie'
+ # localizations['en']['AU'][:greeting] => "Good'ay"
+ #
+ # locales, including languages and countries use string keys while localization keys themselves are symbols
+ def localizations
+ ML10n.localizations
+ end
+
+ # files containing localizations
+ def localization_files
+ @@localization_files ||= find_localization_files
+ end
+
+ # locations to look for localization files
+ def localization_dirs
+ @@localization_dirs ||= Merb::Plugins.config[:merb_babel][:localization_dirs].dup
+ end
+
+ # add a dir to look for localizations
+ def add_localization_dir(dir_path)
+ return localization_dirs if dir_path.empty?
+ unless localization_dirs.include?(dir_path)
+ localization_dirs << dir_path
+ reload_localization_files!
+ end
+ return localization_dirs
+ end
+
+ def load_localization!
+
+ # look for localization files directly just in case new files were added
+ reset_localization_files!
+ find_localization_files.each do |l_file|
+ begin
+ l_hash = YAML.load_file(l_file).symbolize_keys
+ rescue Exception => e
+ # might raise a real error here in the future
+ p e.inspect
+ else
+ load_localization_hash(l_hash)
+ end
+ end
+
+ end
+
+ # go through the localization dirs and find the localization files
+ def find_localization_files
+ l_files = []
+ localization_dirs.map do |l_dir|
+ Dir["#{l_dir}/*", '*.{yml,yaml}'].each do |file|
+ l_files << file unless l_files.include?(file)
+ end
+ end
+ return l_files
+ end
+
+ # reset the localization dirs and files to the plugin config
+ # careful when using this method since it will remove any localization files
+ # you loaded after the plugin started
+ #
+ # note that it won't clear the localization already loaded in memory
+ def reset_localization_files_and_dirs!
+ reset_localization_dirs!
+ reset_localization_files!
+ end
+
+ def reset_localization_dirs!
+ @@localization_dirs = nil
+ end
+
+ def reset_localization_files!
+ @@localization_files = nil
+ find_localization_files
+ end
+
+ def reload_localization_files!
+ reset_localization_files!
+ find_localization_files
+ end
+
+ # localization helper
+ def self.localize(key, *options)
+ MI18n.localize(options.merge({:locale => locale}))
+ end
+
+ protected
+
+ def load_localization_hash(l_hash)
+ if l_hash.valid_localization_hash?
+ language = l_hash[:mloc_language_code]
+ if l_hash.localization_hash_with_locale?
+ country = l_hash[:mloc_country_code]
+ # load localization under the full locale namespace
+ localizations[language] ||= {}
+ (localizations[language][country] ||= {}).merge!(l_hash)
+ else
+ # load generic language localization
+ (localizations[language] ||= {}).merge!(l_hash)
+ end
+ end
+ end
+
+end
View
76 gems/gems/merb_babel-0.1.0/lib/merb_babel/m_locale.rb
@@ -0,0 +1,76 @@
+# The MLocale module helps you set up a locale, language, country
+# You don't have to use a locale, in some cases you might just want to use the language
+module MLocale
+
+ # A locale is made of a language + country code, such as en-UK or en-US
+ def locale
+ request.env[:locale] || params[:locale] || (session ? session[:locale] : nil) || default_locale
+ end
+
+ # Many people don't care about locales, they might just want to use languages instead
+ def language
+ request.env[:language] || params[:language] || language_from_locale || (session ? session[:language] : nil) || default_language
+ end
+
+ # The country is used when localizing currency or time
+ def country
+ request.env[:country] || params[:country] || country_from_locale || (session ? session[:country] : nil) || default_country
+ end
+
+ # Extract the language from the locale
+ def language_from_locale
+ if request.env[:locale] && request.env[:locale] =~ locale_regexp
+ language, country = request.env[:locale].match(locale_regexp).captures
+ return language
+ else
+ return nil
+ end
+ end
+
+ # Extract the country from the locale
+ def country_from_locale
+ request.env[:locale] ? request.env[:locale][3..5].upcase : nil
+ end
+
+ # Defaults set in the plugin settings
+ # You can change the default settings by overwriting
+ # the Merb::Plugins.config[:merb_abel] hash in your settings
+ #
+ def default_locale
+ Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_locale] : nil
+ end
+
+ def default_language
+ Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_language] : nil
+ end
+
+ def default_country
+ Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_country] : nil
+ end
+ #
+ #### end of defaults
+
+ protected
+
+ def locale_regexp
+ /(.+)\-([a-z]{2})/i
+ end
+
+ # takes a locale as in fr-FR or en-US
+ def set_locale
+
+ if locale =~ locale_regexp
+ language, country = locale.match(locale_regexp).captures
+ # Set the locale, language and country
+ language = language.downcase
+ country = country.upcase
+ request.env[:locale] = "#{language}-#{country}"
+ end
+
+ end
+
+ def set_language
+ request.env[:language] = language.downcase
+ end
+
+end
View
6 gems/gems/merb_babel-0.1.0/lib/merb_babel/merbtasks.rb
@@ -0,0 +1,6 @@
+namespace :merb_babel do
+ desc "Do something for merb_babel"
+ task :default do
+ puts "merb_babel doesn't do anything"
+ end
+end
View
13 gems/gems/merb_babel-0.1.0/spec/lang/en-UK.yml
@@ -0,0 +1,13 @@
+#################################################
+# en-UK only localization
+#
+# only add localization specific to this locale
+# other comman localizations must go in en.yml
+#################################################
+# required
+mloc_language_code: en
+# required for a locale localization file
+mloc_country_code: UK
+#################################################
+
+greetings: Heya
View
13 gems/gems/merb_babel-0.1.0/spec/lang/en-US.yml
@@ -0,0 +1,13 @@
+#################################################
+# en-US only localization
+#
+# only add localization specific to this locale
+# other comman localizations must go in en.yml
+#################################################
+# required
+mloc_language_code: en
+# required for a locale localization file
+mloc_country_code: US
+#################################################
+
+greetings: Howdie
View
16 gems/gems/merb_babel-0.1.0/spec/lang/en.yml
@@ -0,0 +1,16 @@
+#################################################
+# English localization
+#
+# localization specific must go in their own file
+# see en-US.yml for instance
+#################################################
+mloc_language_code: en
+# Optional, the following key has the name of the localized language in its ow language.
+mloc_language_name: English
+# mloc_country:
+#################################################
+
+
+right: right
+left: left
+greetings: Hello
View
49 gems/gems/merb_babel-0.1.0/spec/m_i18n_spec.rb
@@ -0,0 +1,49 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe '#babelize' do
+
+ before(:each) do
+ Merb::Controller.send :include, Merb::GlobalHelpers
+ @c = dispatch_to(TestController, :index)
+ @c.add_localization_dir(File.expand_path(File.dirname(__FILE__) + "/lang"))
+ @c.add_localization_dir(File.expand_path(File.dirname(__FILE__) + "/other_lang_dir"))
+ @c.load_localization!
+ end
+
+ it "should babelize a word in English " do
+ @c.locale.should == 'en-US'
+ @c.language.should == 'en'
+ @c.country.should == 'US'
+ @c.babelize(:left).should == 'left'
+ end
+
+ it "should translate a word in English" do
+ @c.t(:left).should == 'left'
+ @c.translate(:left).should == 'left'
+ @c._(:left).should == 'left'
+ end
+
+ it "should translate a word in american English" do
+ @c.t(:greetings).should == 'Howdie'
+ end
+
+ it "should translate a word in british English" do
+ @c.request.env[:locale] = 'en-UK'
+ @c.t(:greetings).should == 'Heya'
+ end
+
+ it "should translate a word in French" do
+ @c.request.env[:language] = 'fr'
+ @c.t(:greetings).should == 'Salut'
+ end
+
+ it "should translate passing the full locale" do
+ @c.t(:greetings, :language => 'en', :country => 'UK').should == 'Heya'
+ @c.t(:greetings, :language => 'en', :country => 'US').should == 'Howdie'
+ end
+
+ it "should translate passing the language" do
+ @c.t(:greetings, :language => 'fr').should == 'Salut'
+ end
+
+end
View
51 gems/gems/merb_babel-0.1.0/spec/m_l10n_spec.rb
@@ -0,0 +1,51 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "ML10n" do
+ include ML10n
+
+ before(:each) do
+ @lang_test_path = File.expand_path(File.dirname(__FILE__) + "/lang")
+ @lang_test_path_2 = File.expand_path(File.dirname(__FILE__) + "/other_lang_dir")
+ reset_localization_files_and_dirs!
+ end
+
+ it "should have a list of localization directories" do
+ localization_dirs.should == Merb::Plugins.config[:merb_babel][:localization_dirs]
+ end
+
+ it "should be able to add a new localization directory" do
+ add_localization_dir(@lang_test_path)
+ localization_dirs.include?(@lang_test_path)
+ end
+
+ it "should have a list of localization source files" do
+ localization_files.should == []
+ add_localization_dir(@lang_test_path)
+ localization_files.include?("#{@lang_test_path}/en.yml").should be_true
+ localization_files.include?("#{@lang_test_path}/en-US.yml").should be_true
+ end
+
+ it "should load localization files and have them available" do
+ add_localization_dir(@lang_test_path)
+ load_localization!
+ localizations['en'][:right].should == 'right'
+ localizations['en'][:left].should == 'left'
+ localizations['en']['US'][:greetings].should == 'Howdie'
+ end
+
+ it "should load more localization files and have them available" do
+ add_localization_dir(@lang_test_path)
+ load_localization!
+ localizations['en'][:right].should == 'right'
+ localizations.has_key?('fr').should be_false
+
+ add_localization_dir(@lang_test_path_2)
+ load_localization!
+ localizations['en'][:right].should == 'right'
+ localizations.has_key?('fr').should be_true
+ localizations['fr'][:right].should == 'la droite'
+ localizations['fr'][:left].should == 'la gauche'
+ localizations['fr'][:greetings].should == 'Salut'
+ end
+
+end
View
127 gems/gems/merb_babel-0.1.0/spec/merb_babel_spec.rb
@@ -0,0 +1,127 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+
+describe 'using set_locale before filter, ' do
+ describe 'a controller' do
+
+ describe 'locale' do
+
+ it "should be set by default" do
+ c = dispatch_to(TestController, :index)
+ c.locale.should == 'en-US'
+ end
+
+ it "should be able to be set by passing a param" do
+ c = dispatch_to(TestController, :index, :locale => 'fr-FR')
+ c.locale.should == 'fr-FR'
+ end
+
+ it "should be able to be set using the session" do
+ c = dispatch_to(TestController, :index) do |controller|
+ controller.stub!(:session).and_return( :locale => "es-BO" )
+ end
+ c.locale.should == 'es-BO'
+ end
+
+ it "should be set using an url (when the routes are set properly)" do
+ #c = get('tests')
+ #c.locale.should == 'en-US'
+ c = get('en-US/tests')
+ c.locale.should == 'en-US'
+ c = get('en-UK/tests')
+ c.locale.should == 'en-UK'
+ end
+
+ it "should support longer locales (like zh-Hans-CN)" do
+ c = dispatch_to(TestController, :index, :locale => 'zh-Hans-CN')
+ c.locale.should == 'zh-hans-CN'
+ end
+
+ end
+
+ describe 'language' do
+
+ it "should be set by default" do
+ c = dispatch_to(TestController, :index)
+ c.language.should == 'en'
+ end
+
+ it "should be able to be set by passing a param" do
+ c = dispatch_to(TestController, :index, :language => 'fr')
+ c.language.should == 'fr'
+ end
+
+ it "should bet set when a locale was set by params" do
+ c = dispatch_to(TestController, :index, :locale => 'fr-FR')
+ c.locale.should == 'fr-FR'
+ c.language.should == 'fr'
+ end
+
+ it "should be set when a locale was set by params even with a long locale" do
+ c = dispatch_to(TestController, :index, :locale => 'zh-Hans-CN')
+ c.language.should == 'zh-hans'
+ end
+
+ it "should bet set when a locale was set by session" do
+ c = dispatch_to(TestController, :index) do |controller|
+ controller.stub!(:session).and_return( :locale => "es-BO" )
+ end
+ c.language.should == 'es'
+ end
+
+ it "should be set by the router" do
+ c = get('fr/languages')
+ c.language.should == 'fr'
+ c.locale.should == 'en-US'
+ # c = get('languages')
+ # c.language.should == 'en'
+ end
+
+ end
+
+ describe 'country' do
+
+ it "should be set by default" do
+ c = dispatch_to(TestController, :index)
+ c.country.should == 'US'
+ end
+
+ it "should be able to be set by passing a param" do
+ c = dispatch_to(TestController, :index, :country => 'ES')
+ c.country.should == 'ES'
+ end
+
+ it "should bet set when a locale was set by params" do
+ c = dispatch_to(TestController, :index, :locale => 'fr-FR')
+ c.country.should == 'FR'
+ end
+
+ it "should bet set when a locale was set by session" do
+ c = dispatch_to(TestController, :index) do |controller|
+ controller.stub!(:session).and_return( :locale => "es-BO" )
+ end
+ c.country.should == 'BO'
+ end
+
+ end
+
+ end
+end
+
+describe 'using set_language, ' do
+ describe 'a controller' do
+ describe 'language' do
+
+ it "should be set by default" do
+ c = dispatch_to(LanguageController, :index)
+ c.language.should == 'en'
+ end
+
+ it "should be able to be set by passing a param" do
+ c = dispatch_to(LanguageController, :index, :language => 'fr')
+ c.language.should == 'fr'
+ end
+
+ end
+ end
+end
View
14 gems/gems/merb_babel-0.1.0/spec/other_lang_dir/fr.yml
@@ -0,0 +1,14 @@
+#################################################
+# French localization
+#
+# localization specific must go in their own file
+# see en-US.yml for instance
+#################################################
+mloc_language_code: fr
+# mloc_country:
+#################################################
+
+right: la droite
+left: la gauche
+greetings: Salut
+
View
49 gems/gems/merb_babel-0.1.0/spec/spec_helper.rb
@@ -0,0 +1,49 @@
+$TESTING=true
+$:.push File.join(File.dirname(__FILE__), '..', 'lib')
+require "rubygems"
+require "merb-core"
+require File.join(File.dirname(__FILE__), "..", 'lib', 'merb_babel')
+
+default_options = {
+ :environment => 'test',
+ :adapter => 'runner',
+ :session_store => 'cookie',
+ :session_secret_key => '187a66e27674660418cf4499471d5a0587f360d0'
+}
+
+options = default_options.merge($START_OPTIONS || {})
+
+Merb.disable(:initfile)
+Merb.start_environment(options)
+
+Spec::Runner.configure do |config|
+ config.include Merb::Test::ViewHelper
+ config.include Merb::Test::RouteHelper
+ config.include Merb::Test::RequestHelper
+ config.include Merb::Test::ControllerHelper
+end
+
+Merb.load_dependencies(:environment => 'test')
+
+Merb::Router.prepare do |r|
+ r.match(/\/?(en\-US|en\-UK|es\-ES|es\-AR)?/).to(:locale => "[1]") do |l|
+ l.match("/tests").to(:controller => "test_controller")
+ end
+ r.match(/\/?(en|es|fr|de)?/).to(:language => "[1]") do |l|
+ l.match("/languages").to(:controller => "language_controller")
+ end
+end
+
+
+
+class TestController < Merb::Controller
+
+ before :set_locale
+ def index; end
+end
+
+class LanguageController < Merb::Controller
+
+ before :set_language
+ def index; end
+end
View
33 gems/specifications/merb_babel-0.1.0.gemspec
@@ -0,0 +1,33 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{merb_babel}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Matt Aimonetti"]
+ s.autorequire = %q{merb_babel}
+ s.date = %q{2008-12-02}
+ s.description = %q{Merb plugin that provides simple localization/internationalisation}
+ s.email = %q{mattaimonetti@gmail.com}
+ s.extra_rdoc_files = ["README", "LICENSE", "TODO"]
+ s.files = ["LICENSE", "README", "Rakefile", "TODO", "lib/merb_babel", "lib/merb_babel/core_ext.rb", "lib/merb_babel/m_i18n.rb", "lib/merb_babel/m_l10n.rb", "lib/merb_babel/m_locale.rb", "lib/merb_babel/merbtasks.rb", "lib/merb_babel.rb", "spec/lang", "spec/lang/en-UK.yml", "spec/lang/en-US.yml", "spec/lang/en.yml", "spec/m_i18n_spec.rb", "spec/m_l10n_spec.rb", "spec/merb_babel_spec.rb", "spec/other_lang_dir", "spec/other_lang_dir/fr.yml", "spec/spec_helper.rb"]
+ s.has_rdoc = true
+ s.homepage = %q{http://github.com/mattetti/merb_babel/}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.1}
+ s.summary = %q{Merb plugin that provides simple localization/internationalisation}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<merb-core>, ["~> 1.0.0"])
+ else
+ s.add_dependency(%q<merb-core>, ["~> 1.0.0"])
+ end
+ else
+ s.add_dependency(%q<merb-core>, ["~> 1.0.0"])
+ end
+end
View
1  log/merb.4000.pid
@@ -1 +0,0 @@
-71328
View
15 public/stylesheets/master.css
@@ -21,6 +21,21 @@ body {
-webkit-box-shadow: #333 0px 1px 5px;
}
+.to-translate:before {
+ font-weight: bold;
+ background: #999;
+ color: white;
+ padding: 0.5em;
+ content: "Translation needed";
+}
+
+
+.to-translate{
+ border-left: 1.5em solid #CC6600;
+ padding: 1em;
+ background: #ccc;
+}
+
#book-title{
padding: 4em 2em 1em 9em;
font-size: 160%;
Please sign in to comment.
Something went wrong with that request. Please try again.