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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 436e3fea5b855f2cafb0f0778615ac2c170c2d9c
4
- data.tar.gz: 74d014ed8dd18dd4185315a3993f130e117f9dfe
3
+ metadata.gz: ae5359c53ffe96b66366568f2790628519a13b71
4
+ data.tar.gz: ae39063b3c123f91947705d5cc2369e27bf0198b
5
5
  SHA512:
6
- metadata.gz: eb344e5fad00b7cbf7bc48721d294f54bc7a236b10d3edc706511af19f23ff29e8bacd942d91fb8e8a72ec86b32992c718cb3fc75ad48eb8424f8f1450bd17f6
7
- data.tar.gz: 54897ef4de7ec1aa26a2c6e280ccb97632851d0b80b7d0e0b4dac7b4b2441103ea80a0cc54aaf8db3dc95240406c0979e967e14f4702647112f06e63829279a6
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: ["ja", "en", "ja"]
34
+ languages: ["en", "ja"]
31
35
  ```
32
36
 
33
- The first language in the array will be the default language, Japanese and English will be added in to separate subfolders.
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/ja.po
39
- - _i18n/en/en.po
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
- Liquid tags. This will pick the correct string from the `language.po` file during compilation, or add it if no translation exists so you can fill it in later.
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
 
@@ -0,0 +1 @@
1
+ _plugins
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+ group :jekyll_plugins do
3
+ gem 'kramdown'
4
+ gem 'jekyll-watch'
5
+ gem 'fast_gettext'
6
+ gem 'get_pomo'
7
+ gem 'pry'
8
+ end
@@ -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,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,9 @@
1
+ {% assign n = page.title | size %}
2
+ {{ page.title }}
3
+ {% for i in (0..n) %}{% cycle "=", "-" %}{% endfor %}
4
+
5
+ {{ content }}
6
+
7
+
8
+ --------------------------------------------------------------------------------
9
+ {% t Language: %} {{ page.language }}
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+ group :jekyll_plugins do
3
+ gem 'kramdown'
4
+ gem 'jekyll-watch'
5
+ gem 'fast_gettext'
6
+ gem 'get_pomo'
7
+ gem 'pry'
8
+ end
@@ -0,0 +1,9 @@
1
+
2
+ Übersetzungsbeispiel
3
+ =-=-=-=-=-=-=-=-=-=-=-
4
+
5
+ Diese Datei wird in dem Ordner ihrer Sprache platziert.
6
+
7
+
8
+ --------------------------------------------------------------------------------
9
+ Sprache: de
@@ -0,0 +1,9 @@
1
+
2
+ Translation Example
3
+ =-=-=-=-=-=-=-=-=-=-
4
+
5
+ This file is placed in the folder of the language.
6
+
7
+
8
+ --------------------------------------------------------------------------------
9
+ Language: en
@@ -0,0 +1 @@
1
+ This file is just there and not translated.
@@ -0,0 +1,2 @@
1
+ Diese Datei wird nur nach Deutsch übersetzt.
2
+
@@ -0,0 +1 @@
1
+ This file is just there and not translated.
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: de
3
+ ---
4
+
5
+ {% t This file is translated into German only. %}
6
+
@@ -0,0 +1,7 @@
1
+ ---
2
+ layout: default
3
+ translate:
4
+ title: Translation Example
5
+ ---
6
+
7
+ {% t This file is placed in the folder of the language. %}
@@ -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
- alias :process_org :process
28
- attr_accessor :dest
29
- def process
30
- if !self.config['baseurl']
31
- self.config['baseurl'] = ""
32
- end
33
-
34
- # variables
35
- config['baseurl_root'] = self.config['baseurl']
36
- baseurl_org = self.config['baseurl']
37
- languages = self.config['languages']
38
- dest_org = self.dest
39
-
40
- # loop
41
- self.config['lang'] = languages.first
42
- puts
43
- puts "Building site for default language: \"#{self.config['lang']}\" to: " + self.dest
44
- self.load_translations
45
- process_org
46
- self.save_missing_translations
47
-
48
- languages.drop(1).each do |lang|
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
- def load_translations
67
- @all_translations = TranslationLogger.new
68
- @missing_translations = TranslationLogger.new
69
-
70
- repos = [
71
- FastGettext::TranslationRepository.build(self.config['lang'], :type=>:logger, :callback=>@all_translations),
72
- FastGettext::TranslationRepository.build(self.config['lang'], :type=>:logger, :callback=>@missing_translations),
73
- FastGettext::TranslationRepository.build(self.config['lang'], :path => self.source + "/_i18n", :type => :po)
74
- ]
75
- FastGettext.add_text_domain(self.config['lang'], :type=>:chain, :chain=>repos)
76
-
77
- FastGettext.text_domain = self.config['lang']
78
- FastGettext.locale = self.config['lang']
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 save_missing_translations
82
- self.posts.each {|post| _(post.title)}
83
- filename = self.source + "/_i18n/" + self.config['lang'] + '/' + self.config['lang'] + '.po'
84
- existing_translations = GetPomo.unique_translations(GetPomo::PoFile.parse(File.read(filename)))
85
-
86
- # ignores any keys that already exist
87
- missing_translations_msgids = @missing_translations.get_translations.reject {|msgid| existing_translations.find {|trans| trans.msgid == msgid}}
88
-
89
- final_translations = existing_translations
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
- missing_translations_msgids.each do |new_msgid|
92
- new_trans = GetPomo::Translation.new
93
- new_trans.msgid = new_msgid
94
- new_trans.msgstr = ""
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
- # uncomment this to remove translations that were not used
99
- # not_used = final_translations.reject { |trans| @all_translations.get_translations.find {|msgid| trans.msgid == msgid}}
100
- # final_translations = final_translations.reject {|trans1| not_used.find {|trans2| trans1.msgid == trans2.msgid}}
101
-
102
- final_translations.sort_by!(&:msgid)
103
-
104
- File.open(filename, 'w'){|f|f.print(GetPomo::PoFile.to_text(final_translations))}
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)
@@ -1,7 +1,7 @@
1
1
  module Jekyll
2
2
  module Gettext
3
3
  module Plugin
4
- VERSION = "0.0.4"
4
+ VERSION = "1.0.0"
5
5
  end
6
6
  end
7
7
  end
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
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-08-28 00:00:00.000000000 Z
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