locale_schema 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@locale_schema
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in locale_schema.gemspec
4
+ gemspec
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,3 @@
1
+ require "locale_schema/version"
2
+ require "locale_schema/translation"
3
+ require "locale_schema/rails/action_view"
@@ -0,0 +1,23 @@
1
+ module LocaleSchema
2
+ module Rails
3
+ module ActionView
4
+ def st(key, options={})
5
+ translation = LocaleSchema::Translation.new(
6
+ :key => key,
7
+ :options => options,
8
+ :context => :views,
9
+ :scope => "#{controller_path}/#{action_name}"
10
+ )
11
+ s = translation.to_s
12
+ s = s.html_safe if translation.html_safe? && s.respond_to(:html_safe)
13
+ s
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ module ActionView
20
+ class Base
21
+ include LocaleSchema::Rails::ActionView
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ module LocaleSchema
2
+ class Translation
3
+ attr_reader :key, :context, :options, :scope
4
+
5
+ HTML_SAFE_LEAF_NAMES = %w{markdown textile html}
6
+
7
+ def initialize(args={})
8
+ @key = args[:key]
9
+ @options = args[:options] || {}
10
+ @context = args[:context]
11
+ @scope = args[:scope].split('/') || []
12
+ end
13
+
14
+ def leaf_name
15
+ key.to_s.split('.').last
16
+ end
17
+
18
+ def html_safe?
19
+ HTML_SAFE_LEAF_NAMES.include?(leaf_name)
20
+ end
21
+
22
+ def defaults
23
+ [
24
+ :"#{context}.#{scope.join('.')}.#{key}",
25
+ :"#{context}.application.#{key}"
26
+ ]
27
+ end
28
+
29
+ def to_s
30
+ I18n.t(defaults[0], options.merge(:default => defaults[1..-1]))
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module LocaleSchema
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "locale_schema/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "locale_schema"
7
+ s.version = LocaleSchema::VERSION
8
+ s.authors = ["Christopher Dell"]
9
+ s.email = ["chris@tigrish.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{This gem provides a hierarchy for I18n defaults}
12
+ s.description = %q{This gem provides a hierarchy for I18n defaults}
13
+
14
+ s.rubyforge_project = "locale_schema"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency('i18n')
22
+ s.add_development_dependency('rspec')
23
+ s.add_development_dependency('guard-rspec')
24
+ end
@@ -0,0 +1,11 @@
1
+ en:
2
+ views:
3
+ application:
4
+ button:
5
+ submit: Submit button for application
6
+ welcome: "Welcome %{name}!"
7
+ projects:
8
+ new:
9
+ heading: New project heading
10
+ button:
11
+ submit: Submit button for new project
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require 'support/views'
3
+
4
+ describe LocaleSchema::Rails::ActionView, '#st' do
5
+ before do
6
+ @view = ActionView::Base.new('projects/new')
7
+ end
8
+
9
+ it "looks up the view translation for the current controller and action" do
10
+ @view.st(:heading).should == "New project heading"
11
+ end
12
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe LocaleSchema::Translation do
4
+ def do_setup(options={})
5
+ opts = {:key => :'button.submit', :context => :views, :scope => 'projects/new'}.merge(options)
6
+ @translation = LocaleSchema::Translation.new(opts)
7
+ end
8
+
9
+ describe "#leaf_name" do
10
+ it "returns the last segment of the key" do
11
+ do_setup
12
+ @translation.leaf_name.should == 'submit'
13
+ end
14
+ end
15
+
16
+ describe "#html_safe?" do
17
+ it "returns true when leaf name is :html" do
18
+ do_setup(:key => :'foo.html')
19
+ @translation.should be_html_safe
20
+ end
21
+
22
+ it "returns true when leaf name is :textile" do
23
+ do_setup(:key => :'foo.textile')
24
+ @translation.should be_html_safe
25
+ end
26
+
27
+ it "returns true when leaf name is :markdown" do
28
+ do_setup(:key => :'foo.markdown')
29
+ @translation.should be_html_safe
30
+ end
31
+
32
+ it "returns false otherwise" do
33
+ do_setup(:key => :'foo.bar')
34
+ @translation.should_not be_html_safe
35
+ end
36
+ end
37
+
38
+ describe "#defaults" do
39
+ before { do_setup }
40
+
41
+ it "returns the scoped key as the first array element" do
42
+ @translation.defaults.first.should == :'views.projects.new.button.submit'
43
+ end
44
+
45
+ it "returns the application scope as the last array element" do
46
+ @translation.defaults.last.should == :'views.application.button.submit'
47
+ end
48
+ end
49
+
50
+ describe "#to_s" do
51
+ it "performs the I18n.t call" do
52
+ do_setup(:scope => 'projects/new')
53
+ @translation.to_s.should == "Submit button for new project"
54
+ end
55
+
56
+ it "passes in the :default param to the I18n.t call" do
57
+ do_setup(:scope => 'projects/edit')
58
+ @translation.to_s.should == "Submit button for application"
59
+ end
60
+
61
+ it "passes in :options to I18n" do
62
+ do_setup(:key => :welcome, :options => {:name => 'Chris'})
63
+ @translation.to_s.should == "Welcome Chris!"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ require 'locale_schema'
2
+ require 'i18n'
3
+
4
+ I18n.load_path << 'spec/fixtures/en.yml'
@@ -0,0 +1,15 @@
1
+ module ActionView
2
+ class Base
3
+ def initialize(path)
4
+ @path = path.split('/')
5
+ end
6
+
7
+ def controller_path
8
+ @path[0..-2].join('/')
9
+ end
10
+
11
+ def action_name
12
+ @path.last
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locale_schema
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.3
6
+ platform: ruby
7
+ authors:
8
+ - Christopher Dell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-12 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: i18n
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: guard-rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: This gem provides a hierarchy for I18n defaults
50
+ email:
51
+ - chris@tigrish.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - .rvmrc
61
+ - Gemfile
62
+ - Guardfile
63
+ - Rakefile
64
+ - lib/locale_schema.rb
65
+ - lib/locale_schema/rails/action_view.rb
66
+ - lib/locale_schema/translation.rb
67
+ - lib/locale_schema/version.rb
68
+ - locale_schema.gemspec
69
+ - spec/fixtures/en.yml
70
+ - spec/locale_schema/rails/actionv_view_spec.rb
71
+ - spec/locale_schema/translation_spec.rb
72
+ - spec/spec_helper.rb
73
+ - spec/support/views.rb
74
+ has_rdoc: true
75
+ homepage: ""
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: locale_schema
98
+ rubygems_version: 1.5.0
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: This gem provides a hierarchy for I18n defaults
102
+ test_files:
103
+ - spec/fixtures/en.yml
104
+ - spec/locale_schema/rails/actionv_view_spec.rb
105
+ - spec/locale_schema/translation_spec.rb
106
+ - spec/spec_helper.rb
107
+ - spec/support/views.rb