merb-i18n 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ log/*
3
+ tmp/*
4
+ *~
5
+ .#*
6
+ pkg/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tymon Tobolski (http://teamon.eu)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,25 @@
1
+ merb-i18n
2
+ =========
3
+
4
+ A plugin for the Merb framework that provides i18n support to translate your site in several languages.
5
+
6
+ It is just a wrap for R18n core library. See R18n documentation for more information. (http://github.com/ai/r18n)
7
+
8
+ Features
9
+ - It has special support for countries with two official languages. If there isn’t translation in user locale, it will be found in locales, which user may know (not only in default locale). For example, many people in Belarus can understand Russian, and locale has information about it.
10
+ - It can format numbers and time to the rules of the user locale, translate month and week days name and give other locale information.
11
+ - It has translation for commons words, like “OK”, “Cancel”, etc.
12
+ - It storage translation in rich YAML format. You can put procedures and pluralization (“1 comment”, “5 comments”) in your translation.
13
+
14
+ Instalation
15
+ -----------
16
+ gem sources -a http://gemcutter.org
17
+ sudo gem install merb-i18n
18
+
19
+ # config/dependencies.rb
20
+ dependency "merb-i18n"
21
+
22
+ Usage
23
+ -----
24
+
25
+ - Create directory Merb.root/app/i18n and put there
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "merb-i18n"
5
+ gemspec.summary = gemspec.description = "Merb plugin that provides bindings to r18n"
6
+ gemspec.email = "i@teamon.eu"
7
+ gemspec.homepage = "http://github.com/teamon/merb-i18n"
8
+ gemspec.authors = ['Andrey "A.I." Sitnik', "Tymon Tobolski"]
9
+ gemspec.add_dependency('merb-core', '>= 1.0')
10
+ gemspec.add_dependency('r18n-core', '>= 0.2.3')
11
+ end
12
+ Jeweler::GemcutterTasks.new
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
16
+
17
+ desc "Run specs"
18
+ task :spec do
19
+ system("spec -O spec/spec.opts spec")
20
+ end
data/TODO ADDED
File without changes
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.3
data/lib/merb-i18n.rb ADDED
@@ -0,0 +1,88 @@
1
+ require 'r18n-core'
2
+ # make sure we're running inside Merb
3
+ if defined?(Merb::Plugins)
4
+
5
+ # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
6
+ Merb::Plugins.config[:merb_i18n] = {
7
+ :default_locale => 'en',
8
+ :untranslated => '%2<span style="color: red">%3</span>'
9
+ }
10
+
11
+ Merb.push_path(:i18n, Merb.root / :app / :i18n) unless Merb.load_paths.include?(:i18n)
12
+
13
+ module MerbI18n
14
+
15
+ module Controller
16
+ # Return tool for i18n support. It will be R18n::I18n object, see it
17
+ # documentation for more information.
18
+ def i18n
19
+ @i18n
20
+ end
21
+
22
+ def _set_i18n
23
+ R18n::I18n.default = Merb::Plugins.config[:merb_i18n][:default_locale]
24
+
25
+ locales = R18n::I18n.parse_http(request.env['HTTP_ACCEPT_LANGUAGE'])
26
+ locales.insert(0, params[:locale]) if params[:locale]
27
+
28
+ @i18n = R18n::I18n.new(locales, self.i18n_dirs)
29
+ R18n.set(@i18n)
30
+ end
31
+
32
+ # Namespaced i18n with controller and action name
33
+ def ni18n
34
+ @n18n ||= i18n.send(params[:controller]).send(params[:action])
35
+ end
36
+
37
+ # Dirs to load translations
38
+ def i18n_dirs
39
+ Merb.dir_for(:i18n)
40
+ end
41
+ end
42
+
43
+ module Helpers
44
+ def error_messages_for(obj = nil, opts = {})
45
+ current_form_context.error_messages_for(obj, opts[:error_class] || "error",
46
+ opts[:build_li] || "<li>%s</li>",
47
+ opts[:header] || "<h2>#{i18n.merb.error_messages_for_header}</h2>",
48
+ opts.key?(:before) ? opts[:before] : true)
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ class Merb::Controller
55
+ include MerbI18n::Controller
56
+ before :_set_i18n
57
+ end
58
+
59
+ if defined?(Merb::Helpers)
60
+ module Merb::Helpers::Form
61
+ include MerbI18n::Helpers
62
+ end
63
+ end
64
+
65
+ Merb::BootLoader.before_app_loads do
66
+ R18n.untranslated = Merb::Plugins.config[:merb_i18n][:untranslated]
67
+ end
68
+
69
+ Merb::BootLoader.after_app_loads do
70
+ if defined? Merb::Slices
71
+ Merb::Slices.each_slice do |slice|
72
+ unless slice.slice_paths.include? :i18n
73
+ slice.push_path :i18n, slice.root_path('app' / 'i18n')
74
+ end
75
+ end
76
+
77
+ module Merb::Slices::ControllerMixin::MixinMethods::InstanceMethods
78
+ protected
79
+ def i18n_dirs
80
+ [self.slice.dir_for(:i18n), Merb.dir_for(:i18n)]
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ Merb::Plugins.add_rakefiles "merb-i18n/merbtasks"
87
+
88
+ end
@@ -0,0 +1,8 @@
1
+ namespace :merb_i18n do
2
+ desc "Create i18n folder"
3
+ task :install do
4
+ dir = Merb.root / :app / :i18n
5
+ Dir.mkdir(dir) unless File.exist?(dir)
6
+ File.open(dir / 'en.yml', 'w') {|f| f.puts "foo: bar" } unless File.exist?(dir / 'en.yml')
7
+ end
8
+ end
data/merb-i18n.gemspec ADDED
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{merb-i18n}
8
+ s.version = "0.2.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Andrey \"A.I.\" Sitnik", "Tymon Tobolski"]
12
+ s.date = %q{2009-10-12}
13
+ s.description = %q{Merb plugin that provides bindings to r18n}
14
+ s.email = %q{i@teamon.eu}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "TODO",
25
+ "VERSION",
26
+ "lib/merb-i18n.rb",
27
+ "lib/merb-i18n/merbtasks.rb",
28
+ "merb-i18n.gemspec",
29
+ "spec/app/controllers/i18n.rb",
30
+ "spec/app/i18n/en.yml",
31
+ "spec/app/i18n/ru.yml",
32
+ "spec/app/slice/app/i18n/en.yml",
33
+ "spec/app/slice/application.rb",
34
+ "spec/app/slice/lib/slice.rb",
35
+ "spec/app/views/i18n/code.html.erb",
36
+ "spec/app/views/i18n/find_me.html.erb",
37
+ "spec/app/views/i18n/index.html.erb",
38
+ "spec/app/views/i18n/untranslated.html.erb",
39
+ "spec/app/views/i18n/use_t.html.erb",
40
+ "spec/merb-i18n_spec.rb",
41
+ "spec/spec.opts",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/teamon/merb-i18n}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.5}
48
+ s.summary = %q{Merb plugin that provides bindings to r18n}
49
+ s.test_files = [
50
+ "spec/app/controllers/i18n.rb",
51
+ "spec/app/slice/application.rb",
52
+ "spec/app/slice/lib/slice.rb",
53
+ "spec/merb-i18n_spec.rb",
54
+ "spec/spec_helper.rb"
55
+ ]
56
+
57
+ if s.respond_to? :specification_version then
58
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
59
+ s.specification_version = 3
60
+
61
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
62
+ s.add_runtime_dependency(%q<merb-core>, [">= 1.0"])
63
+ s.add_runtime_dependency(%q<r18n-core>, [">= 0.2.3"])
64
+ else
65
+ s.add_dependency(%q<merb-core>, [">= 1.0"])
66
+ s.add_dependency(%q<r18n-core>, [">= 0.2.3"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<merb-core>, [">= 1.0"])
70
+ s.add_dependency(%q<r18n-core>, [">= 0.2.3"])
71
+ end
72
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ class I18n < Merb::Controller
3
+ self._template_root = File.dirname(__FILE__) / "../views"
4
+
5
+ def index
6
+ @article = i18n.l 10000
7
+ @created = Time.at(0)
8
+
9
+ render
10
+ end
11
+
12
+ def code
13
+ render
14
+ end
15
+
16
+ def untranslated
17
+ render
18
+ end
19
+
20
+ def find_me
21
+ render
22
+ end
23
+
24
+ def use_t
25
+ render
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ title: Article %1
2
+
3
+ created:
4
+ at: Created at %1
5
+
6
+ other: "Translations: %1"
7
+
8
+ app: APP
9
+
10
+ i18n:
11
+ find_me:
12
+ hidden: "Hurray!"
13
+
14
+ I am a string: I am translated string!
15
+ "I see numbers: %1": I am translated with %1
@@ -0,0 +1,4 @@
1
+ created:
2
+ at: Написана %1
3
+
4
+ other: "Переводы: %1"
@@ -0,0 +1 @@
1
+ slice: SLICE
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ module Slice
3
+ class Main < Merb::Controller
4
+ controller_for_slice
5
+ def index
6
+ render i18n.slice + ' and ' + i18n.app
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ if defined? Merb::Plugins
3
+ dependency 'merb-slices', '~> 1.0'
4
+ Merb::Slices::register(__FILE__)
5
+
6
+ module Slice
7
+ self.description = 'Test i18n for slices'
8
+ self.version = '0.0.1'
9
+ self.author = 'Andrey A.I. Sitnik'
10
+
11
+ def self.loaded; end
12
+ def self.activate; end
13
+ def self.deactivate; end
14
+
15
+ def self.setup_router(scope)
16
+ scope.default_routes
17
+ end
18
+ end
19
+
20
+ require File.dirname(__FILE__) / '../application'
21
+ end
@@ -0,0 +1 @@
1
+ <%= i18n.locale['code'] %>
@@ -0,0 +1 @@
1
+ <%= ni18n.hidden %>
@@ -0,0 +1,3 @@
1
+ <h1><%= i18n.title(@article) %></h1>
2
+ <p><%= i18n.created.at i18n.l(@created, :standard) %></p>
3
+ <p><%= i18n.other(i18n.translations.values.join(', ')) %></p>
@@ -0,0 +1 @@
1
+ <%= i18n.i.dont.exist %>
@@ -0,0 +1,2 @@
1
+ <%= "I am a string".t %>
2
+ <%= "I see numbers: %1".t(5) %>
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "merb-i18n" do
4
+
5
+ it "should provide i18n support in merb controller" do
6
+ c = dispatch_to(I18n, :index, {}, "HTTP_ACCEPT_LANGUAGE" => "ru_RU")
7
+
8
+ c.body.should == "<h1>Article 10 000</h1>\n" +
9
+ "<p>Написана 01.01.1970 01:00</p>\n" +
10
+ "<p>Переводы: Русский, English</p>\n"
11
+ end
12
+
13
+ it "should set locale manually" do
14
+ c = dispatch_to(I18n, :code, {}, "HTTP_ACCEPT_LANGUAGE" => "ru")
15
+ c.body.should == "ru\n"
16
+
17
+ c = dispatch_to(I18n, :code, {:locale => "en"}, "HTTP_ACCEPT_LANGUAGE" => "ru")
18
+ c.body.should == "en\n"
19
+ end
20
+
21
+ it "should provide i18n support in slice controller" do
22
+ dispatch_to(Slice::Main, :index).body.should == 'SLICE and APP'
23
+ end
24
+
25
+ it "should try to find translation using controller and action name" do
26
+ dispatch_to(I18n, :find_me).body.should == "Hurray!\n"
27
+ end
28
+
29
+ it "should mark untranslated string with custom pattern string" do
30
+ Merb::Plugins.config[:merb_i18n][:untranslated] = "[T]%1[/T]"
31
+ Merb::BootLoader::BeforeAppLoads.run
32
+ dispatch_to(I18n, :untranslated).body.should == "[T]i.dont.exist[/T]\n"
33
+ end
34
+
35
+ # describe MerbI18n::Helpers do
36
+ # it "should have translated error header" do
37
+ # pending
38
+ # end
39
+ # end
40
+
41
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format specdoc
@@ -0,0 +1,25 @@
1
+ require "rubygems"
2
+
3
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
4
+
5
+ require "merb-core"
6
+ require "merb-slices"
7
+ require "merb-helpers"
8
+ require "spec" # Satisfies Autotest and anyone else not using the Rake tasks
9
+
10
+ require File.dirname(__FILE__) / '../lib/merb-i18n'
11
+ require File.dirname(__FILE__) / 'app/controllers/i18n'
12
+
13
+ Merb.push_path(:i18n, File.dirname(__FILE__) / 'app/i18n')
14
+
15
+ Merb::Plugins.config[:merb_slices][:auto_register] = true
16
+ Merb::Plugins.config[:merb_slices][:search_path] = File.dirname(__FILE__) / 'app/'
17
+ Merb::Router.prepare { add_slice(:slice) }
18
+
19
+ Merb.start_environment(:testing => true, :adapter => 'runner', :environment => ENV['MERB_ENV'] || 'test', :session_store => "memory")
20
+
21
+ Spec::Runner.configure do |config|
22
+ config.include(Merb::Test::ViewHelper)
23
+ config.include(Merb::Test::RouteHelper)
24
+ config.include(Merb::Test::ControllerHelper)
25
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb-i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Andrey "A.I." Sitnik
8
+ - Tymon Tobolski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-10-12 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: merb-core
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "1.0"
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: r18n-core
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.2.3
35
+ version:
36
+ description: Merb plugin that provides bindings to r18n
37
+ email: i@teamon.eu
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - LICENSE
44
+ - README.markdown
45
+ files:
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.markdown
49
+ - Rakefile
50
+ - TODO
51
+ - VERSION
52
+ - lib/merb-i18n.rb
53
+ - lib/merb-i18n/merbtasks.rb
54
+ - merb-i18n.gemspec
55
+ - spec/app/controllers/i18n.rb
56
+ - spec/app/i18n/en.yml
57
+ - spec/app/i18n/ru.yml
58
+ - spec/app/slice/app/i18n/en.yml
59
+ - spec/app/slice/application.rb
60
+ - spec/app/slice/lib/slice.rb
61
+ - spec/app/views/i18n/code.html.erb
62
+ - spec/app/views/i18n/find_me.html.erb
63
+ - spec/app/views/i18n/index.html.erb
64
+ - spec/app/views/i18n/untranslated.html.erb
65
+ - spec/app/views/i18n/use_t.html.erb
66
+ - spec/merb-i18n_spec.rb
67
+ - spec/spec.opts
68
+ - spec/spec_helper.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/teamon/merb-i18n
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.5
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Merb plugin that provides bindings to r18n
97
+ test_files:
98
+ - spec/app/controllers/i18n.rb
99
+ - spec/app/slice/application.rb
100
+ - spec/app/slice/lib/slice.rb
101
+ - spec/merb-i18n_spec.rb
102
+ - spec/spec_helper.rb