jekyll-msgcat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ebb561375a6b447a3d039e66ab65c7c2fa4057d2
4
+ data.tar.gz: b38b45b7f69f34c9473a3fa30e97ab881b5e9f94
5
+ SHA512:
6
+ metadata.gz: 52df745b24b9d2c9b27c7a7a2f065776c176d6a33e813effd8e7f8bbd9204d7b3cc8dfea45c851144a94fe502625c257a80e4f44753daf3ee4b28c8a9b24e97d
7
+ data.tar.gz: 3b8aa3c44dbb144285b96fd0b7165573c6238a1e05cc401416091f2cd870cc214466029a9bd30f79347173e2d306e824b27b2e2c2c6c1652a1f681d98df4f033
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ph
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ #ruby=ruby-2.0.0
2
+ #ruby-gemset=msgcat
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in jekyll-msgcat.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexander Gromnitsky
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # jekyll-msgcat
2
+
3
+ Multi-lingual interface with Jekyll via .yaml message catalogs.
4
+
5
+ Tested with Jekyll 1.2.1.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'jekyll-msgcat'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jekyll-msgcat
20
+
21
+ ## Usage
22
+
23
+ Create ``_plugins/req.rb`` with 1 line:
24
+
25
+ require 'jekyll/msgcat'
26
+
27
+ Add to ``_config.yml``:
28
+
29
+ msgcat:
30
+ locale: ru
31
+
32
+ Default locale is always 'en'. 'en' locale is implicit, you cannot
33
+ select it.
34
+
35
+ Create ``_msgcat.yaml``:
36
+
37
+ uk:
38
+ 'Home': На головну сторiнку
39
+
40
+ And use in Liquid templates:
41
+
42
+ {{ 'Home' | mc }}
43
+
44
+ If 'Home' key wasn't found anywhere in the message catalog, or you
45
+ didn't select any locale, a string 'Home' will be used.
46
+
47
+ More info [here](http://gromnitsky.blogspot.com/2013/10/multi-lingual-interface-with-jekyll.html).
48
+
49
+ ## License
50
+
51
+ MIT.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # -*-ruby-*-
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ task :default => [:test]
6
+
7
+ Rake::TestTask.new do |t|
8
+ # we need to chdir to test directory before running
9
+ t.ruby_opts = ['-C', 'test']
10
+ t.test_files = FileList['test/test_*.rb'].map {|idx| idx.sub(/^test\//, '') }
11
+ # t.verbose = true
12
+ end
@@ -0,0 +1 @@
1
+ _site
@@ -0,0 +1,44 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6
+ <title>{{ page.title }}</title>
7
+ <meta name="viewport" content="width=device-width">
8
+
9
+ <!-- syntax highlighting CSS -->
10
+ <link rel="stylesheet" href="/css/syntax.css">
11
+
12
+ <!-- Custom CSS -->
13
+ <link rel="stylesheet" href="/css/main.css">
14
+
15
+ </head>
16
+ <body>
17
+
18
+ <div class="site">
19
+ <div class="header">
20
+ <h1 class="title"><a href="/">{{ site.name }}</a></h1>
21
+ <a class="extra" href="/">{{ 'home' | mc }}</a>
22
+ </div>
23
+
24
+ {{ content }}
25
+
26
+ <div class="footer">
27
+ <div class="contact">
28
+ <p>
29
+ Your Name<br />
30
+ What You Are<br />
31
+ you@example.com
32
+ </p>
33
+ </div>
34
+ <div class="contact">
35
+ <p>
36
+ <a href="https://github.com/yourusername">github.com/yourusername</a><br />
37
+ <a href="https://twitter.com/yourusername">twitter.com/yourusername</a><br />
38
+ </p>
39
+ </div>
40
+ </div>
41
+ </div>
42
+
43
+ </body>
44
+ </html>
@@ -0,0 +1,9 @@
1
+ ---
2
+ layout: default
3
+ ---
4
+ <h2>{{ page.title }}</h2>
5
+ <p class="meta">{{ page.date | date_to_string }}</p>
6
+
7
+ <div class="post">
8
+ {{ content }}
9
+ </div>
@@ -0,0 +1,89 @@
1
+ require 'andand'
2
+ require 'safe_yaml'
3
+
4
+ require_relative "msgcat/version"
5
+
6
+ module Jekyll
7
+ module Msgcat
8
+ MSGCAT_DB = '_msgcat.yaml'
9
+ @@__msgcat = nil
10
+
11
+ # Return a hash of parsed messge catalog or nil.
12
+ def __get_msgcat
13
+ return @@__msgcat if @@__msgcat
14
+
15
+ begin
16
+ @@__msgcat = YAML.load_file MSGCAT_DB, :safe => false
17
+ rescue
18
+ $stderr.puts "msgcat warning: #{MSGCAT_DB} not found"
19
+ return nil
20
+ end
21
+
22
+ @@__msgcat
23
+ end
24
+
25
+ def __get_site
26
+ @context.registers[:site].config
27
+ end
28
+
29
+ # Return a string ('lt', for example) or nil.
30
+ def __get_locale
31
+ __get_site.andand['msgcat'].andand['locale']
32
+ end
33
+
34
+ # Extract localized version of 'input' from message catalog or
35
+ # return the original if no localization was found.
36
+ def mc input
37
+ locale = __get_locale || (return input)
38
+ msgcat = __get_msgcat || (return input)
39
+
40
+ if !msgcat[locale]
41
+ $stderr.puts "msgcat warning: '#{locale}' wasn't found in #{MSGCAT_DB}"
42
+ return input
43
+ end
44
+
45
+ (msg = msgcat[locale][input]) ? msg : input
46
+ end
47
+
48
+ def cur_page_in_another_locale targetLocale, cssClass = 'btn btn-primary btn-xs'
49
+ raise 'target locale requred' if targetLocale =~ /^\s*$/
50
+
51
+ site = __get_site
52
+ pattern = "<a href='%s' class='#{cssClass} %s'>#{targetLocale}</a>"
53
+ locale = __get_locale || 'en'
54
+
55
+ # current site
56
+ return pattern % ['#', 'disabled'] if locale == targetLocale
57
+
58
+ deploy = site.andand['msgcat'].andand['deploy'] || 'domain'
59
+
60
+ if deploy == 'domain'
61
+ begin
62
+ uri = URI site['url']
63
+ rescue
64
+ raise "invalid 'url' property in _config.yml: #{$!}"
65
+ end
66
+ host = uri.host.split '.'
67
+ host[0] = targetLocale
68
+ uri.host = host.join '.'
69
+ else
70
+ raise "no 'baseurl' property in _config.yml" unless site['baseurl']
71
+ uri = site['baseurl'].split '/'
72
+ if uri.size == 0
73
+ uri = ['', targetLocale]
74
+ else
75
+ uri[uri.size-1] = targetLocale
76
+ end
77
+ uri = uri.join '/'
78
+ end
79
+
80
+ return pattern % [uri + @context.registers[:page]['url'], ""]
81
+ end
82
+
83
+ end
84
+ end
85
+
86
+ # Register a new Liquid filter
87
+ if defined? Liquid::Template
88
+ Liquid::Template.register_filter Jekyll::Msgcat
89
+ end
@@ -0,0 +1,160 @@
1
+ /*****************************************************************************/
2
+ /*
3
+ /* Common
4
+ /*
5
+ /*****************************************************************************/
6
+
7
+ /* Global Reset */
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ }
12
+
13
+ html, body { height: 100%; }
14
+
15
+ body {
16
+ background-color: #FFF;
17
+ font: 13.34px Helvetica, Arial, sans-serif;
18
+ font-size: small;
19
+ text-align: center;
20
+ }
21
+
22
+ h1, h2, h3, h4, h5, h6 {
23
+ font-size: 100%; }
24
+
25
+ h1 { margin-bottom: 1em; }
26
+ p { margin: 1em 0; }
27
+
28
+ a { color: #00a; }
29
+ a:hover { color: #000; }
30
+ a:visited { color: #a0a; }
31
+
32
+ /*****************************************************************************/
33
+ /*
34
+ /* Home
35
+ /*
36
+ /*****************************************************************************/
37
+ ul.posts {
38
+ list-style-type: none;
39
+ margin-bottom: 2em;
40
+ }
41
+
42
+ ul.posts li {
43
+ line-height: 1.75em;
44
+ }
45
+
46
+ ul.posts span {
47
+ color: #aaa;
48
+ font-family: Monaco, "Courier New", monospace;
49
+ font-size: 80%;
50
+ }
51
+
52
+ /*****************************************************************************/
53
+ /*
54
+ /* Site
55
+ /*
56
+ /*****************************************************************************/
57
+
58
+ .site {
59
+ font-size: 115%;
60
+ text-align: justify;
61
+ width: 42em;
62
+ margin: 3em auto 2em;
63
+ line-height: 1.5em;
64
+ }
65
+
66
+ .site .header a {
67
+ font-weight: bold;
68
+ text-decoration: none;
69
+ }
70
+
71
+ .site .header h1.title {
72
+ display: inline-block;
73
+ margin-bottom: 2em;
74
+ }
75
+
76
+ .site .header h1.title a {
77
+ color: #a00;
78
+ }
79
+
80
+ .site .header h1.title a:hover {
81
+ color: #000;
82
+ }
83
+
84
+ .site .header a.extra {
85
+ color: #aaa;
86
+ margin-left: 1em;
87
+ }
88
+
89
+ .site .header a.extra:hover {
90
+ color: #000;
91
+ }
92
+
93
+ .site .meta {
94
+ color: #aaa;
95
+ }
96
+
97
+ .site .footer {
98
+ font-size: 80%;
99
+ color: #666;
100
+ border-top: 4px solid #eee;
101
+ margin-top: 2em;
102
+ overflow: hidden;
103
+ }
104
+
105
+ .site .footer .contact {
106
+ float: left;
107
+ margin-right: 3em;
108
+ }
109
+
110
+ .site .footer .contact a {
111
+ color: #8085C1;
112
+ }
113
+
114
+ .site .footer .rss {
115
+ margin-top: 1.1em;
116
+ margin-right: -.2em;
117
+ float: right;
118
+ }
119
+
120
+ .site .footer .rss img {
121
+ border: 0;
122
+ }
123
+
124
+ /*****************************************************************************/
125
+ /*
126
+ /* Posts
127
+ /*
128
+ /*****************************************************************************/
129
+
130
+ /* standard */
131
+ .post pre {
132
+ border: 1px solid #ddd;
133
+ background-color: #eef;
134
+ padding: 0 .4em;
135
+ }
136
+
137
+ .post ul, .post ol {
138
+ margin-left: 1.35em;
139
+ }
140
+
141
+ .post code {
142
+ border: 1px solid #ddd;
143
+ background-color: #eef;
144
+ padding: 0 .2em;
145
+ }
146
+
147
+ .post pre code {
148
+ border: none;
149
+ }
150
+
151
+ /* terminal */
152
+ .post pre.terminal {
153
+ border: 1px solid #000;
154
+ background-color: #333;
155
+ color: #FFF;
156
+ }
157
+
158
+ .post pre.terminal code {
159
+ background-color: #333;
160
+ }
@@ -0,0 +1,60 @@
1
+ .highlight { background: #ffffff; }
2
+ .highlight .c { color: #999988; font-style: italic } /* Comment */
3
+ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
4
+ .highlight .k { font-weight: bold } /* Keyword */
5
+ .highlight .o { font-weight: bold } /* Operator */
6
+ .highlight .cm { color: #999988; font-style: italic } /* Comment.Multiline */
7
+ .highlight .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
8
+ .highlight .c1 { color: #999988; font-style: italic } /* Comment.Single */
9
+ .highlight .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
10
+ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
11
+ .highlight .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */
12
+ .highlight .ge { font-style: italic } /* Generic.Emph */
13
+ .highlight .gr { color: #aa0000 } /* Generic.Error */
14
+ .highlight .gh { color: #999999 } /* Generic.Heading */
15
+ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
16
+ .highlight .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */
17
+ .highlight .go { color: #888888 } /* Generic.Output */
18
+ .highlight .gp { color: #555555 } /* Generic.Prompt */
19
+ .highlight .gs { font-weight: bold } /* Generic.Strong */
20
+ .highlight .gu { color: #aaaaaa } /* Generic.Subheading */
21
+ .highlight .gt { color: #aa0000 } /* Generic.Traceback */
22
+ .highlight .kc { font-weight: bold } /* Keyword.Constant */
23
+ .highlight .kd { font-weight: bold } /* Keyword.Declaration */
24
+ .highlight .kp { font-weight: bold } /* Keyword.Pseudo */
25
+ .highlight .kr { font-weight: bold } /* Keyword.Reserved */
26
+ .highlight .kt { color: #445588; font-weight: bold } /* Keyword.Type */
27
+ .highlight .m { color: #009999 } /* Literal.Number */
28
+ .highlight .s { color: #d14 } /* Literal.String */
29
+ .highlight .na { color: #008080 } /* Name.Attribute */
30
+ .highlight .nb { color: #0086B3 } /* Name.Builtin */
31
+ .highlight .nc { color: #445588; font-weight: bold } /* Name.Class */
32
+ .highlight .no { color: #008080 } /* Name.Constant */
33
+ .highlight .ni { color: #800080 } /* Name.Entity */
34
+ .highlight .ne { color: #990000; font-weight: bold } /* Name.Exception */
35
+ .highlight .nf { color: #990000; font-weight: bold } /* Name.Function */
36
+ .highlight .nn { color: #555555 } /* Name.Namespace */
37
+ .highlight .nt { color: #000080 } /* Name.Tag */
38
+ .highlight .nv { color: #008080 } /* Name.Variable */
39
+ .highlight .ow { font-weight: bold } /* Operator.Word */
40
+ .highlight .w { color: #bbbbbb } /* Text.Whitespace */
41
+ .highlight .mf { color: #009999 } /* Literal.Number.Float */
42
+ .highlight .mh { color: #009999 } /* Literal.Number.Hex */
43
+ .highlight .mi { color: #009999 } /* Literal.Number.Integer */
44
+ .highlight .mo { color: #009999 } /* Literal.Number.Oct */
45
+ .highlight .sb { color: #d14 } /* Literal.String.Backtick */
46
+ .highlight .sc { color: #d14 } /* Literal.String.Char */
47
+ .highlight .sd { color: #d14 } /* Literal.String.Doc */
48
+ .highlight .s2 { color: #d14 } /* Literal.String.Double */
49
+ .highlight .se { color: #d14 } /* Literal.String.Escape */
50
+ .highlight .sh { color: #d14 } /* Literal.String.Heredoc */
51
+ .highlight .si { color: #d14 } /* Literal.String.Interpol */
52
+ .highlight .sx { color: #d14 } /* Literal.String.Other */
53
+ .highlight .sr { color: #009926 } /* Literal.String.Regex */
54
+ .highlight .s1 { color: #d14 } /* Literal.String.Single */
55
+ .highlight .ss { color: #990073 } /* Literal.String.Symbol */
56
+ .highlight .bp { color: #999999 } /* Name.Builtin.Pseudo */
57
+ .highlight .vc { color: #008080 } /* Name.Variable.Class */
58
+ .highlight .vg { color: #008080 } /* Name.Variable.Global */
59
+ .highlight .vi { color: #008080 } /* Name.Variable.Instance */
60
+ .highlight .il { color: #009999 } /* Literal.Number.Integer.Long */
@@ -0,0 +1,3 @@
1
+ name: Your New Jekyll Site
2
+ markdown: redcarpet
3
+ pygments: true
@@ -0,0 +1,24 @@
1
+ ---
2
+ layout: post
3
+ title: "Welcome to Jekyll!"
4
+ date: 2013-10-24 11:34:36
5
+ categories: jekyll update
6
+ ---
7
+
8
+ You'll find this post in your `_posts` directory - edit this post and re-build (or run with the `-w` switch) to see your changes!
9
+ To add new posts, simply add a file in the `_posts` directory that follows the convention: YYYY-MM-DD-name-of-post.ext.
10
+
11
+ Jekyll also offers powerful support for code snippets:
12
+
13
+ {% highlight ruby %}
14
+ def print_hi(name)
15
+ puts "Hi, #{name}"
16
+ end
17
+ print_hi('Tom')
18
+ #=> prints 'Hi, Tom' to STDOUT.
19
+ {% endhighlight %}
20
+
21
+ Check out the [Jekyll docs][jekyll] for more info on how to get the most out of Jekyll. File all bugs/feature requests at [Jekyll's GitHub repo][jekyll-gh].
22
+
23
+ [jekyll-gh]: https://github.com/mojombo/jekyll
24
+ [jekyll]: http://jekyllrb.com
@@ -0,0 +1,13 @@
1
+ ---
2
+ layout: default
3
+ title: Your New Jekyll Site
4
+ ---
5
+
6
+ <div id="home">
7
+ <h1>Blog Posts</h1>
8
+ <ul class="posts">
9
+ {% for post in site.posts %}
10
+ <li><span>{{ post.date | date_to_string }}</span> &raquo; <a href="{{ post.url }}">{{ post.title }}</a></li>
11
+ {% endfor %}
12
+ </ul>
13
+ </div>
@@ -0,0 +1,8 @@
1
+ name: Your New Jekyll Site
2
+ markdown: redcarpet
3
+ pygments: true
4
+
5
+ msgcat:
6
+ locale: uk
7
+ # may be 'domain' or 'nearby'
8
+ deploy: nearby
@@ -0,0 +1,3 @@
1
+ uk:
2
+ 'home': На головну сторiнку
3
+
@@ -0,0 +1,24 @@
1
+ ---
2
+ layout: post
3
+ title: "Welcome to Jekyll!"
4
+ date: 2013-10-24 11:34:36
5
+ categories: jekyll update
6
+ ---
7
+
8
+ You'll find this post in your `_posts` directory - edit this post and re-build (or run with the `-w` switch) to see your changes!
9
+ To add new posts, simply add a file in the `_posts` directory that follows the convention: YYYY-MM-DD-name-of-post.ext.
10
+
11
+ Jekyll also offers powerful support for code snippets:
12
+
13
+ {% highlight ruby %}
14
+ def print_hi(name)
15
+ puts "Hi, #{name}"
16
+ end
17
+ print_hi('Tom')
18
+ #=> prints 'Hi, Tom' to STDOUT.
19
+ {% endhighlight %}
20
+
21
+ Check out the [Jekyll docs][jekyll] for more info on how to get the most out of Jekyll. File all bugs/feature requests at [Jekyll's GitHub repo][jekyll-gh].
22
+
23
+ [jekyll-gh]: https://github.com/mojombo/jekyll
24
+ [jekyll]: http://jekyllrb.com
@@ -0,0 +1,13 @@
1
+ ---
2
+ layout: default
3
+ title: Your New Jekyll Site
4
+ ---
5
+
6
+ <div id="home">
7
+ <h1>Blog Posts</h1>
8
+ <ul class="posts">
9
+ {% for post in site.posts %}
10
+ <li><span>{{ post.date | date_to_string }}</span> &raquo; <a href="{{ post.url }}">{{ post.title }}</a></li>
11
+ {% endfor %}
12
+ </ul>
13
+ </div>
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jekyll/msgcat/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jekyll-msgcat"
8
+ spec.version = Jekyll::Msgcat::VERSION
9
+ spec.authors = ["Alexander Gromnitsky"]
10
+ spec.email = ["alexander.gromnitsky@gmail.com"]
11
+ spec.description = "Multi-Lingual Interface With Jekyll"
12
+ spec.summary = "Multi-lingual interface with Jekyll via .yaml message catalogs."
13
+ spec.homepage = "https://github.com/gromnitsky/jekyll-msgcat"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "bundler", "~> 1.3"
22
+ spec.add_dependency "andand", "~> 1.3.3"
23
+ spec.add_dependency "safe_yaml", "~> 0.9.7"
24
+
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Msgcat
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,89 @@
1
+ require 'andand'
2
+ require 'safe_yaml'
3
+
4
+ require_relative "msgcat/version"
5
+
6
+ module Jekyll
7
+ module Msgcat
8
+ MSGCAT_DB = '_msgcat.yaml'
9
+ @@__msgcat = nil
10
+
11
+ # Return a hash of parsed messge catalog or nil.
12
+ def __get_msgcat
13
+ return @@__msgcat if @@__msgcat
14
+
15
+ begin
16
+ @@__msgcat = YAML.load_file MSGCAT_DB, :safe => false
17
+ rescue
18
+ $stderr.puts "msgcat warning: #{MSGCAT_DB} not found"
19
+ return nil
20
+ end
21
+
22
+ @@__msgcat
23
+ end
24
+
25
+ def __get_site
26
+ @context.registers[:site].config
27
+ end
28
+
29
+ # Return a string ('lt', for example) or nil.
30
+ def __get_locale
31
+ __get_site.andand['msgcat'].andand['locale']
32
+ end
33
+
34
+ # Extract localized version of 'input' from message catalog or
35
+ # return the original if no localization was found.
36
+ def mc input
37
+ locale = __get_locale || (return input)
38
+ msgcat = __get_msgcat || (return input)
39
+
40
+ if !msgcat[locale]
41
+ $stderr.puts "msgcat warning: '#{locale}' wasn't found in #{MSGCAT_DB}"
42
+ return input
43
+ end
44
+
45
+ (msg = msgcat[locale][input]) ? msg : input
46
+ end
47
+
48
+ def cur_page_in_another_locale targetLocale, cssClass = 'btn btn-primary btn-xs'
49
+ raise 'target locale requred' if targetLocale =~ /^\s*$/
50
+
51
+ site = __get_site
52
+ pattern = "<a href='%s' class='#{cssClass} %s'>#{targetLocale}</a>"
53
+ locale = __get_locale || 'en'
54
+
55
+ # current site
56
+ return pattern % ['#', 'disabled'] if locale == targetLocale
57
+
58
+ deploy = site.andand['msgcat'].andand['deploy'] || 'domain'
59
+
60
+ if deploy == 'domain'
61
+ begin
62
+ uri = URI site['url']
63
+ rescue
64
+ raise "invalid 'url' property in _config.yml: #{$!}"
65
+ end
66
+ host = uri.host.split '.'
67
+ host[0] = targetLocale
68
+ uri.host = host.join '.'
69
+ else
70
+ raise "no 'baseurl' property in _config.yml" unless site['baseurl']
71
+ uri = site['baseurl'].split '/'
72
+ if uri.size == 0
73
+ uri = ['', targetLocale]
74
+ else
75
+ uri[uri.size-1] = targetLocale
76
+ end
77
+ uri = uri.join '/'
78
+ end
79
+
80
+ return pattern % [uri + @context.registers[:page]['url'], ""]
81
+ end
82
+
83
+ end
84
+ end
85
+
86
+ # Register a new Liquid filter
87
+ if defined? Liquid::Template
88
+ Liquid::Template.register_filter Jekyll::Msgcat
89
+ end
data/test/_msgcat.yaml ADDED
@@ -0,0 +1,3 @@
1
+ ru:
2
+ 'News': Новости
3
+ 'Write to Us': Напишите нам
@@ -0,0 +1,115 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'pp'
3
+ require 'ostruct'
4
+ require 'uri'
5
+
6
+ require '../lib/jekyll/msgcat'
7
+
8
+ # mock
9
+ module Site_ru
10
+ def initialize *a, &b
11
+ super
12
+ @context = OpenStruct.new("registers" => {
13
+ site: OpenStruct.new("config" => {
14
+ 'msgcat' => {
15
+ 'locale' => 'ru'
16
+ }
17
+ }),
18
+ page: {
19
+ 'url' => '/foo/bar'
20
+ }
21
+ })
22
+
23
+ @context_orig = @context
24
+ end
25
+ end
26
+
27
+ require 'minitest/autorun'
28
+
29
+ class MsgcatTest < Minitest::Test
30
+ include Site_ru
31
+ include Jekyll::Msgcat
32
+
33
+ def setup
34
+ @context = @context_orig
35
+ # pp @context
36
+ end
37
+
38
+ def test_mc_empty
39
+ refute mc nil
40
+ assert_equal "", mc("")
41
+ end
42
+
43
+ def test_mc_no_msgcat_entry
44
+ @context.registers[:site].config['msgcat'] = nil
45
+ assert_equal "", mc("")
46
+ assert_equal "News", mc("News")
47
+ end
48
+
49
+ def test_mc
50
+ assert_equal "Новости", mc("News")
51
+ assert_equal "news", mc("news")
52
+ assert_equal "Напишите нам", mc("Write to Us")
53
+ end
54
+
55
+ def test_invalid_locale_name
56
+ @context.registers[:site].config['msgcat']['locale'] = 'uk'
57
+ out, err = capture_io do
58
+ assert_equal "News", mc("News")
59
+ end
60
+ assert_match(/msgcat warning: 'uk' wasn't found/, err)
61
+ end
62
+
63
+
64
+ def test_cur_page_in_another_locale__this_locale
65
+ assert_equal "<a href='#' class='btn btn-primary btn-xs disabled'>ru</a>", cur_page_in_another_locale('ru')
66
+ end
67
+
68
+ def test_cur_page_in_another_locale__this_locale_custom_class
69
+ assert_equal "<a href='#' class='myclass1 myclass2 disabled'>ru</a>", cur_page_in_another_locale('ru', "myclass1 myclass2")
70
+ end
71
+
72
+ def test_cur_page_in_another_locale__no_url_in_config
73
+ @context.registers[:site].config['url'] = nil
74
+ r = assert_raises(RuntimeError) do
75
+ cur_page_in_another_locale 'lt'
76
+ end
77
+ # pp r
78
+ assert_match(/bad argument/, r.to_s)
79
+ end
80
+
81
+ def test_cur_page_in_another_locale__domain_no_deploy
82
+ @context.registers[:site].config['msgcat']['deploy'] = nil
83
+ @context.registers[:site].config['url'] = 'http://lt.example.com'
84
+ assert_equal "<a href='http://lt.example.com/foo/bar' class='btn btn-primary btn-xs '>lt</a>", cur_page_in_another_locale('lt')
85
+ end
86
+
87
+ def test_cur_page_in_another_locale__domain_no_deploy_no_msgcat
88
+ @context.registers[:site].config['msgcat'] = nil
89
+ @context.registers[:site].config['url'] = 'http://lt.example.com'
90
+ assert_equal "<a href='http://lt.example.com/foo/bar' class='btn btn-primary btn-xs '>lt</a>", cur_page_in_another_locale('lt')
91
+ end
92
+
93
+ def test_cur_page_in_another_locale__domain
94
+ @context.registers[:site].config['msgcat']['deploy'] = 'domain'
95
+ @context.registers[:site].config['url'] = 'http://lt.example.com'
96
+ assert_equal "<a href='http://lt.example.com/foo/bar' class='btn btn-primary btn-xs '>lt</a>", cur_page_in_another_locale('lt')
97
+ end
98
+
99
+ def test_cur_page_in_another_locale__nearby_no_baseurl
100
+ @context.registers[:site].config['msgcat']['deploy'] = 'nearby'
101
+ @context.registers[:site].config['url'] = '/blog/lt'
102
+ r = assert_raises(RuntimeError) do
103
+ cur_page_in_another_locale 'lt'
104
+ end
105
+ assert_match(/no 'baseurl' property/, r.to_s)
106
+ end
107
+
108
+ def test_cur_page_in_another_locale__nearby
109
+ @context.registers[:site].config['msgcat']['locale'] = 'uk'
110
+ @context.registers[:site].config['msgcat']['deploy'] = 'nearby'
111
+ @context.registers[:site].config['baseurl'] = '/blog/lt'
112
+ assert_equal "<a href='/blog/lt/foo/bar' class='btn btn-primary btn-xs '>lt</a>", cur_page_in_another_locale('lt')
113
+ end
114
+
115
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-msgcat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Gromnitsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: andand
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: safe_yaml
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Multi-Lingual Interface With Jekyll
84
+ email:
85
+ - alexander.gromnitsky@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - example/.gitignore
96
+ - example/_layouts/default.html
97
+ - example/_layouts/post.html
98
+ - example/_plugins/msgcat.rb
99
+ - example/css/main.css
100
+ - example/css/syntax.css
101
+ - example/en/_config.yml
102
+ - example/en/_posts/2013-10-24-welcome-to-jekyll.markdown
103
+ - example/en/index.html
104
+ - example/uk/_config.yml
105
+ - example/uk/_msgcat.yaml
106
+ - example/uk/_posts/2013-10-24-welcome-to-jekyll.markdown
107
+ - example/uk/index.html
108
+ - jekyll-msgcat.gemspec
109
+ - lib/jekyll/msgcat.rb
110
+ - lib/jekyll/msgcat/version.rb
111
+ - test/_msgcat.yaml
112
+ - test/test_msgcat.rb
113
+ homepage: https://github.com/gromnitsky/jekyll-msgcat
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.0.7
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Multi-lingual interface with Jekyll via .yaml message catalogs.
137
+ test_files:
138
+ - test/_msgcat.yaml
139
+ - test/test_msgcat.rb