haml-i18n 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in haml-i18n.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Clemens Helm
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.md ADDED
@@ -0,0 +1,39 @@
1
+ # haml-i18n
2
+
3
+ Haml-i18n provides basic functionality to translate [haml](https://github.com/nex3/haml) templates. Instead of writing `%p= I18n.translate('.user_name')` from now on you can just write `%p User name`.
4
+
5
+ Note that this is a very early version of the plugin. I just developed it to fit one project’s needs. If you find any errors please write an issue or provide a fix directly. Please take a look at _Contributing to haml-i18n_ on how to write a fix.
6
+
7
+ ## Installation
8
+
9
+ Add `gem "haml-i18n"` to your project’s `GEMFILE` and run `bundle` to install haml-i18n and its dependencies.
10
+
11
+ ## Usage
12
+
13
+ You can put your keys in plain text into your haml template:
14
+
15
+ %p User name
16
+
17
+ This will try to
18
+
19
+ * retrieve `[view_folder].[template_name].user_name`
20
+ * retrieve `user_name` if the template-scoped translation doesn’t exist.
21
+
22
+ ## Contributing to haml-i18n
23
+
24
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
25
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
26
+ * Fork the project
27
+ * Start a feature/bugfix branch
28
+ * Commit and push until you are happy with your contribution
29
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
30
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
31
+
32
+ ## Copyright
33
+
34
+ Copyright (c) 2011 Clemens Helm. See LICENSE.txt for
35
+ further details.
36
+
37
+ ## Thanks to…
38
+
39
+ [Cail](https://github.com/cail) for giving me the inspiration and a starting point with [haml\_i18n](https://github.com/cail/haml_i18n) for developing this gem.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/haml-i18n.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "haml-i18n/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "haml-i18n"
7
+ s.homepage = "http://github.com/clemenshelm/haml-i18n"
8
+ s.license = "MIT"
9
+ s.version = Haml::I18n::VERSION
10
+ s.authors = ["Clemens Helm"]
11
+ s.email = ["clemens.helm@gmail.com"]
12
+ s.homepage = ""
13
+ s.summary = %q{Easy translation of haml templates.}
14
+ s.description = %q{This gem automatically retrieves translations from the I18n backend without reqiring explicit calls to I18n.translate from the haml templates.}
15
+
16
+ s.add_development_dependency "rspec"
17
+
18
+ s.add_dependency 'haml'
19
+ s.add_dependency 'activesupport'
20
+ s.add_dependency 'i18n'
21
+
22
+ s.rubyforge_project = "haml-i18n"
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+
29
+ # specify any dependencies here; for example:
30
+ # s.add_development_dependency "rspec"
31
+ # s.add_runtime_dependency "rest-client"
32
+ end
@@ -0,0 +1,5 @@
1
+ module Haml
2
+ module I18n
3
+ VERSION = "0.0.3"
4
+ end
5
+ end
data/lib/haml-i18n.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'haml'
2
+ require 'active_support/all'
3
+
4
+ module HamlI18n
5
+ module Internationalizer
6
+ def self.included(base)
7
+ base.class_eval do
8
+ alias_method_chain :plain, :i18n
9
+ alias_method_chain :parse_tag, :i18n
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def plain_with_i18n(text, escape_html = nil)
16
+ i18n_text = translate(text)
17
+ plain_without_i18n(i18n_text, escape_html)
18
+ end
19
+
20
+ def parse_tag_with_i18n(line)
21
+ tag_name, attributes, attributes_hash, object_ref, nuke_outer_whitespace, nuke_inner_whitespace, action, value = parse_tag_without_i18n(line)
22
+
23
+ i18n_value = action || value.blank? ? value : translate(value)
24
+
25
+ [tag_name, attributes, attributes_hash, object_ref, nuke_outer_whitespace, nuke_inner_whitespace, action, i18n_value]
26
+ end
27
+
28
+ def translate(text)
29
+ simple_key = text.parameterize.underscore
30
+
31
+ filename_components = options[:filename].match(/\/views\/([^\/]+)\/([^\/]+).html.haml/)
32
+ key = filename_components ? "#{filename_components[1]}.#{filename_components[2]}.#{simple_key}" : simple_key
33
+
34
+ begin
35
+ I18n.translate(key, raise: true)
36
+ rescue I18n::MissingTranslationData
37
+ I18n.translate(simple_key)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ module Haml
44
+ class Engine
45
+ include HamlI18n::Internationalizer
46
+ end
47
+ end
@@ -0,0 +1,70 @@
1
+ require File.expand_path("../../lib/haml-i18n.rb", __FILE__)
2
+ require 'i18N'
3
+ require 'haml'
4
+
5
+ describe HamlI18n do
6
+ after { I18n.backend.reload! }
7
+
8
+ it "looks up 'key' as 'key'" do
9
+ I18n.backend.store_translations :en, key: 'Cheers'
10
+ template = Haml::Engine.new 'key'
11
+ template.render.should =~ /Cheers/
12
+ end
13
+
14
+ it "looks up 'Key' as 'key'" do
15
+ I18n.backend.store_translations :en, key: 'Bless you'
16
+ template = Haml::Engine.new 'Key'
17
+ template.render.should =~ /Bless you/
18
+ end
19
+
20
+ it "looks up 'User name' as 'user_name'" do
21
+ I18n.backend.store_translations :en, user_name: 'Kevin'
22
+ template = Haml::Engine.new 'User name'
23
+ template.render.should =~ /Kevin/
24
+ end
25
+
26
+ context "within a tag" do
27
+ it "looks up 'key' as 'key'" do
28
+ I18n.backend.store_translations :en, key: 'Cheers'
29
+ template = Haml::Engine.new '%p key'
30
+ template.render.should =~ /<p>Cheers<\/p>/
31
+ end
32
+
33
+ context "that is nested" do
34
+ it "looks up 'key' as 'key'" do
35
+ I18n.backend.store_translations :en, key: 'Cheers'
36
+ template = Haml::Engine.new "%div\n\t%p key"
37
+ template.render.should =~ /<p>Cheers<\/p>/
38
+ end
39
+ end
40
+
41
+ context "within a ruby block" do
42
+ it "doesn't look up anything" do
43
+ template = Haml::Engine.new '%p= "Cheers"'
44
+ template.render.should =~ /<p>Cheers<\/p>/
45
+ end
46
+ end
47
+ end
48
+
49
+ context "when there is a template path" do
50
+ it "looks up 'key' as '[view_folder].[template_name].key'" do
51
+ I18n.backend.store_translations :en, users: {index: {key: 'Cheers'}}
52
+ template = Haml::Engine.new 'key', filename: '/long/path/to/views/users/index.html.haml'
53
+ template.render.should =~ /Cheers/
54
+ end
55
+
56
+ it "looks up 'Key' as '[view_folder].[template_name].key'" do
57
+ I18n.backend.store_translations :en, users: {index: {key: 'Cheers'}}
58
+ template = Haml::Engine.new 'Key', filename: '/long/path/to/views/users/index.html.haml'
59
+ template.render.should =~ /Cheers/
60
+ end
61
+
62
+ context "when there is no translation for the template path" do
63
+ it "looks up 'key' as 'key'" do
64
+ I18n.backend.store_translations :en, key: 'Cheers'
65
+ template = Haml::Engine.new 'key', filename: '/long/path/to/views/users/index.html.haml'
66
+ template.render.should =~ /Cheers/
67
+ end
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haml-i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Clemens Helm
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2153666160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2153666160
25
+ - !ruby/object:Gem::Dependency
26
+ name: haml
27
+ requirement: &2153665740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2153665740
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &2153665320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2153665320
47
+ - !ruby/object:Gem::Dependency
48
+ name: i18n
49
+ requirement: &2153664900 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *2153664900
58
+ description: This gem automatically retrieves translations from the I18n backend without
59
+ reqiring explicit calls to I18n.translate from the haml templates.
60
+ email:
61
+ - clemens.helm@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - haml-i18n.gemspec
72
+ - lib/haml-i18n.rb
73
+ - lib/haml-i18n/version.rb
74
+ - spec/haml-i18n_spec.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project: haml-i18n
96
+ rubygems_version: 1.8.6
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Easy translation of haml templates.
100
+ test_files:
101
+ - spec/haml-i18n_spec.rb