/usr/lib/ruby/vendor_ruby/haml/magic_translations.rb is in ruby-haml-magic-translations 4.0.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | # -*- coding: UTF-8 -*-
require 'haml'
require 'json'
##
# This plugin provides "magical translations" in your .haml files. What does it
# mean? It's mean that all your raw texts in templates will be automatically
# translated by GetText, FastGettext or Gettext backend from I18n. No more
# complicated translation keys and ugly translation methods in views. Now you can
# only write in your language, nothing more. At the end of your work you can easy
# find all phrases to translate and generate .po files for it. This type of files
# are also more readable and easier to translate, thanks to it you save your
# time with translations.
#
# === Examples
#
# Now you can write what you want, and at the end of work you
# will easy found all phrases to translate. Check out following example:
#
# %p This is my simple dummy text.
# %p And more lorem ipsum...
# %p= link_to _("This will be also translated"), "#"
#
# Those translations are allso allowing you to use standard Haml interpolation.
# You can easy write:
#
# %p This is my text with #{"interpolation".upcase}... Great, isn't it?
#
# And text from codes above will be stored in .po files as:
#
# # File test1.haml, line 1
# msgid "This is my simple dummy text"
# msgstr "This is my dummy translation of dummy text"
#
# # File test2.haml, line 1
# msgid "This is my text with %s... Great, isn't it?"
# msgstr "Next one %s translation!"
#
# Generator for .po files also includes information where your phrases are placed
# in filesystem. Thanks to it you don't forget about any even small word to
# translate.
#
module Haml::MagicTranslations
def self.included(haml) # :nodoc:
if defined? Haml::Template
Haml::Template.send(:extend, TemplateMethods)
end
end
class Parser < Haml::Parser
# Overriden function that parses Haml tags. Injects gettext call for all plain
# text lines.
def parse_tag(line)
tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
nuke_inner_whitespace, action, value, last_line = super(line)
if !value.empty?
unless action && action == '=' || action == '!' && value[0] == ?= || action == '&' && value[0] == ?=
value, interpolation_arguments = Haml::MagicTranslations.prepare_i18n_interpolation(value)
value = "\#{_('#{value.gsub(/'/, "\\\\'")}') % #{interpolation_arguments}\}\n"
end
end
[tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
nuke_inner_whitespace, action, value, last_line]
end
# Magical translations will be also used for plain text.
def plain(text, escape_html = nil)
value, interpolation_arguments = Haml::MagicTranslations.prepare_i18n_interpolation(text, escape_html)
value = "_('#{value.gsub(/'/, "\\\\'")}') % #{interpolation_arguments}\n"
script(value, !:escape_html)
end
end
class Compiler < Haml::Compiler
class << self
attr_accessor :magic_translations_helpers
end
def compile_filter
case @node.value[:name]
when 'markdown', 'maruku'
@node.value[:text] = "\#{_(<<-'END_OF_TRANSLATABLE_MARKDOWN'.rstrip
#{@node.value[:text].rstrip.gsub(/\n/, '\n')}
END_OF_TRANSLATABLE_MARKDOWN
)}"
when 'javascript'
@node.value[:text].gsub!(/_\(('(([^']|\\')+)'|"(([^"]|\\")+)")\)/) do |m|
to_parse = $1[1..-2].gsub(/"/, '\"')
parsed_string = JSON.parse("[\"#{to_parse}\"]")[0]
parsed_string.gsub!(/'/, "\\\\'")
"\#{_('#{parsed_string}').to_json}"
end
end
super
end
def compile_root
@precompiled << "extend #{Compiler.magic_translations_helpers};"
super
end
end
# It discovers all fragments of code embeded in text and replacing with
# simple string interpolation parameters.
#
# ==== Example:
#
# Following line...
#
# %p This is some #{'Interpolated'.upcase'} text
#
# ... will be translated to:
#
# [ "This is some %s text", "['Interpolated'.upcase]" ]
#
def self.prepare_i18n_interpolation(str, escape_html = nil)
args = []
res = ''
str = str.
gsub(/\n/, '\n').
gsub(/\r/, '\r').
gsub(/\#/, '\#').
gsub(/\"/, '\"').
gsub(/\\/, '\\\\')
rest = Haml::Util.handle_interpolation '"' + str + '"' do |scan|
escapes = (scan[2].size - 1) / 2
res << scan.matched[0...-3 - escapes]
if escapes % 2 == 1
res << '#{'
else
content = eval('"' + Haml::Util.balance(scan, ?{, ?}, 1)[0][0...-1] + '"')
content = "Haml::Helpers.html_escape(#{content.to_s})" if escape_html
args << content
res << '%s'
end
end
value = res+rest.gsub(/\\(.)/, '\1').chomp
value = value[1..-2] unless value.to_s == ''
args = "[#{args.join(', ')}]"
[value, args]
end
def self.enabled?
@enabled
end
# Enable magic translations using the given backend
#
# Supported backends:
#
# +:i18n+:: Use I18n::Backend::GetText and I18n::GetText::Helpers
# from the 'i18n'
# +:gettext+:: Use GetText from 'gettext'
# +:fast_gettext+:: Use FastGettext::Translation from 'fast_gettext'
def self.enable(backend = :i18n)
return if @enabled
case backend
when :i18n
require 'i18n'
require 'i18n/backend/gettext'
require 'i18n/gettext/helpers'
I18n::Backend::Simple.send(:include, I18n::Backend::Gettext)
Compiler.magic_translations_helpers = I18n::Gettext::Helpers
when :gettext
require 'gettext'
Compiler.magic_translations_helpers = GetText
when :fast_gettext
require 'fast_gettext'
Compiler.magic_translations_helpers = FastGettext::Translation
else
@enabled = false
raise ArgumentError, "Backend #{backend.to_s} is not available in Haml::MagicTranslations"
end
@original_parser = Haml::Options.defaults[:parser_class]
Haml::Options.defaults[:parser_class] = Parser
@original_compiler = Haml::Options.defaults[:compiler_class]
Haml::Options.defaults[:compiler_class] = Compiler
@enabled = true
end
# Disable magic translations
def self.disable
return unless @enabled
@enabled = false
Haml::Options.defaults[:compiler_class] = @original_compiler
@original_compiler = nil
Haml::Options.defaults[:parser_class] = @original_parser
@original_parser = nil
Compiler.magic_translations_helpers = nil
end
module TemplateMethods # :nodoc:all
# backward compatibility with versions < 0.3
def enable_magic_translations(backend = :i18n)
Haml::MagicTranslations.enable backend
end
end
end
Haml::Engine.send(:include, Haml::MagicTranslations)
|