octopress-multilingual 0.2.3 → 0.3.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: 46557f90d2d061e6f23d42976d53a040f09f8f45
4
- data.tar.gz: daabecf689490d31f77ee6983e0b1169df486b2b
3
+ metadata.gz: 5846ed8df08ad7f6a0f3116a97eb703e90d354d7
4
+ data.tar.gz: 0797e18cc9f11f1f7633fd4b0ccf68a4d8acc297
5
5
  SHA512:
6
- metadata.gz: f6aaa3a0ff312a958f1fac98ed54e1504ffae38bc3acb691d45011557d39a8c434580326f82d269674015345f841b5dee4f123cee91e0417707a4a7202891f0b
7
- data.tar.gz: aab67497b57a2d25d19b6028bab7ad0b04ead84169ee31ba33e3a844bc2bdd57e77e3cc0802f6a173b770083f56a576e41d7e752c6f174b898fcf21e184d7b5f
6
+ metadata.gz: b3f8d265ffec1f614b736adc1b0462c9990f222132093f6c91ddabbb47e7df9333c41c854bcc377ca5471baecd6a3b194c4f07ad80cddda9bcf66df971e11d32
7
+ data.tar.gz: 3e81c7d2d60f172f71e281bb32923aaf267c7c67a32abef2dac659919341d4966505a2b5a1cc7a1789cd5d68b954cbd3839058864cbaf849e9a250e8461d25de
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ### 0.3.0 (2015-02-03)
4
+ - New: Language dictionaries for simplifying site templates.
5
+ - Change: `translate` command renamed to `id` to be clearer about what it actually does.
6
+
3
7
  ### 0.2.3 (2015-02-03)
4
8
  - Fixed an issue where permalinks would break if post/page language was not defined.
5
9
 
data/README.md CHANGED
@@ -6,6 +6,7 @@ Add multiple language features to your Jekyll site. This plugin makes it easy to
6
6
  - Link between translated posts and pages.
7
7
  - Use language in your permalinks.
8
8
  - Cross-post a single post to all languages.
9
+ - Add translation dictionaries to simplify site templates.
9
10
 
10
11
  [![Build Status](http://img.shields.io/travis/octopress/multilingual.svg)](https://travis-ci.org/octopress/multilingual)
11
12
  [![Gem Version](http://img.shields.io/gem/v/octopress-multilingual.svg)](https://rubygems.org/gems/octopress-multilingual)
@@ -113,19 +114,62 @@ If your default post index is at `/index.html` you should create additional inde
113
114
  How does it work? First this plugin groups all of your posts by language. Then at build time, any page with a language defined will
114
115
  have its posts filtered to display only matching languages. If your site uses [octopress-linkblog](https://github.com/octopress/linkblog) to publish link-posts, your `site.articles` and `site.linkposts` will be filtered as well.
115
116
 
117
+ ## Site template language dictionaries
118
+
119
+ It's annoying to have to write multiple site layouts and includes when the only differences are translated words. Octopress Multilingual
120
+ adds language dictionaries to make this much easier. In your site's `_data` directory you can add language dictionaries with the file
121
+ naming pattern `lang_[langauge_code].yml`. For example:
122
+
123
+ ```
124
+ _data
125
+ lang_en.yml
126
+ lang_de.yml
127
+ ```
128
+
129
+ Your files might look like this:
130
+
131
+ ```
132
+ # lang_en.yml
133
+ title: English title
134
+
135
+ # lang_de.yml
136
+ title: Deutsch titel
137
+ ```
138
+
139
+ Now in your layouts or includes you can reference these dictionaries under the global variable `lang`. The configured page or post
140
+ language will determine which language dictionary is used. For example:
141
+
142
+ ```
143
+ # On a page or post where lang: en
144
+ {{ lang.title }} => English title
145
+
146
+ # On a page or post where lang: de
147
+ {{ lang.title }} => Deutsch titel
148
+ ```
149
+
150
+ If no language is configured for a page or post, it will default to the site's default language.
151
+
152
+ ```
153
+ # No page lang, site is configured lang: en
154
+ {{ lang.title }} => English title
155
+ ```
156
+
157
+ Since these are Jekyll data sources, these dictionaries can also be accessed at `site.data.lang_en` and `site.data.lang_de`. This
158
+ plugin merely adds the global `lang` variable and swaps out context based on configured language.
159
+
116
160
  ## Link between translated posts or pages
117
161
 
118
162
  URLs can change and manually linking to translated posts or pages isn't the best idea. This plugin helps you link posts together using a shared translation ID. With [octopress](https://github.com/octopress/octopress), you'll be able to automatically add translation IDs to pages and posts. Then you can reference the array of translations with `post.tranlsations` or `page.translations`. Here's the syntax:
119
163
 
120
164
  ```
121
- $ octopress translate path [path path...]
165
+ $ octopress id path [path path...]
122
166
  ```
123
167
 
124
168
  This will create a unique key and automatically write it to the YAML front-matter each of the pages or posts you pass in. Here's an
125
169
  example:
126
170
 
127
171
  ```
128
- $ octopress translate _posts/2015-02-02-english-post.md _posts/2015-02-02-deutsch-post.md _posts/2015-02-02-espanol-post.md
172
+ $ octopress id _posts/2015-02-02-english-post.md _posts/2015-02-02-deutsch-post.md _posts/2015-02-02-espanol-post.md
129
173
  ```
130
174
 
131
175
  This will add `translation_id: fcdbc7e82b45346d67cced3523a2f236` to the YAML front-matter of each of these posts. There's nothing special about this key except that it is unique. If you want to write your own you can, it'll work just fine.
@@ -222,7 +266,6 @@ ordinal => /:lang/:categories/:year/:y_day/:title.html
222
266
 
223
267
  If you don't want language to appear in your URLs, you must configure your own permalinks without `:lang`.
224
268
 
225
-
226
269
  ## Temporary language scoping
227
270
 
228
271
  Using the `set_lang` liquid block, you can temporarily switch languages while rendering a portion of your site. For example:
@@ -30,6 +30,16 @@ module Octopress
30
30
  language_names[name] || name
31
31
  end
32
32
 
33
+ def lang_dict
34
+ @lang_dict ||= begin
35
+ data = {}
36
+ site.languages.each do |lang|
37
+ data[lang] = site.data["lang_#{lang}"]
38
+ end
39
+ data
40
+ end
41
+ end
42
+
33
43
  def language_names
34
44
  @language_names ||= begin
35
45
  config = SafeYAML.load_file(File.expand_path('../../language_key.yml', __FILE__))
@@ -118,10 +128,13 @@ module Octopress
118
128
  end
119
129
 
120
130
  def page_payload(lang)
121
- {
122
- 'posts' => posts_by_language[lang],
123
- 'linkposts' => linkposts_by_language[lang],
124
- 'articles' => articles_by_language[lang]
131
+ {
132
+ 'site' => {
133
+ 'posts' => posts_by_language[lang],
134
+ 'linkposts' => linkposts_by_language[lang],
135
+ 'articles' => articles_by_language[lang]
136
+ },
137
+ 'lang' => lang_dict[lang]
125
138
  }
126
139
  end
127
140
 
@@ -129,10 +142,13 @@ module Octopress
129
142
  # Skip when when showing documentation site
130
143
  if main_language
131
144
  @payload ||= {
132
- 'posts_by_language' => posts_by_language,
133
- 'linkposts_by_language' => linkposts_by_language,
134
- 'articles_by_language' => articles_by_language,
135
- 'languages' => languages
145
+ 'site' => {
146
+ 'posts_by_language' => posts_by_language,
147
+ 'linkposts_by_language' => linkposts_by_language,
148
+ 'articles_by_language' => articles_by_language,
149
+ 'languages' => languages
150
+ },
151
+ 'lang' => lang_dict[main_language]
136
152
  }
137
153
  else
138
154
  {}
@@ -9,21 +9,18 @@ if defined? Octopress::Command
9
9
  module Multilingual
10
10
  class Translate < Command
11
11
  def self.init_with_program(p)
12
- p.command(:translate) do |c|
13
- c.syntax 'translation <path> [path path...]>'
12
+ p.command(:id) do |c|
13
+ c.syntax 'id <path> [path path...]>'
14
14
  c.description "Generate a uniqe id to link translated posts or pages."
15
+
15
16
  c.action do |args|
16
- translate(args)
17
+ generate_id(args)
17
18
  end
18
19
  end
19
20
  end
20
21
 
21
22
  def self.generate_id(paths)
22
- Digest::MD5.hexdigest(paths.join)
23
- end
24
-
25
- def self.translate(paths)
26
- id = generate_id(paths)
23
+ id = Digest::MD5.hexdigest(paths.join)
27
24
  translated = []
28
25
  paths.each do |path|
29
26
  if File.file? path
@@ -16,15 +16,15 @@ module Octopress
16
16
  if item.translated
17
17
  # Access array of translated items via (post/page).translations
18
18
  item.data.merge!({
19
- 'translations' => item.translations,
20
- 'translated' => item.translated
21
- })
19
+ 'translations' => item.translations,
20
+ 'translated' => item.translated
21
+ })
22
22
  end
23
23
  end
24
24
  end
25
25
 
26
26
  def merge_payload(payload, site)
27
- { 'site' => Octopress::Multilingual.site_payload }
27
+ Octopress::Multilingual.site_payload
28
28
  end
29
29
  end
30
30
 
@@ -41,7 +41,7 @@ module Octopress
41
41
  #
42
42
  def merge_payload(payload, item)
43
43
  if item.lang
44
- { 'site' => Octopress::Multilingual.page_payload(item.lang) }
44
+ Octopress::Multilingual.page_payload(item.lang)
45
45
  end
46
46
  end
47
47
 
@@ -4,7 +4,7 @@ module Octopress
4
4
  class SetLang < Liquid::Block
5
5
  def initialize(tag_name, markup, tokens)
6
6
  super
7
- @lang = markup.strip
7
+ @input = markup.strip
8
8
  end
9
9
 
10
10
  def render(context)
@@ -33,19 +33,27 @@ module Octopress
33
33
  #
34
34
  def set_lang(lang)
35
35
  @context.environments.first['page']['lang'] = lang
36
- Multilingual.page_payload(lang).each do |key,value|
37
- @context.environments.first['site'][key] = value
36
+ payload = Multilingual.page_payload(lang)
37
+ set_payload('site', payload)
38
+ set_payload('lang', payload)
39
+ end
40
+
41
+ def set_payload(payload_key, payload)
42
+ payload[payload_key].each do |key,value|
43
+ @context.environments.first[payload_key][key] = value
38
44
  end
39
45
  end
40
46
 
41
47
  def store_state
42
48
  @current_lang = @context['page.lang']
43
49
  @site = @context['site'].clone
50
+ @lang = @context['lang'].clone
44
51
  end
45
52
 
46
53
  def restore_state
47
54
  @context.environments.first['page']['lang'] = @current_lang
48
55
  @context.environments.first['site'] = @site
56
+ @context.environments.first['lang'] = @lang
49
57
  end
50
58
 
51
59
  def lang
@@ -53,7 +61,7 @@ module Octopress
53
61
  # Read tag arguments as a string first, if that fails,
54
62
  # Look at the local context, to see if it is a variable
55
63
  #
56
- if lang = [@lang, @context[@lang]].select do |l|
64
+ if lang = [@input, @context[@input]].select do |l|
57
65
  @languages.include?(l)
58
66
  end.first
59
67
 
@@ -1,5 +1,5 @@
1
1
  module Octopress
2
2
  module Multilingual
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octopress-multilingual
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mathis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octopress-hooks