haml-magic-translations 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +17 -16
- data/Rakefile +6 -7
- data/TODO +1 -1
- data/VERSION +1 -1
- data/haml-magic-translations.gemspec +9 -14
- data/lib/haml/magic_translations.rb +108 -86
- data/spec/{magic_translations_spec.rb → magic_translations/i18n_spec.rb} +4 -7
- data/spec/spec_helper.rb +5 -3
- metadata +11 -38
data/README.rdoc
CHANGED
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
This plugin provides "magical translations" in your .haml files. What does it
|
4
4
|
mean? It's mean that all your raw texts in templates will be automatically
|
5
|
-
translated by Gettext backend from I18n. No more
|
6
|
-
and ugly translation methods in views. Now you can
|
7
|
-
nothing more. At the end of your work you can easy
|
8
|
-
and generate .po files for it. This type of files
|
9
|
-
easier to translate, thanks to it you save your
|
5
|
+
translated by GetText, FastGettext or Gettext backend from I18n. No more
|
6
|
+
complicated translation keys and ugly translation methods in views. Now you can
|
7
|
+
only write in your language, nothing more. At the end of your work you can easy
|
8
|
+
find all phrases to translate and generate .po files for it. This type of files
|
9
|
+
are also more readable and easier to translate, thanks to it you save your
|
10
|
+
time with translations.
|
10
11
|
|
11
12
|
=== Installation
|
12
13
|
|
@@ -22,20 +23,20 @@ or
|
|
22
23
|
|
23
24
|
=== Gettings started with Gettext
|
24
25
|
|
25
|
-
To use automatical translations you have to using new I18n with Gettext backend
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
I18n.load_path += Dir["path/to/your/locales/*.{po}")]
|
30
|
-
|
31
|
-
=== Time for translations
|
26
|
+
To use automatical translations you have to using new I18n with Gettext backend,
|
27
|
+
GetText or FastGettext, properly installed and configured. To enable plugin
|
28
|
+
you have to do following things:
|
32
29
|
|
33
|
-
|
34
|
-
|
30
|
+
Haml::Template.enable_magic_translations(:i18n) # this backend is used by default
|
31
|
+
|
32
|
+
Available backends are `:i18n`, `:gettext and `:fast_gettext`
|
35
33
|
|
36
|
-
|
34
|
+
Now you have to add path to all your .po files with locales. Eg. for i18n backend
|
35
|
+
it will be something like:
|
36
|
+
|
37
|
+
I18n.load_path += Dir["path/to/your/locales/*.{po}")]
|
37
38
|
|
38
|
-
And that's all.
|
39
|
+
And that's all. Now you can enjoy automatically translations in your .haml files!
|
39
40
|
|
40
41
|
=== Examples
|
41
42
|
|
data/Rakefile
CHANGED
@@ -8,18 +8,17 @@ begin
|
|
8
8
|
gem.description = <<-DESCR
|
9
9
|
This plugin provides "magical translations" in your .haml files. What does it
|
10
10
|
mean? It's mean that all your raw texts in templates will be automatically
|
11
|
-
translated by Gettext backend from I18n. No more
|
12
|
-
and ugly translation methods in views. Now you can
|
13
|
-
nothing more. At the end of your work you can easy
|
14
|
-
and generate .po files for it. This type of files
|
15
|
-
easier to translate, thanks to it you save your
|
11
|
+
translated by GetText, FastGettext or Gettext backend from I18n. No more
|
12
|
+
complicated translation keys and ugly translation methods in views. Now you can
|
13
|
+
only write in your language, nothing more. At the end of your work you can easy
|
14
|
+
find all phrases to translate and generate .po files for it. This type of files
|
15
|
+
are also more readable and easier to translate, thanks to it you save your
|
16
|
+
time with translations.
|
16
17
|
DESCR
|
17
18
|
gem.email = "kriss.kowalik@gmail.com"
|
18
19
|
gem.homepage = "http://github.com/kriss/haml-magic-translations"
|
19
20
|
gem.authors = ["Kriss Kowalik"]
|
20
21
|
gem.add_development_dependency "haml", ">= 3.0.0"
|
21
|
-
gem.add_development_dependency "i18n", ">= 0.4.1"
|
22
|
-
gem.add_development_dependency "gettext", ">= 2.0.0"
|
23
22
|
end
|
24
23
|
Jeweler::GemcutterTasks.new
|
25
24
|
rescue LoadError
|
data/TODO
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -5,18 +5,19 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{haml-magic-translations}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kriss Kowalik"]
|
12
12
|
s.date = %q{2010-07-21}
|
13
13
|
s.description = %q{This plugin provides "magical translations" in your .haml files. What does it
|
14
14
|
mean? It's mean that all your raw texts in templates will be automatically
|
15
|
-
translated by Gettext backend from I18n. No more
|
16
|
-
and ugly translation methods in views. Now you can
|
17
|
-
nothing more. At the end of your work you can easy
|
18
|
-
and generate .po files for it. This type of files
|
19
|
-
easier to translate, thanks to it you save your
|
15
|
+
translated by GetText, FastGettext or Gettext backend from I18n. No more
|
16
|
+
complicated translation keys and ugly translation methods in views. Now you can
|
17
|
+
only write in your language, nothing more. At the end of your work you can easy
|
18
|
+
find all phrases to translate and generate .po files for it. This type of files
|
19
|
+
are also more readable and easier to translate, thanks to it you save your
|
20
|
+
time with translations.
|
20
21
|
}
|
21
22
|
s.email = %q{kriss.kowalik@gmail.com}
|
22
23
|
s.extra_rdoc_files = [
|
@@ -36,7 +37,7 @@ easier to translate, thanks to it you save your time with translations.
|
|
36
37
|
"lib/haml/magic_translations.rb",
|
37
38
|
"spec/locales/en.po",
|
38
39
|
"spec/locales/pl.po",
|
39
|
-
"spec/
|
40
|
+
"spec/magic_translations/i18n_spec.rb",
|
40
41
|
"spec/spec_helper.rb"
|
41
42
|
]
|
42
43
|
s.homepage = %q{http://github.com/kriss/haml-magic-translations}
|
@@ -46,7 +47,7 @@ easier to translate, thanks to it you save your time with translations.
|
|
46
47
|
s.summary = %q{Provides automaticaly translations in haml templates}
|
47
48
|
s.test_files = [
|
48
49
|
"spec/spec_helper.rb",
|
49
|
-
"spec/
|
50
|
+
"spec/magic_translations/i18n_spec.rb"
|
50
51
|
]
|
51
52
|
|
52
53
|
if s.respond_to? :specification_version then
|
@@ -55,17 +56,11 @@ easier to translate, thanks to it you save your time with translations.
|
|
55
56
|
|
56
57
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
57
58
|
s.add_development_dependency(%q<haml>, [">= 3.0.0"])
|
58
|
-
s.add_development_dependency(%q<i18n>, [">= 0.4.1"])
|
59
|
-
s.add_development_dependency(%q<gettext>, [">= 2.0.0"])
|
60
59
|
else
|
61
60
|
s.add_dependency(%q<haml>, [">= 3.0.0"])
|
62
|
-
s.add_dependency(%q<i18n>, [">= 0.4.1"])
|
63
|
-
s.add_dependency(%q<gettext>, [">= 2.0.0"])
|
64
61
|
end
|
65
62
|
else
|
66
63
|
s.add_dependency(%q<haml>, [">= 3.0.0"])
|
67
|
-
s.add_dependency(%q<i18n>, [">= 0.4.1"])
|
68
|
-
s.add_dependency(%q<gettext>, [">= 2.0.0"])
|
69
64
|
end
|
70
65
|
end
|
71
66
|
|
@@ -1,18 +1,15 @@
|
|
1
1
|
require 'haml'
|
2
2
|
require 'haml/template'
|
3
3
|
|
4
|
-
require 'i18n'
|
5
|
-
require 'i18n/backend/gettext'
|
6
|
-
require 'i18n/gettext/helpers'
|
7
|
-
|
8
4
|
##
|
9
5
|
# This plugin provides "magical translations" in your .haml files. What does it
|
10
6
|
# mean? It's mean that all your raw texts in templates will be automatically
|
11
|
-
# translated by Gettext backend from I18n. No more
|
12
|
-
# and ugly translation methods in views. Now you can
|
13
|
-
# nothing more. At the end of your work you can easy
|
14
|
-
# and generate .po files for it. This type of files
|
15
|
-
# easier to translate, thanks to it you save your
|
7
|
+
# translated by GetText, FastGettext or Gettext backend from I18n. No more
|
8
|
+
# complicated translation keys and ugly translation methods in views. Now you can
|
9
|
+
# only write in your language, nothing more. At the end of your work you can easy
|
10
|
+
# find all phrases to translate and generate .po files for it. This type of files
|
11
|
+
# are also more readable and easier to translate, thanks to it you save your
|
12
|
+
# time with translations.
|
16
13
|
#
|
17
14
|
# === Examples
|
18
15
|
#
|
@@ -43,95 +40,120 @@ require 'i18n/gettext/helpers'
|
|
43
40
|
# translate.
|
44
41
|
#
|
45
42
|
module Haml::MagicTranslations
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
|
50
|
-
nuke_inner_whitespace, action, value, last_line = super(line)
|
51
|
-
|
52
|
-
magic_translations = self.options[:magic_translations]
|
53
|
-
magic_translations = Haml::Template.options[:magic_translations] if magic_translations.nil?
|
54
|
-
|
55
|
-
if magic_translations
|
56
|
-
unless action && action != '!' || action == '!' && value[0] == '=' || value.empty?
|
57
|
-
value, interpolation_arguments = prepare_i18n_interpolation(value)
|
58
|
-
value = "\#{_('#{value.gsub(/'/, "\\\\'")}') % #{interpolation_arguments}\}\n"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
[tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
|
62
|
-
nuke_inner_whitespace, action, value, last_line]
|
43
|
+
def self.included(haml) # :nodoc:
|
44
|
+
haml.send(:include, EngineMethods)
|
45
|
+
Haml::Template.send(:extend, TemplateMethods)
|
63
46
|
end
|
64
47
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
:
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
if contains_interpolation?(text)
|
81
|
-
options[:escape_html] = self.options[:escape_html] if options[:escape_html].nil?
|
82
|
-
push_script(
|
83
|
-
unescape_interpolation(text, :escape_html => options[:escape_html]),
|
84
|
-
:escape_html => false)
|
48
|
+
module TemplateMethods
|
49
|
+
def enable_magic_translations(backend = :i18n)
|
50
|
+
case backend
|
51
|
+
when :i18n
|
52
|
+
require 'i18n'
|
53
|
+
require 'i18n/backend/gettext'
|
54
|
+
require 'i18n/gettext/helpers'
|
55
|
+
I18n::Backend::Simple.send(:include, I18n::Backend::Gettext)
|
56
|
+
Haml::Helpers.send(:include, I18n::Gettext::Helpers)
|
57
|
+
when :gettext
|
58
|
+
require 'gettext'
|
59
|
+
Haml::Helpers.send(:include, GetText)
|
60
|
+
when :fast_gettext
|
61
|
+
require 'fast_gettext'
|
62
|
+
Haml::Helpers.send(:include, FastGettext::Translation)
|
85
63
|
else
|
86
|
-
|
64
|
+
raise ArgumentError, "Backend #{which.to_s} is not available in Haml::MagicTranslations"
|
87
65
|
end
|
66
|
+
Haml::Template.options[:magic_translations] = true
|
88
67
|
end
|
89
68
|
end
|
90
69
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
#
|
98
|
-
# %p This is some #{'Interpolated'.upcase'} text
|
99
|
-
#
|
100
|
-
# ... will be translated to:
|
101
|
-
#
|
102
|
-
# [ "This is some %s text", "['Interpolated'.upcase]" ]
|
103
|
-
#
|
104
|
-
def prepare_i18n_interpolation(str, opts = {})
|
105
|
-
args = []
|
106
|
-
res = ''
|
107
|
-
str = str.
|
108
|
-
gsub(/\n/, '\n').
|
109
|
-
gsub(/\r/, '\r').
|
110
|
-
gsub(/\#/, '\#').
|
111
|
-
gsub(/\"/, '\"').
|
112
|
-
gsub(/\\/, '\\\\')
|
70
|
+
module EngineMethods
|
71
|
+
# Overriden function that parses Haml tags. Injects gettext call for all plain
|
72
|
+
# text lines.
|
73
|
+
def parse_tag(line)
|
74
|
+
tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
|
75
|
+
nuke_inner_whitespace, action, value, last_line = super(line)
|
113
76
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
if
|
118
|
-
|
77
|
+
magic_translations = self.options[:magic_translations]
|
78
|
+
magic_translations = Haml::Template.options[:magic_translations] if magic_translations.nil?
|
79
|
+
|
80
|
+
if magic_translations
|
81
|
+
unless action && action != '!' || action == '!' && value[0] == '=' || value.empty?
|
82
|
+
value, interpolation_arguments = prepare_i18n_interpolation(value)
|
83
|
+
value = "\#{_('#{value.gsub(/'/, "\\\\'")}') % #{interpolation_arguments}\}\n"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
[tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
|
87
|
+
nuke_inner_whitespace, action, value, last_line]
|
88
|
+
end
|
89
|
+
|
90
|
+
# Magical translations will be also used for plain text.
|
91
|
+
def push_plain(text, options = {})
|
92
|
+
if block_opened?
|
93
|
+
raise SyntaxError.new("Illegal nesting: nesting within plain text is illegal.", @next_line.index)
|
94
|
+
end
|
95
|
+
|
96
|
+
options[:magic_translations] = self.options[:magic_translations] if options[:magic_translations].nil?
|
97
|
+
options[:magic_translations] = Haml::Template.options[:magic_translations] if options[:magic_translations].nil?
|
98
|
+
|
99
|
+
if options[:magic_translations]
|
100
|
+
value, interpolation_arguments = prepare_i18n_interpolation(text,
|
101
|
+
:escape_html => options[:escape_html])
|
102
|
+
value = "_('#{value.gsub(/'/, "\\\\'")}') % #{interpolation_arguments}\n"
|
103
|
+
push_script(value, :escape_html => false)
|
119
104
|
else
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
105
|
+
if contains_interpolation?(text)
|
106
|
+
options[:escape_html] = self.options[:escape_html] if options[:escape_html].nil?
|
107
|
+
push_script(
|
108
|
+
unescape_interpolation(text, :escape_html => options[:escape_html]),
|
109
|
+
:escape_html => false)
|
110
|
+
else
|
111
|
+
push_text text
|
112
|
+
end
|
124
113
|
end
|
125
114
|
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
115
|
+
|
116
|
+
# It discovers all fragments of code embeded in text and replacing with
|
117
|
+
# simple string interpolation parameters.
|
118
|
+
#
|
119
|
+
# ==== Example:
|
120
|
+
#
|
121
|
+
# Following line...
|
122
|
+
#
|
123
|
+
# %p This is some #{'Interpolated'.upcase'} text
|
124
|
+
#
|
125
|
+
# ... will be translated to:
|
126
|
+
#
|
127
|
+
# [ "This is some %s text", "['Interpolated'.upcase]" ]
|
128
|
+
#
|
129
|
+
def prepare_i18n_interpolation(str, opts = {})
|
130
|
+
args = []
|
131
|
+
res = ''
|
132
|
+
str = str.
|
133
|
+
gsub(/\n/, '\n').
|
134
|
+
gsub(/\r/, '\r').
|
135
|
+
gsub(/\#/, '\#').
|
136
|
+
gsub(/\"/, '\"').
|
137
|
+
gsub(/\\/, '\\\\')
|
138
|
+
|
139
|
+
rest = Haml::Shared.handle_interpolation '"' + str + '"' do |scan|
|
140
|
+
escapes = (scan[2].size - 1) / 2
|
141
|
+
res << scan.matched[0...-3 - escapes]
|
142
|
+
if escapes % 2 == 1
|
143
|
+
res << '#{'
|
144
|
+
else
|
145
|
+
content = eval('"' + balance(scan, ?{, ?}, 1)[0][0...-1] + '"')
|
146
|
+
content = "Haml::Helpers.html_escape(#{content.to_s})" if opts[:escape_html]
|
147
|
+
args << content
|
148
|
+
res << '%s'
|
149
|
+
end
|
150
|
+
end
|
151
|
+
value = res+rest.gsub(/\\(.)/, '\1').chomp
|
152
|
+
value = value[1..-2] unless value.blank?
|
153
|
+
args = "[#{args.join(', ')}]"
|
154
|
+
[value, args]
|
155
|
+
end
|
130
156
|
end
|
131
157
|
end
|
132
158
|
|
133
|
-
I18n::Backend::Simple.send(:include, I18n::Backend::Gettext)
|
134
159
|
Haml::Engine.send(:include, Haml::MagicTranslations)
|
135
|
-
Haml::Helpers.send(:include, I18n::Gettext::Helpers)
|
136
|
-
Haml::Template.options[:magic_translations] = true
|
137
|
-
|
@@ -1,12 +1,9 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper.rb')
|
1
|
+
require File.join(File.dirname(__FILE__), '../spec_helper.rb')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
locals = options.delete(:locals) || {}
|
6
|
-
Haml::Engine.new(text, options).to_html(scope, locals, &block)
|
7
|
-
end
|
3
|
+
Haml::Template.enable_magic_translations(:i18n)
|
4
|
+
I18n.load_path += Dir[File.join(File.dirname(__FILE__), "../locales/*.{po}")]
|
8
5
|
|
9
|
-
describe 'Haml magic translations' do
|
6
|
+
describe 'Haml magic translations with I18n' do
|
10
7
|
|
11
8
|
it 'should allow to set :magic_translations option in Haml::Template' do
|
12
9
|
Haml::Template.options.key?(:magic_translations).should be_true
|
data/spec/spec_helper.rb
CHANGED
@@ -13,6 +13,8 @@ require 'haml/magic_translations'
|
|
13
13
|
Haml::Template.options[:ugly] = false
|
14
14
|
Haml::Template.options[:format] = :xhtml
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def render(text, options = {}, &block)
|
17
|
+
scope = options.delete(:scope) || Object.new
|
18
|
+
locals = options.delete(:locals) || {}
|
19
|
+
Haml::Engine.new(text, options).to_html(scope, locals, &block)
|
20
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kriss Kowalik
|
@@ -31,42 +31,15 @@ dependencies:
|
|
31
31
|
version: 3.0.0
|
32
32
|
type: :development
|
33
33
|
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: i18n
|
36
|
-
prerelease: false
|
37
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - ">="
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
segments:
|
42
|
-
- 0
|
43
|
-
- 4
|
44
|
-
- 1
|
45
|
-
version: 0.4.1
|
46
|
-
type: :development
|
47
|
-
version_requirements: *id002
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: gettext
|
50
|
-
prerelease: false
|
51
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
-
requirements:
|
53
|
-
- - ">="
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
segments:
|
56
|
-
- 2
|
57
|
-
- 0
|
58
|
-
- 0
|
59
|
-
version: 2.0.0
|
60
|
-
type: :development
|
61
|
-
version_requirements: *id003
|
62
34
|
description: |
|
63
35
|
This plugin provides "magical translations" in your .haml files. What does it
|
64
36
|
mean? It's mean that all your raw texts in templates will be automatically
|
65
|
-
translated by Gettext backend from I18n. No more
|
66
|
-
and ugly translation methods in views. Now you can
|
67
|
-
nothing more. At the end of your work you can easy
|
68
|
-
and generate .po files for it. This type of files
|
69
|
-
easier to translate, thanks to it you save your
|
37
|
+
translated by GetText, FastGettext or Gettext backend from I18n. No more
|
38
|
+
complicated translation keys and ugly translation methods in views. Now you can
|
39
|
+
only write in your language, nothing more. At the end of your work you can easy
|
40
|
+
find all phrases to translate and generate .po files for it. This type of files
|
41
|
+
are also more readable and easier to translate, thanks to it you save your
|
42
|
+
time with translations.
|
70
43
|
|
71
44
|
email: kriss.kowalik@gmail.com
|
72
45
|
executables: []
|
@@ -89,7 +62,7 @@ files:
|
|
89
62
|
- lib/haml/magic_translations.rb
|
90
63
|
- spec/locales/en.po
|
91
64
|
- spec/locales/pl.po
|
92
|
-
- spec/
|
65
|
+
- spec/magic_translations/i18n_spec.rb
|
93
66
|
- spec/spec_helper.rb
|
94
67
|
has_rdoc: true
|
95
68
|
homepage: http://github.com/kriss/haml-magic-translations
|
@@ -123,4 +96,4 @@ specification_version: 3
|
|
123
96
|
summary: Provides automaticaly translations in haml templates
|
124
97
|
test_files:
|
125
98
|
- spec/spec_helper.rb
|
126
|
-
- spec/
|
99
|
+
- spec/magic_translations/i18n_spec.rb
|