jekyll-gettext-plugin 0.0.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +69 -5
- data/examples/.gitignore +1 -0
- data/examples/default-configuration/Gemfile +8 -0
- data/examples/default-configuration/_config.yml +10 -0
- data/examples/default-configuration/_i18n/.gitignore +1 -0
- data/examples/default-configuration/_i18n/de/website.po +25 -0
- data/examples/default-configuration/_i18n/website.pot +19 -0
- data/examples/default-configuration/_layouts/default.txt +9 -0
- data/examples/default-configuration/_site/Gemfile +8 -0
- data/examples/default-configuration/_site/de/translated-file.txt +9 -0
- data/examples/default-configuration/_site/en/translated-file.txt +9 -0
- data/examples/default-configuration/_site/file.txt +1 -0
- data/examples/default-configuration/_site/german-file.txt +2 -0
- data/examples/default-configuration/file.txt +1 -0
- data/examples/default-configuration/german-file.txt +6 -0
- data/examples/default-configuration/translated-file.txt +7 -0
- data/lib/jekyll/gettext/plugin.rb +166 -76
- data/lib/jekyll/gettext/plugin/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae5359c53ffe96b66366568f2790628519a13b71
|
4
|
+
data.tar.gz: ae39063b3c123f91947705d5cc2369e27bf0198b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b283a7c67d9cc119e2a7a47c842edf9374a8ee8769516dfd91024a2d6c1ca8e48cc05253bd2008665f88178642b1890d38ed38046342ab6c404ab4cf0da490e2
|
7
|
+
data.tar.gz: e90416ea03eaf7a88021fefe10dd0be86bd36b615b8cdf996ecb4f10e017fb7f60374ad21747c4baef035af947a9750fbead8fa9255d11286eaa408a02306731
|
data/README.md
CHANGED
@@ -22,31 +22,95 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
+
###Examples
|
26
|
+
|
27
|
+
Have a look at the [examples](examples) folder.
|
25
28
|
|
26
29
|
###Configuration
|
30
|
+
|
27
31
|
Add the i18n configuration to your _config.yml:
|
28
32
|
|
29
33
|
```yaml
|
30
|
-
languages: ["
|
34
|
+
languages: ["en", "ja"]
|
31
35
|
```
|
32
36
|
|
33
|
-
The first language in the array will be the default language
|
37
|
+
The first language in the array will be the default language.
|
38
|
+
|
39
|
+
Default configuration values:
|
40
|
+
|
41
|
+
```yaml
|
42
|
+
# the languages to look for
|
43
|
+
languages: ["en"]
|
44
|
+
|
45
|
+
# the name of the text domain.
|
46
|
+
# This is the name of the .pot and .po files.
|
47
|
+
text_domain: "website"
|
48
|
+
|
49
|
+
# the folder relative to the _config.yml
|
50
|
+
# where the translations are placed inside
|
51
|
+
translations_folder: "_i18n"
|
52
|
+
|
53
|
+
# the files that are ignored by jekyll.
|
54
|
+
# This variable is automatically filled to avoid build loops
|
55
|
+
exclude: ["_i18n/website.pot"]
|
56
|
+
```
|
34
57
|
|
35
58
|
###i18n
|
59
|
+
|
36
60
|
Create this folder structure in your Jekyll project as an example:
|
37
61
|
|
38
|
-
- _i18n/ja/
|
39
|
-
- _i18n/en/
|
62
|
+
- _i18n/ja/website.po
|
63
|
+
- _i18n/en/website.po
|
40
64
|
|
41
65
|
To add a string to your site use one of these
|
42
66
|
|
43
67
|
```liquid
|
68
|
+
---
|
69
|
+
translate: true
|
70
|
+
---
|
71
|
+
|
44
72
|
{% t key %}
|
45
73
|
or
|
46
74
|
{% translate key %}
|
47
75
|
```
|
48
76
|
|
49
|
-
|
77
|
+
These are liquid tags. They will pick the correct string from the correct `website.po` file during compilation for that language, or add it if no translation exists so you can fill it in later.
|
78
|
+
|
79
|
+
All files with a `translate` field inside the yaml header are put in the folders of their languages.
|
80
|
+
Without the `translate` field they remain where they are as usual.
|
81
|
+
|
82
|
+
You can translate variables in the header like this:
|
83
|
+
|
84
|
+
```liquid
|
85
|
+
---
|
86
|
+
translate:
|
87
|
+
title: Main Page
|
88
|
+
---
|
89
|
+
```
|
90
|
+
|
91
|
+
All translations automatically turn up in the `_i18n/website.pot` file.
|
92
|
+
You can translate them using [Poedit](https://poedit.net/download).
|
93
|
+
Whenever you save the po file, a `jekyll serve` rebuilds the site.
|
94
|
+
|
95
|
+
Make sure all files are saved in UTF-8 encoding.
|
96
|
+
|
97
|
+
All files with a `translate` field inside the yaml header are put in the folders of their languages.
|
98
|
+
Without the `translate` field they remain where they are as usual.
|
99
|
+
|
100
|
+
You can translate variables in the header like this:
|
101
|
+
|
102
|
+
```liquid
|
103
|
+
---
|
104
|
+
translate:
|
105
|
+
title: Main Page
|
106
|
+
---
|
107
|
+
```
|
108
|
+
|
109
|
+
All translations automatically turn up in the `_i18n/website.pot` file.
|
110
|
+
You can translate them using [Poedit](https://poedit.net/download).
|
111
|
+
Whenever you save the po file, a `jekyll serve` rebuilds the site.
|
112
|
+
|
113
|
+
Make sure all files are saved in UTF-8 encoding.
|
50
114
|
|
51
115
|
## Contributing
|
52
116
|
|
data/examples/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
_plugins
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# the languages to look for
|
2
|
+
languages: ["en", "de"]
|
3
|
+
|
4
|
+
# the name of the text domain.
|
5
|
+
# This is the name of the .pot and .po files.
|
6
|
+
text_domain: "website"
|
7
|
+
|
8
|
+
# the folder relative to the _config.yml
|
9
|
+
# where the translations are placed inside
|
10
|
+
translations_folder: "_i18n"
|
@@ -0,0 +1 @@
|
|
1
|
+
*.mo
|
@@ -0,0 +1,25 @@
|
|
1
|
+
msgid ""
|
2
|
+
msgstr ""
|
3
|
+
"Project-Id-Version: \n"
|
4
|
+
"POT-Creation-Date: \n"
|
5
|
+
"PO-Revision-Date: \n"
|
6
|
+
"Last-Translator: \n"
|
7
|
+
"Language-Team: \n"
|
8
|
+
"Language: de\n"
|
9
|
+
"MIME-Version: 1.0\n"
|
10
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
11
|
+
"Content-Transfer-Encoding: 8bit\n"
|
12
|
+
"X-Generator: Poedit 1.8.8\n"
|
13
|
+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
14
|
+
|
15
|
+
msgid "Translation Example"
|
16
|
+
msgstr "Übersetzungsbeispiel"
|
17
|
+
|
18
|
+
msgid "This file is translated into German only."
|
19
|
+
msgstr "Diese Datei wird nur nach Deutsch übersetzt."
|
20
|
+
|
21
|
+
msgid "This file is placed in the folder of the language."
|
22
|
+
msgstr "Diese Datei wird in dem Ordner ihrer Sprache platziert."
|
23
|
+
|
24
|
+
msgid "Language:"
|
25
|
+
msgstr "Sprache:"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
msgid ""
|
2
|
+
msgstr ""
|
3
|
+
"MIME-Version: 1.0\n"
|
4
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
5
|
+
"Content-Transfer-Encoding: 8bit\n"
|
6
|
+
"Language: en\n"
|
7
|
+
"X-Generator: Jekyll::PotLocalizationPlugin\n"
|
8
|
+
|
9
|
+
msgid "Translation Example"
|
10
|
+
msgstr ""
|
11
|
+
|
12
|
+
msgid "This file is translated into German only."
|
13
|
+
msgstr ""
|
14
|
+
|
15
|
+
msgid "This file is placed in the folder of the language."
|
16
|
+
msgstr ""
|
17
|
+
|
18
|
+
msgid "Language:"
|
19
|
+
msgstr ""
|
@@ -0,0 +1 @@
|
|
1
|
+
This file is just there and not translated.
|
@@ -0,0 +1 @@
|
|
1
|
+
This file is just there and not translated.
|
@@ -2,106 +2,177 @@ require "jekyll/gettext/plugin/version"
|
|
2
2
|
|
3
3
|
require 'fast_gettext'
|
4
4
|
require 'get_pomo'
|
5
|
-
|
6
5
|
require 'pry'
|
7
6
|
|
8
7
|
class TranslationLogger
|
8
|
+
|
9
9
|
def initialize
|
10
10
|
@translations = []
|
11
11
|
end
|
12
12
|
|
13
|
-
def get_translations
|
14
|
-
return @translations
|
15
|
-
end
|
16
|
-
|
17
13
|
def call(unfound)
|
18
14
|
@translations.push(unfound)
|
19
15
|
end
|
16
|
+
|
17
|
+
def translations
|
18
|
+
@translations.map do |msgid|
|
19
|
+
translation = GetPomo::Translation.new
|
20
|
+
translation.msgid = msgid
|
21
|
+
translation.msgstr = ""
|
22
|
+
translation
|
23
|
+
end
|
24
|
+
end
|
20
25
|
end
|
21
26
|
|
22
27
|
module Jekyll
|
23
28
|
|
29
|
+
class Page
|
30
|
+
attr_reader :base
|
31
|
+
|
32
|
+
def activate
|
33
|
+
raise "Add 'translate: true' to " + relative_path + "."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
24
37
|
class Site
|
38
|
+
attr_accessor :pot_localization_plugin
|
39
|
+
end
|
40
|
+
|
41
|
+
class TranslatedPage < Page
|
25
42
|
include FastGettext::Translation
|
26
43
|
|
27
|
-
|
28
|
-
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
# build site for language lang
|
51
|
-
self.dest = self.dest + "/" + lang
|
52
|
-
self.config['baseurl'] = self.config['baseurl'] + "/" + lang
|
53
|
-
self.config['lang'] = lang
|
54
|
-
puts "Building site for language: \"#{self.config['lang']}\" to: " + self.dest
|
55
|
-
self.load_translations
|
56
|
-
process_org
|
57
|
-
self.save_missing_translations
|
58
|
-
|
59
|
-
# reset variables for next language
|
60
|
-
self.dest = dest_org
|
61
|
-
self.config['baseurl'] = baseurl_org
|
44
|
+
attr_reader :language, :default_language
|
45
|
+
|
46
|
+
def initialize(page, language, default_language)
|
47
|
+
@language = language
|
48
|
+
@default_language = default_language
|
49
|
+
super(page.site, page.base, page.dir, page.name)
|
50
|
+
fill_data
|
51
|
+
end
|
52
|
+
|
53
|
+
def activate
|
54
|
+
FastGettext.locale = language
|
55
|
+
end
|
56
|
+
|
57
|
+
def fill_data
|
58
|
+
activate
|
59
|
+
data["language"]= language
|
60
|
+
data["default_language"]= default_language
|
61
|
+
translate = data["translate"]
|
62
|
+
if translate.is_a? Hash
|
63
|
+
translate.each_pair do |key, value|
|
64
|
+
data[key] = _(value)
|
65
|
+
end
|
62
66
|
end
|
63
|
-
puts 'Build complete'
|
64
67
|
end
|
68
|
+
|
69
|
+
def url
|
70
|
+
"/" + language + super
|
71
|
+
end
|
72
|
+
end
|
65
73
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
74
|
+
class PotLocalizationPlugin < Generator
|
75
|
+
safe true
|
76
|
+
|
77
|
+
attr_reader :site, :translations
|
78
|
+
|
79
|
+
def text_domain
|
80
|
+
unless site.config.include? "text_domain"
|
81
|
+
site.config["text_domain"] = "website"
|
82
|
+
end
|
83
|
+
site.config["text_domain"]
|
84
|
+
end
|
85
|
+
|
86
|
+
def pot_file
|
87
|
+
translations_folder + "/" + text_domain + ".pot"
|
88
|
+
end
|
89
|
+
|
90
|
+
def languages
|
91
|
+
unless site.config.include? "languages"
|
92
|
+
site.config["languages"] = ["en"]
|
93
|
+
end
|
94
|
+
site.config["languages"]
|
95
|
+
end
|
96
|
+
|
97
|
+
def po_file(language)
|
98
|
+
translations_folder + "/" + language + "/" + text_domain + ".po"
|
79
99
|
end
|
80
100
|
|
81
|
-
def
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
101
|
+
def generate(site)
|
102
|
+
@site = site
|
103
|
+
site.pot_localization_plugin = self
|
104
|
+
setup_translations
|
105
|
+
add_translated_sites
|
106
|
+
end
|
107
|
+
|
108
|
+
def setup_translations
|
109
|
+
@translations = TranslationLogger.new
|
90
110
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
final_translations.push(new_trans)
|
111
|
+
renew_translations
|
112
|
+
|
113
|
+
Hooks.register(:site, :pre_render) do |_site, payload|
|
114
|
+
renew_translations
|
96
115
|
end
|
97
116
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
117
|
+
Hooks.register(:site, :post_write) do |_site, payload|
|
118
|
+
if _site == site and not translations.nil?
|
119
|
+
save_translations translations.translations
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def renew_translations
|
125
|
+
languages.each do |language|
|
126
|
+
repos = [
|
127
|
+
FastGettext::TranslationRepository.build(text_domain, :type=>:logger, :callback=>translations),
|
128
|
+
FastGettext::TranslationRepository.build(text_domain, :path => translations_folder, :type => :po)
|
129
|
+
]
|
130
|
+
FastGettext.add_text_domain(text_domain, :type=>:chain, :chain=>repos)
|
131
|
+
end
|
132
|
+
FastGettext.text_domain = text_domain
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
# use the "translations_folder" tag from the _config.yml or default "_i18n"
|
137
|
+
def translations_folder
|
138
|
+
site.in_source_dir(site.config["translations_folder"] || "_i18n")
|
139
|
+
end
|
140
|
+
|
141
|
+
def add_translated_sites
|
142
|
+
default_language = languages.first
|
143
|
+
translated_pages = []
|
144
|
+
site.pages.reject! do |page|
|
145
|
+
to_translate = page.data["translate"]
|
146
|
+
if to_translate
|
147
|
+
languages.each do |language|
|
148
|
+
translated_pages << TranslatedPage.new(page, language, default_language)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
to_translate
|
152
|
+
end
|
153
|
+
site.pages.concat(translated_pages)
|
154
|
+
end
|
155
|
+
|
156
|
+
def save_translations(translations)
|
157
|
+
if translations.empty?
|
158
|
+
return
|
159
|
+
end
|
160
|
+
site.config["exclude"] ||= []
|
161
|
+
unless site.config["exclude"].include? pot_file
|
162
|
+
site.config["exclude"] << pot_file
|
163
|
+
end
|
164
|
+
File.open(pot_file, 'w') do |file|
|
165
|
+
file.write('msgid ""
|
166
|
+
msgstr ""
|
167
|
+
"MIME-Version: 1.0\n"
|
168
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
169
|
+
"Content-Transfer-Encoding: 8bit\n"
|
170
|
+
"Language: ' + languages.first + '\n"
|
171
|
+
"X-Generator: ' + self.class.name + '\n"
|
172
|
+
|
173
|
+
')
|
174
|
+
file.print(GetPomo::PoFile.to_text(translations))
|
175
|
+
end
|
105
176
|
end
|
106
177
|
end
|
107
178
|
|
@@ -114,6 +185,17 @@ module Jekyll
|
|
114
185
|
end
|
115
186
|
|
116
187
|
def render(context)
|
188
|
+
site = context.registers[:site]
|
189
|
+
page = context.registers[:page]
|
190
|
+
language = page["language"]
|
191
|
+
if language.nil?
|
192
|
+
default_language = page["default_language"].to_s
|
193
|
+
raise ("Missing language! Either put 'language: " + default_language +
|
194
|
+
"' or 'translate: true' into the header")
|
195
|
+
end
|
196
|
+
add_localization_to_dependency(site, language, page["path"]) if page.key?("path")
|
197
|
+
|
198
|
+
FastGettext.locale = language
|
117
199
|
candidate = _(@key)
|
118
200
|
|
119
201
|
if candidate == ""
|
@@ -121,8 +203,16 @@ module Jekyll
|
|
121
203
|
end
|
122
204
|
candidate
|
123
205
|
end
|
206
|
+
|
207
|
+
def add_localization_to_dependency(site, language, path)
|
208
|
+
po_file = site.pot_localization_plugin.po_file(language)
|
209
|
+
site.regenerator.add_dependency(
|
210
|
+
site.in_source_dir(path),
|
211
|
+
po_file
|
212
|
+
)
|
213
|
+
end
|
124
214
|
end
|
125
215
|
end
|
126
216
|
|
127
217
|
Liquid::Template.register_tag('t', Jekyll::LocalizeTag)
|
128
|
-
Liquid::Template.register_tag('translate', Jekyll::LocalizeTag)
|
218
|
+
Liquid::Template.register_tag('translate', Jekyll::LocalizeTag)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-gettext-plugin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Doyle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fast_gettext
|
@@ -93,6 +93,21 @@ files:
|
|
93
93
|
- LICENSE.txt
|
94
94
|
- README.md
|
95
95
|
- Rakefile
|
96
|
+
- examples/.gitignore
|
97
|
+
- examples/default-configuration/Gemfile
|
98
|
+
- examples/default-configuration/_config.yml
|
99
|
+
- examples/default-configuration/_i18n/.gitignore
|
100
|
+
- examples/default-configuration/_i18n/de/website.po
|
101
|
+
- examples/default-configuration/_i18n/website.pot
|
102
|
+
- examples/default-configuration/_layouts/default.txt
|
103
|
+
- examples/default-configuration/_site/Gemfile
|
104
|
+
- examples/default-configuration/_site/de/translated-file.txt
|
105
|
+
- examples/default-configuration/_site/en/translated-file.txt
|
106
|
+
- examples/default-configuration/_site/file.txt
|
107
|
+
- examples/default-configuration/_site/german-file.txt
|
108
|
+
- examples/default-configuration/file.txt
|
109
|
+
- examples/default-configuration/german-file.txt
|
110
|
+
- examples/default-configuration/translated-file.txt
|
96
111
|
- jekyll-gettext-plugin.gemspec
|
97
112
|
- lib/jekyll/gettext/plugin.rb
|
98
113
|
- lib/jekyll/gettext/plugin/version.rb
|