-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht.rb
59 lines (49 loc) · 1.8 KB
/
t.rb
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
module Jekyll
require 'yaml'
class TTag < Liquid::Tag
# A plugin for adding some I18n to your jekyll.
# 1. Create a _locales folder in your jekyll root.
# 2. For each locale you would like to support, add a .yml file.
# 3. Example: _locales/en.yml
# 4. Set an environment variable with ENV["JLANG"], or set a value in the
# site config vars. I.e. locale: "en"
# 5. Call {% t hello_world %} in your views. It will search the .yml file
# for the respective key and return the translation
@@locale = nil
@@locales = nil
def initialize(tag_name, text, tokens)
super
# There can be whitespace on the key, ensure it is removed
@text = text.strip
end
def render(context)
return unless set_locale(context)
translate(@text, context)
end
def translate(translation_key, context)
translation = @@locales[translation_key]
missing_translation(translation_key, context) unless translation
translation || translation_key
end
def missing_translation(translation_key, context)
page = context.registers[:page]
puts "= Missing translation ="
puts "Please translate: #{page["url"] + page["path"]} - #{translation_key}"
puts "= End Missing translation ="
end
def set_locale(context)
@@locale = @@locale || ENV["JLANG"] || context.registers[:site].config["locale"]
if @@locale
@@locales = @@locales ||
begin
YAML.load_file("_locales/#{@@locale}.yml")
rescue
raise "please make sure that you: Created the _locales in your root jekyll folder
&& Properly created the language file for #{@@locale}. Caught "
end
end
@@locale && @@locales
end
end
end
Liquid::Template.register_tag('t', Jekyll::TTag)