Class: I18nTemplate::Document
- Inherits:
-
Object
- Object
- I18nTemplate::Document
- Defined in:
- lib/i18n_template/document.rb
Overview
I18nTemplate::Document processes on the fly xhtml document internationalization.
Next document will be automatically internationalized
<body>
<% current_year = Time.now.year %>
<span i18n="p">hello</span>
<h2>Dashboard</h2>
<div>Posts count: <%= current_user.posts.count %></div>
<div>Click<a href="#">here</a></div>
</body>
to:
<body>
<% current_year = Time.now.year %>
<span>
<%- i18n_variables = {}; i18n_wrappers = [] -%>
<%= ::I18nTemplate::Translation.translate("hello", i18n_wrappers, i18n_variables) %>
</span>
<h2>
<%- i18n_variables = {}; i18n_wrappers = [] -%>
<%= ::I18nTemplate::Translation.translate("Dashboard", i18n_wrappers, i18n_variables) %>
</h2>
<div>
<%- i18n_variables = {}; i18n_wrappers = [] -%>
<%- i18n_variables['current user posts count'] = capture do -%>
<%= current_user.posts.count %>
<%- end -%>
<%= ::I18nTemplate::Translation.translate("Posts count: {current user posts count}",
i18n_wrappers, i18n_variables) %>
</div>
<div>
<%- i18n_variables = {}; i18n_wrappers = [] -%>
<%- i18n_wrappers[1] = capture do -%>
<a href="#" i18n_wrapper="1">
<%- i18n_variables = {}; i18n_wrappers = [] -%>
<%= ::I18nTemplate::Translation.translate("here", i18n_wrappers, i18n_variables) %>
</a>
<%- end -%>
<%= ::I18nTemplate::Translation.translate("Click[1]here[/1]", i18n_wrappers, i18n_variables) %>
</div>
</body>
So you need just tp translate next phrases:
-
hello
-
Dashboard
-
_Posts count: user posts count_
-
Click[1]here[/1]
I18n special markup element/attributes:
-
<i18n>content</i18n> - mark invisible for parser content for internationalization
-
<… i18n=“i” …>content<…> - (ignore) ignore element content internationalization
-
<… i18n=“p” …>content<…> - (phrase) explicitly enable content internationalization
-
<… i18n=“s” …>content<…> - (subphrase) mark element content as subphrase for parent element phrase
Internal i18n element/attributes/scriptlets:
-
< … i18n_phrase=“phrase content” …> - set extracted phrase into attribute
-
< … i18n_wrapper=“position” …> - mark element as wrapper as position in i18n_wrappers array
-
<i18n_variable name=“variable name”>variable value</i18n_variable> - holds captured variable value with specified variable name from i18n_variables hash
-
<% i18n_wrappers %> - array of captured wrapper contents
-
<% i18n_variables %> - hash of name-value where name is variable name and value is captured variable value
Constant Summary collapse
- FOLD_START =
a symbol that means fold start
[0x2264].pack("U*").freeze
- FOLD_END =
a symbol that means fold end
[0x2265].pack("U*").freeze
- FOLDS =
folds mapping
[ [ 'ignore', /<!DOCTYPE--.+?-->/m ], [ 'ignore', /<script[^>]*?>.+?<\/script>/m ], [ 'ignore', /<!--.+?-->/m ], [ 'ignore', /<style[^>]*?>.+?<\/style>/m ], [ 'eval', /<select.+?<\/select>/m ], [ 'ignore', /<%[^=](.*?)%>/m ], [ 'eval', /<%=(.*?)%>/m ] ].freeze
- FOLD =
$1 - fold index $2 - fold type e.g (eval, ignore)
/#{FOLD_START}(\d+):(\w+)#{FOLD_END}/.freeze
- OPEN_TAG =
$1 tag name. E.g a-b:c_d
/^<(\w+(:[\w_-]+)?)/.freeze
- CLOSED_TAG =
$1 tag name. E.g a-b:c_d
/<\/(\w+(:[\w_-]+)?)>/.freeze
- SELF_CLOSE =
/\/>$/.freeze
- BLOCK_TAGS =
%w( i18n address blockquote p div h1 h2 h3 h4 h5 h6 li dd dt td th a legend label title caption option optgroup button ).freeze
- HTML_ENTITY =
© ©
/&(#\d+|\w+);/
Instance Attribute Summary collapse
-
#folds ⇒ Object
readonly
array of folds.
-
#node_stack ⇒ Object
readonly
stack of document nodes.
-
#phrases ⇒ Object
readonly
array of translation phrases.
-
#root_node ⇒ Object
readonly
root document node.
-
#source ⇒ Object
readonly
processed document source.
-
#warnings ⇒ Object
readonly
array of processing warings.
Instance Method Summary collapse
-
#initialize(source) ⇒ Document
constructor
Initialize document processor.
-
#preprocess! ⇒ Object
Pre process document: * add translation key attributes * extract translation phrases * modify document source.
-
#preprocessed? ⇒ Boolean
return true if document is preprocessed?.
-
#process! ⇒ Object
Processs a document: * expand translation keys * modify document source.
-
#processed? ⇒ Boolean
return true if document is processed?.
Constructor Details
#initialize(source) ⇒ Document
Initialize document processor
134 135 136 137 138 139 |
# File 'lib/i18n_template/document.rb', line 134 def initialize(source) @source = source.dup @warnings = [] @folds = [] @phrases = [] end |
Instance Attribute Details
#folds ⇒ Object (readonly)
array of folds
121 122 123 |
# File 'lib/i18n_template/document.rb', line 121 def folds @folds end |
#node_stack ⇒ Object (readonly)
stack of document nodes
130 131 132 |
# File 'lib/i18n_template/document.rb', line 130 def node_stack @node_stack end |
#phrases ⇒ Object (readonly)
array of translation phrases
124 125 126 |
# File 'lib/i18n_template/document.rb', line 124 def phrases @phrases end |
#root_node ⇒ Object (readonly)
root document node
127 128 129 |
# File 'lib/i18n_template/document.rb', line 127 def root_node @root_node end |
#source ⇒ Object (readonly)
processed document source
115 116 117 |
# File 'lib/i18n_template/document.rb', line 115 def source @source end |
#warnings ⇒ Object (readonly)
array of processing warings
118 119 120 |
# File 'lib/i18n_template/document.rb', line 118 def warnings @warnings end |
Instance Method Details
#preprocess! ⇒ Object
Pre process document:
-
add translation key attributes
-
extract translation phrases
-
modify document source
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/i18n_template/document.rb', line 146 def preprocess! raise "Document is already preprocessed" if @preprocessed parse_nodes do |node| set_node_phrase(node) end @source = "" @node_stack.each do |node| @source << node_to_text(node) end @preprocessed = true end |
#preprocessed? ⇒ Boolean
return true if document is preprocessed?
180 181 182 |
# File 'lib/i18n_template/document.rb', line 180 def preprocessed? @preprocessed end |
#process! ⇒ Object
Processs a document:
-
expand translation keys
-
modify document source
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/i18n_template/document.rb', line 166 def process! raise "Document is already processed" if @processed preprocess! parse_nodes @source = "" @root_node.children.each { |node| translate_node(node) } @processed = true end |
#processed? ⇒ Boolean
return true if document is processed?
185 186 187 |
# File 'lib/i18n_template/document.rb', line 185 def processed? @processed end |