forked from bitcoin-dot-org/Bitcoin.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.rb
113 lines (99 loc) · 3.42 KB
/
translate.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
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
# This file is licensed under the MIT License (MIT) available on
# http://opensource.org/licenses/MIT.
#translate( id [,category ,lang] )
#Return translated string using translations files
#category and lang are set to current page.id and page.lang, but they can
#also be set manually to get translations for global layout and urls.
#Example: {% translate button-wallet layout %} will return the
#translated button-wallet string for the global layout file
#dynamic variables can be used as arguments
#Example: {% translate menu-{{id}} %}
#urls and anchors are automatically replaced and translated.
#Example: #vocabulary##[vocabulary.wallet] is replaced by
#/en/vocabulary#wallet when the page is in english or
#/fr/vocabulaire#porte-monnaie when the page is in french.
require 'yaml'
require 'cgi'
module Jekyll
class TranslateTag < Liquid::Tag
def initialize(tag_name, id, tokens)
super
@id = id
end
def render(context)
#Load translations
site = context.registers[:site].config
if !site.has_key?("loc")
site['loc'] = {}
Dir.foreach('_translations') do |file|
next if file == '.' or file == '..'
lang=file.split('.')[0]
site['loc'][lang] = YAML.load_file('_translations/' file)[lang]
end
end
#define id, category and lang
defaulten = true
lang = Liquid::Template.parse("{{page.lang}}").render context
cat = Liquid::Template.parse("{{page.id}}").render context
[email protected](' ')
if !id[1].nil?
cat = Liquid::Template.parse(id[1]).render context
end
if !id[2].nil?
lang = Liquid::Template.parse(id[2]).render context
defaulten = false
end
id=Liquid::Template.parse(id[0]).render context
if lang == ''
lang = 'en'
end
#get translated string
text = ''
keys = cat.split('.')
ar = site['loc'][lang]
#recursive loop to handle cases where category is like "anchor.vocabulary"
for key in keys do
break if !ar.is_a?(Hash) || !ar.has_key?(key) || !ar[key].is_a?(Hash)
ar = ar[key]
end
if ar.has_key?(id) && ar[id].is_a?(String)
text = ar[id]
end
#fallback to English if string is empty
if text == '' and defaulten == true
lang = 'en'
ar = site['loc'][lang]
for key in keys do
break if !ar.is_a?(Hash) || !ar.has_key?(key) || !ar[key].is_a?(Hash)
ar = ar[key]
end
if ar.has_key?(id) && ar[id].is_a?(String)
text = ar[id]
end
end
#interpret Liquid templating in string
text = Liquid::Template.parse(text).render context
#replace urls and anchors in string
url = site['loc'][lang]['url']
url.each do |key,value|
if !value.nil?
text.gsub!("#" key "#",'/' lang '/' CGI::escape(value))
end
end
## Hack for renaming links to the Bitcoin paper. Safe to remove
## when all languages have "bitcoin-paper:" defined in the "url:"
## section of their '_translations' YAML file.
text.gsub!('#bitcoin-paper#','/bitcoin.pdf')
anc = site['loc'][lang]['anchor']
anc.each do |page,anch|
anch.each do |key,value|
if !value.nil?
text.gsub!("[" page '.' key "]",CGI::escape(value))
end
end
end
text
end
end
end
Liquid::Template.register_tag('translate', Jekyll::TranslateTag)