octopress-multilingual 0.1.4 → 0.2.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: b3ae15ba4e42285ca409a3e01dad012bbb35df62
4
- data.tar.gz: 6bbbd3561ad7f931c11ab9d7020af47e33ac4329
3
+ metadata.gz: adbfc715861572322e769a378f91f056c0978b21
4
+ data.tar.gz: 9f3d3350c30e02fc88dbf662ae7681bcd7ee3df0
5
5
  SHA512:
6
- metadata.gz: ee9d5706021be85b0cb543dec60807388759ffb7394b74884d73c8e4065f139faff613f0f1d4cca24cc824b2980ecaf6e5b1b7f8856da440abf924f50b8bef40
7
- data.tar.gz: 21863f42abef3df62dfd7992f9cefb46734cc294e425b92c98004a80a5e076280748a45bae2f93ec354957d5ac3348b05e0cebf75c5f47da086ce005f4af3858
6
+ metadata.gz: 5787dc6d327e186c098e6f1751ca45abd9f87c09d697a92e32abfec19b2a93c612d7b865e7c3a791510bf7c165e88dcf5aed0e95fbcc8696367acfb20a81a406
7
+ data.tar.gz: 74d5b41f3d5197f47d7efc9318d009fdb36d09ddafc7c57a705992582134f63fa01f8d7ad8edbce78354006819aef6701be8dc5bde22e47d42a181d9f1f9e378
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ### 0.2.0 (2015-02-01)
4
+ - Link between posts or pages with a matching `translation_id`.
5
+ - Add translation IDs to posts or pages with `octopress translate <path>`.
6
+ - Check if a post or page has been translated with `[post or page].tranlsated`.
7
+ - Access translated posts or pages with `[post or page].tranlsations`.
8
+ - New tags `{% translations [post or page] %}` and `{% translation_list [post or page] %}` for generating links to translated posts.
9
+ - Convert language codes to language names with filter `{{ de | language_name }}`.
10
+
3
11
  ### 0.1.4 (2015-02-01)
4
12
  - Cross-post languages with `lang_crosspost: true` in a post's YAML front-matter.
5
13
 
data/README.md CHANGED
@@ -3,8 +3,9 @@
3
3
  Add multiple language features to your Jekyll site. This plugin makes it easy to:
4
4
 
5
5
  - Add language-specific post indexes, archives, and RSS feeds.
6
- - Set language based permalinks.
7
- - Cross-post between languages.
6
+ - Link between translated posts and pages.
7
+ - Use language in your permalinks.
8
+ - Cross-post a single post to all languages.
8
9
 
9
10
  [![Build Status](http://img.shields.io/travis/octopress/multilingual.svg)](https://travis-ci.org/octopress/multilingual)
10
11
  [![Gem Version](http://img.shields.io/gem/v/octopress-multilingual.svg)](https://rubygems.org/gems/octopress-multilingual)
@@ -31,7 +32,6 @@ Then add the gem to your Jekyll configuration.
31
32
  gems:
32
33
  - octopress-multilingual
33
34
 
34
-
35
35
  ## An important note
36
36
 
37
37
  **There is no Jekyll standard for multilingual sites** and many plugins will not work properly with this setup. Octopress and it's
@@ -69,15 +69,21 @@ title: "Ein nachdenklicher Beitrag"
69
69
  lang: de
70
70
  ```
71
71
 
72
- With Octopress, you can do this automatically from the command line when creating posts, drafts, or pages.
72
+ You can, refer to a page or post language by `page.lang` or `post.lang`. This plugin adds the filter `language_name` which can convert the language short-name into the native language name. For example:
73
73
 
74
74
  ```
75
- $ octopress new post "Some title" --lang en
76
- $ octopress new draft "Some title" --lang en
77
- $ octopress new page de/index.html --lang de
75
+ {{ en | language_name }} # English
76
+ {{ de | language_name }} # Deutsch
77
+ {{ es | language_name }} # Español
78
78
  ```
79
79
 
80
- This command will set the language (in the YAML front-matter) and posts will be created in `_posts/[lang]/[post-file]`.
80
+ Many common language codes are supported, but you can add to or override these in Jekyll's site configuration:
81
+
82
+ ```
83
+ language_names:
84
+ es: Spanish
85
+ omg: WTFBBQ
86
+ ```
81
87
 
82
88
  ## Indexes, RSS feeds and Archives.
83
89
 
@@ -102,12 +108,59 @@ lang: en
102
108
  {% for post in site.posts %} ... {% endfor %}
103
109
  ```
104
110
 
105
- If your default post index is at `/index.html` you should create additional indexes for each secondary language. If your secondary language is German, create a posts index at `/de/index.html`.
111
+ If your default post index is at `/index.html` you should create additional indexes for each secondary language. If you also write in German, you can create a posts index at `/de/index.html`. This approach will work for post archives and RSS feeds, though if you are using [octopress-feeds](https://github.com/octopress/feeds), RSS feeds for each language will be generated automatically.
106
112
 
107
113
  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
108
114
  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.
109
115
 
110
- This same approach will let you create language-specific RSS feeds and post archives. If you are using [octopress-feeds](https://github.com/octopress/feeds), the default RSS feeds will automatically use your default `site.lang` defined language and you can easily create additional RSS feeds for additional languages.
116
+ ## Link between translated posts or pages
117
+
118
+ URLs can change and manually linking to translated posts or pages isn't the best idea. This plugin helps you link posts together using
119
+ 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:
120
+
121
+ ```
122
+ $ octopress translate path [path path...]
123
+ ```
124
+
125
+ 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
126
+ example:
127
+
128
+ ```
129
+ $ octopress translate _posts/2015-02-02-english-post.md _posts/2015-02-02-deutsch-post.md _posts/2015-02-02-espanol-post.md
130
+ ```
131
+
132
+ This will add `translation_id: fcdbc7e82b45346d67cced3523a2f236` to the YAML front-matter of each of these posts.
133
+ Now you can use the `translations` or `translation_list` tags to list links to translated posts or pages. For example:
134
+
135
+ ```
136
+ {% translations post %}
137
+
138
+ # Which outputs:
139
+ <a class='translation-link lang-de' href='/de/2015/02/02/deutsch-post'>Deutsch</a>, <a class='translation-link lang-es' href='/es/2015/02/02/espanol-post'>Español</a>
140
+
141
+ # If you prefer a list:
142
+ {% translation_list post %}
143
+
144
+ # Which ouputs:
145
+ <ul class='translation-list'>
146
+ <li class='translation-item lang-de'>
147
+ <a class='translation-link lang-de' href='/de/2015/02/02/deutsch-post'>Deutsch</a>
148
+ </li>
149
+ <li class='translation-item lang-es'>
150
+ <a class='translation-link lang-es' href='/es/2015/02/02/espanol-post'>Español</a>
151
+ </li>
152
+ </ul>
153
+ ```
154
+
155
+ If you'd rather access translated posts manually, you can loop through the translations like this:
156
+
157
+ ```
158
+ {% if page.translated %}
159
+ Translations: {% for t in page.translations %}
160
+ <a href="{{ t.url }}">{{ t.lang | language_name }}</a>
161
+ {% endfor %}
162
+ {% endif %}
163
+ ```
111
164
 
112
165
  ## Reference posts by language
113
166
 
data/language_key.yml ADDED
@@ -0,0 +1,44 @@
1
+ # Common language names derived from http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
2
+ #
3
+ # You can add to, or override this list in your Jekyll configuration like this:
4
+ #
5
+ # language_names:
6
+ # es: Spanish
7
+ # omg: WTF-BBQ
8
+ #
9
+ #
10
+ en: English
11
+ de: Deutsch
12
+ it: italiano
13
+ jp: 日本語
14
+ cs: čeština
15
+ da: dansk
16
+ nl: Nederlands
17
+ fr: français
18
+ el: ελληνικά
19
+ ga: Gaeilge
20
+ 'no': Norsk
21
+ nb: Norsk bokmål
22
+ nn: Norsk nynorsk
23
+ pl: Polszczyzna
24
+ pt: Português
25
+ ro: limba română
26
+ ru: Русский
27
+ gd: Gàidhlig
28
+ sr: српски језик
29
+ sk: Slovenčina
30
+ sl: Slovenščina
31
+ es: Español
32
+ sv: Svenska
33
+ uk: українська мова
34
+ cy: Cymraeg
35
+ ko: 한국어
36
+ kg: Kikongo
37
+ ng: Owambo
38
+ sw: Kiswahili
39
+ zu: isiZulu
40
+ bo: བོད་ཡིག
41
+ tr: Türkçe
42
+ et: eesti
43
+ fi: suomi
44
+ hu: magyar
@@ -1,9 +1,13 @@
1
1
  require 'octopress-hooks'
2
+ require 'liquid'
2
3
 
3
4
  require "octopress-multilingual/version"
4
- require "octopress-multilingual/set_lang-tag"
5
+ require "octopress-multilingual/set_lang_tag"
6
+ require "octopress-multilingual/translation_tag"
7
+ require "octopress-multilingual/filters"
5
8
  require "octopress-multilingual/hooks"
6
9
  require "octopress-multilingual/jekyll"
10
+ require "octopress-multilingual/command"
7
11
 
8
12
  module Octopress
9
13
  module Multilingual
@@ -24,6 +28,37 @@ module Octopress
24
28
  @site ||= Octopress.site
25
29
  end
26
30
 
31
+ def language_names(name=nil)
32
+ @language_names ||= begin
33
+ config = SafeYAML.load_file(File.expand_path('../../language_key.yml', __FILE__))
34
+ if lang_config = site.config['language_names']
35
+ config.merge!(lang_config)
36
+ end
37
+
38
+ config
39
+ end
40
+
41
+ if name
42
+ @language_names[name]
43
+ else
44
+ @language_names
45
+ end
46
+ end
47
+
48
+ def translated_posts
49
+ @translated_posts ||= begin
50
+ filter = lambda {|p| p.data['translation_id']}
51
+ site.posts.reverse.select(&filter).group_by(&filter)
52
+ end
53
+ end
54
+
55
+ def translated_pages
56
+ @translated_pages ||= begin
57
+ filter = lambda {|p| p.data['translation_id']}
58
+ site.pages.select(&filter).group_by(&filter)
59
+ end
60
+ end
61
+
27
62
  def languages
28
63
  posts_by_language.keys
29
64
  end
@@ -0,0 +1,50 @@
1
+ begin
2
+ require 'octopress'
3
+ require 'digest/md5'
4
+ rescue LoadError
5
+ end
6
+
7
+ if defined? Octopress::Command
8
+ module Octopress
9
+ module Multilingual
10
+ class Translate < Command
11
+ def self.init_with_program(p)
12
+ p.command(:translate) do |c|
13
+ c.syntax 'translation <path> [path path...]>'
14
+ c.description "Generate a uniqe id to link translated posts or pages."
15
+ c.action do |args|
16
+ translate(args)
17
+ end
18
+ end
19
+ end
20
+
21
+ def self.generate_id(paths)
22
+ Digest::MD5.hexdigest(paths.join)
23
+ end
24
+
25
+ def self.translate(paths)
26
+ id = generate_id(paths)
27
+ translated = []
28
+ paths.each do |path|
29
+ if File.file? path
30
+ contents = File.read(path)
31
+ contents.sub!(/\A(---\s+.+?\s+)---/m) do
32
+ fm = $1.sub(/translation_id:.+\n?/,'')
33
+ fm << "translation_id: #{id}\n"
34
+ fm << "---"
35
+ end
36
+
37
+ File.open(path, 'w+') {|f| f.write(contents) }
38
+
39
+ translated << path
40
+ end
41
+ end
42
+
43
+ puts "translation_id: #{id}"
44
+ puts "Added to:"
45
+ puts translated.map {|p| " - #{p}" }.join("\n")
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module Octopress
2
+ module Multilingual
3
+ module Filters
4
+ def language_name(input)
5
+ if input
6
+ Octopress::Multilingual.language_names(input.strip.downcase) || input
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ Liquid::Template.register_filter(Octopress::Multilingual::Filters)
@@ -8,6 +8,21 @@ module Octopress
8
8
  site.config['languages'] = site.languages
9
9
  end
10
10
 
11
+ def pre_render(site)
12
+
13
+ # Add translation page data to each page or post.
14
+ #
15
+ [site.pages, site.posts].flatten.each do |item|
16
+ if item.translated
17
+ # Access array of translated items via (post/page).translations
18
+ item.data.merge!({
19
+ 'translations' => item.translations,
20
+ 'translated' => item.translated
21
+ })
22
+ end
23
+ end
24
+ end
25
+
11
26
  def merge_payload(payload, site)
12
27
  { 'site' => Octopress::Multilingual.site_payload }
13
28
  end
@@ -29,6 +44,7 @@ module Octopress
29
44
  { 'site' => Octopress::Multilingual.page_payload(item.lang) }
30
45
  end
31
46
  end
47
+
32
48
  end
33
49
  end
34
50
  end
@@ -48,6 +48,16 @@ module Jekyll
48
48
  end
49
49
  end
50
50
 
51
+ def translated
52
+ data['translation_id'] && !translations.empty?
53
+ end
54
+
55
+ def translations
56
+ if data['translation_id']
57
+ @translations ||= Octopress::Multilingual.translated_pages[data['translation_id']].reject {|p| p == self }
58
+ end
59
+ end
60
+
51
61
  def permalink
52
62
  if permalink = permalink_orig
53
63
  data['permalink'].sub!(/:lang/, lang)
@@ -70,6 +80,16 @@ module Jekyll
70
80
  template
71
81
  end
72
82
 
83
+ def translated
84
+ data['translation_id'] && !translations.empty?
85
+ end
86
+
87
+ def translations
88
+ if data['translation_id']
89
+ @translations ||= Octopress::Multilingual.translated_posts[data['translation_id']].reject {|p| p == self}
90
+ end
91
+ end
92
+
73
93
  def lang
74
94
  if data['lang']
75
95
  data['lang'].downcase
@@ -0,0 +1,68 @@
1
+ module Octopress
2
+ module Multilingual
3
+ module Tags
4
+ class SetLang < Liquid::Block
5
+ def initialize(tag_name, markup, tokens)
6
+ super
7
+ @lang = markup.strip
8
+ end
9
+
10
+ def render(context)
11
+ @context = context
12
+ @languages = @context['site.languages']
13
+
14
+ # If a language is defined
15
+ if lang && lang != @context['page.lang']
16
+
17
+ store_state # Store current language and post arrays
18
+ set_lang lang # Set to specified language
19
+ content = super(context) # Render
20
+ restore_state # Restore language and post arrays
21
+
22
+ content
23
+ else
24
+ # If the language argument resovles to nil
25
+ # this will render contents normally
26
+ #
27
+ super(context)
28
+ end
29
+ end
30
+
31
+ # Swap out site.posts, site.linkposts, and site.articles with
32
+ # arrays filtered by the selected language
33
+ #
34
+ def set_lang(lang)
35
+ @context.environments.first['page']['lang'] = lang
36
+ Multilingual.page_payload(lang).each do |key,value|
37
+ @context.environments.first['site'][key] = value
38
+ end
39
+ end
40
+
41
+ def store_state
42
+ @current_lang = @context['page.lang']
43
+ @site = @context['site'].clone
44
+ end
45
+
46
+ def restore_state
47
+ @context.environments.first['page']['lang'] = @current_lang
48
+ @context.environments.first['site'] = @site
49
+ end
50
+
51
+ def lang
52
+
53
+ # Read tag arguments as a string first, if that fails,
54
+ # Look at the local context, to see if it is a variable
55
+ #
56
+ if lang = [@lang, @context[@lang]].select do |l|
57
+ @languages.include?(l)
58
+ end.first
59
+
60
+ lang.downcase
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ Liquid::Template.register_tag('set_lang', Octopress::Multilingual::Tags::SetLang)
@@ -0,0 +1,43 @@
1
+ module Octopress
2
+ module Multilingual
3
+ module Tags
4
+ class Translations < Liquid::Tag
5
+ def initialize(tag, input, tokens)
6
+ super
7
+ @tag = tag.strip
8
+ @input = input.strip
9
+ end
10
+
11
+ def render(context)
12
+ @context = context
13
+ if translations
14
+ if @tag == 'translation_list'
15
+ list = translations.dup.map do |t|
16
+ "<li translation-item lang-#{t.lang}'>#{anchor(t)}</li>"
17
+ end.join(' ,')
18
+ "<ul class='translation-list'>#{list}</uL>"
19
+ else
20
+ translations.dup.map do |t|
21
+ anchor(t)
22
+ end.join(', ')
23
+ end
24
+ end
25
+ end
26
+
27
+ def anchor(item)
28
+ language = Octopress::Multilingual.language_names(item.lang)
29
+ "<a class='translation-link lang-#{item.lang}' href='#{ item.url }'>#{ language }</a>"
30
+ end
31
+
32
+ def translations
33
+ if item = @context[@input]
34
+ item['translations'] if item['translated']
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ Liquid::Template.register_tag('translations', Octopress::Multilingual::Tags::Translations)
43
+ Liquid::Template.register_tag('translation_list', Octopress::Multilingual::Tags::Translations)
@@ -1,5 +1,5 @@
1
1
  module Octopress
2
2
  module Multilingual
3
- VERSION = "0.1.4"
3
+ VERSION = "0.2.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.1.4
4
+ version: 0.2.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-01 00:00:00.000000000 Z
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octopress-hooks
@@ -118,10 +118,14 @@ files:
118
118
  - CHANGELOG.md
119
119
  - LICENSE.txt
120
120
  - README.md
121
+ - language_key.yml
121
122
  - lib/octopress-multilingual.rb
123
+ - lib/octopress-multilingual/command.rb
124
+ - lib/octopress-multilingual/filters.rb
122
125
  - lib/octopress-multilingual/hooks.rb
123
126
  - lib/octopress-multilingual/jekyll.rb
124
- - lib/octopress-multilingual/set_lang-tag.rb
127
+ - lib/octopress-multilingual/set_lang_tag.rb
128
+ - lib/octopress-multilingual/translation_tag.rb
125
129
  - lib/octopress-multilingual/version.rb
126
130
  homepage: https://github.com/octopress/multilingual
127
131
  licenses:
@@ -1,69 +0,0 @@
1
- require 'liquid'
2
-
3
- module Octopress
4
- module Multilingual
5
- class PostsByTag < Liquid::Block
6
- def initialize(tag_name, markup, tokens)
7
- super
8
- @lang = markup.strip
9
- end
10
-
11
- def render(context)
12
- @context = context
13
- @languages = @context['site.languages']
14
-
15
- # If a language is defined
16
- if lang && lang != @context['page.lang']
17
-
18
- store_state # Store current language and post arrays
19
- set_lang lang # Set to specified language
20
- content = super(context) # Render
21
- restore_state # Restore language and post arrays
22
-
23
- content
24
- else
25
- # If the language argument resovles to nil
26
- # this will render contents normally
27
- #
28
- super(context)
29
- end
30
- end
31
-
32
- # Swap out site.posts, site.linkposts, and site.articles with
33
- # arrays filtered by the selected language
34
- #
35
- def set_lang(lang)
36
- @context.environments.first['page']['lang'] = lang
37
- Multilingual.page_payload(lang).each do |key,value|
38
- @context.environments.first['site'][key] = value
39
- end
40
- end
41
-
42
- def store_state
43
- @current_lang = @context['page.lang']
44
- @site = @context['site'].clone
45
- end
46
-
47
- def restore_state
48
- @context.environments.first['page']['lang'] = @current_lang
49
- @context.environments.first['site'] = @site
50
- end
51
-
52
- def lang
53
-
54
- # Read tag arguments as a string first, if that fails,
55
- # Look at the local context, to see if it is a variable
56
- #
57
- if lang = [@lang, @context[@lang]].select do |l|
58
- @languages.include?(l)
59
- end.first
60
-
61
- lang.downcase
62
- end
63
- end
64
- end
65
- end
66
- end
67
-
68
- Liquid::Template.register_tag('set_lang', Octopress::Multilingual::PostsByTag)
69
-