lib/jekyll/translations.rb in jekyll-translations-1.0.0 vs lib/jekyll/translations.rb in jekyll-translations-2.0.0
- old
+ new
@@ -14,18 +14,20 @@
module TranslateFilter
@translations = []
@localizationContext = nil
@skipTranslationCheck = nil
+ @showTerm = false
@debug_translations = false
def translations(locale)
return @translations if @translations
site = @context.registers[:site]
config = site.config['translations']
@skipTranslationCheck = config['skipTranslationCheck']
+ @showTerm = !!config['showTerms']
@debug_translations = !@skipTranslationCheck and ENV['DEBUG_TRANSLATIONS'].to_i === 1
translation_data = site.data['translations'][locale]
translations = translation_data['common']
@@ -41,10 +43,32 @@
array.each { |item| translated_hash[item] = self.t(item) }
return translated_hash
end
+ def show_translation_term(translation, tag)
+ if @showTerm && !@showTermOverride
+ return "<#{tag} title=\"#{@lastTerm}\">#{translation}</#{tag}>"
+ end
+
+ return translation
+ end
+
+ def tns(text, args = [])
+ @showTermOverride = true;
+
+ translated = self.t(text, args)
+
+ @showTermOverride = false;
+
+ return translated
+ end
+
+ def ts(translation)
+ return self.show_translation_term(translation, 'div')
+ end
+
def t(text, args = [])
# If we've an array, translate each item and return
return self.translate_array(text) if text.kind_of?(Array)
page = @context.environments.first['page']
@@ -73,11 +97,18 @@
args = args.map { |arg| arg.to_s }
else
args = args.to_s
end
- return (@translations[text] % args) if not args.empty?
- return @translations[text]
+ if not args.empty?
+ translated = (@translations[text] % args)
+ else
+ translated = @translations[text]
+ end
+
+ @lastTerm = text
+
+ return self.show_translation_term(translated, 'span')
end
end
end
Liquid::Template.register_filter(Translations::TranslateFilter)