-
Notifications
You must be signed in to change notification settings - Fork 0
/
localize.rb
39 lines (32 loc) · 926 Bytes
/
localize.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
module Jekyll
class LocalizeTag < Liquid::Tag
def initialize(tag_name, key, tokens)
super
@init = false
@key = key.strip
end
def render(context)
defaultsPath = "translations/defaults.yaml"
@lang = context.registers[:site].config['lang']
@translations = YAML::load(File.open("translations/#{@lang}.yaml"))
if File.exist? defaultsPath
@defaults = YAML::load(File.open(defaultsPath))
end
@init = true
if @key[0..3] == 'page'
tokens = @key.split('.')
@key = context.environments.first
while tokens.length > 0
token = tokens.shift
@key = @key[token]
end
end
result = @translations[@key]
if result.nil? and defined? @defaults
result = @defaults[@key]
end
"#{result}"
end
end
end
Liquid::Template.register_tag('localize', Jekyll::LocalizeTag)