wovnrb 2.2.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1d7702d09e87b697371b1c5609040910fa9383d55a05ebac5d1ae668b04013c
4
- data.tar.gz: c18162741b2cbb82137687cd6b9a0176cc576cae8230c3720d4ebd19df065656
3
+ metadata.gz: 87e1de8bb4d4e0aacac97628ff3f02cc52756f710897e92124d9c05019125fe3
4
+ data.tar.gz: 4cfa377c062238076d77f77e1dc4d8b4a1f539fb78751b8b3a846e0fe70b3269
5
5
  SHA512:
6
- metadata.gz: 3285cbeb773a4415e82fd8cc06b375d4e3e0adea37c2e026699f401cd2692a67972aef40513a209b1d39686fb52664bc3964d6d8be69088d716b960f9e8e1ef3
7
- data.tar.gz: ea6c7b83a2a2b6c16c5d7d5310ec17e021a4e1b87524cc60de3794230890aeb8742205e7197ba9bbd74d492092523f59bcd48f576440729e6a014919d11f4313
6
+ metadata.gz: eb76b8e1d6049620933f25f7921940fd6006b04fda13fba82f86f414906878c6c6c84d93918935a9ab2548c525fb116000e8480cac5c444265f92bdffaf38b5f
7
+ data.tar.gz: 1771496f33d419c64fdf3b3be112db32e5dc8d788c3f4cd3dc14ee3e25e272106dc8d93cd86df5e8fe56dde2ec1e2ba1efa60575b5054fb6223a674f1f833af3
data/.rubocop_todo.yml CHANGED
@@ -29,7 +29,7 @@ Layout/ExtraSpacing:
29
29
  # Cop supports --auto-correct.
30
30
  # Configuration parameters: EnforcedStyle, IndentationWidth.
31
31
  # SupportedStyles: special_inside_parentheses, consistent, align_braces
32
- Layout/IndentFirstHashElement:
32
+ Layout/FirstHashElementIndentation:
33
33
  Exclude:
34
34
  - 'lib/wovnrb/text_caches/cache_base.rb'
35
35
 
@@ -37,7 +37,7 @@ Layout/IndentFirstHashElement:
37
37
  # Cop supports --auto-correct.
38
38
  # Configuration parameters: EnforcedStyle.
39
39
  # SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent
40
- Layout/IndentHeredoc:
40
+ Layout/HeredocIndentation:
41
41
  Exclude:
42
42
  - 'test/lib/lang_test.rb'
43
43
  - 'test/lib/wovnrb_test.rb'
data/README.en.md ADDED
@@ -0,0 +1,174 @@
1
+ # WOVN.io Ruby Library
2
+
3
+ The WOVN.io Ruby library is a library that uses WOVN.io in order to provide translations. The WOVN.io Ruby Library is packaged as Rack Middleware.
4
+
5
+ This document explains the process of installing WOVN.io Ruby, as well as set up and configuration process.
6
+
7
+ ## 1. Install
8
+
9
+ ### 1.1. Creating a WOVN.io account.
10
+
11
+ In order to use the WOVN.io Ruby Library, you need a WOVN.io account. If you do not have an account, please first sign up for one at WOVN.io.
12
+
13
+ ### 1.2. Adding a Page
14
+
15
+ After logging into WOVN.io, add a page that you would like translated.
16
+
17
+ ### 1.3. Ruby Application Settings
18
+
19
+ To use the WOVN.io Ruby Library, insert the following line into your Ruby Application's Gemfile. WOVN.io currently supports version 2.0.1 and up.
20
+
21
+ ```ruby
22
+ gem 'wovnrb', '>= 2.0.1'
23
+ ```
24
+
25
+ After setting the above, execute the following command to install the WOVN.io Ruby Library.
26
+
27
+ ```bash
28
+ bundle install
29
+ ```
30
+
31
+ After installing the library, insert the following into your Ruby Application's settings file.
32
+
33
+ * If you're using Ruby on Rails
34
+
35
+ Insert the following into either config/application.rb or config/environments/.
36
+
37
+ ```ruby
38
+ ...
39
+
40
+ config.wovnrb = {
41
+ :project_token => 'EnS!t3',
42
+ :default_lang => 'en',
43
+ :supported_langs => ['en'],
44
+ :url_pattern => 'path'
45
+ }
46
+
47
+ ...
48
+ ```
49
+
50
+ * If you're using Sinatra
51
+
52
+ Insert the following into either the Application File or config.ru.
53
+
54
+ ```ruby
55
+ ...
56
+
57
+ require 'wovnrb'
58
+
59
+ use Wovnrb::Interceptor, {
60
+ :project_token => 'EnS!t3',
61
+ :default_lang => 'en',
62
+ :supported_langs => ['en'],
63
+ :url_pattern => 'path'
64
+ }
65
+
66
+ ...
67
+ ```
68
+
69
+ After completing setup, start the Ruby Application, and make sure the WOVN.io library is working correctly.
70
+
71
+ ## 2. Parameter Setting
72
+
73
+ The following is a list of the WOVN.io Ruby Library's valid parameters.
74
+
75
+ Parameter Name | Required | Default Setting
76
+ ------------------ | -------- | ----------------
77
+ project_token | yes | ''
78
+ default_lang | yes | 'en'
79
+ supported_langs | yes | ['en']
80
+ url_pattern | yes | 'path'
81
+ lang_param_name | | 'wovn'
82
+ query | | []
83
+ ignore_class | | []
84
+ translate_fragment | | true
85
+ ignore_paths | | []
86
+
87
+ ### 2.1. project_token
88
+
89
+ Set your WOVN.io Account's Project token. This parameter is required.
90
+
91
+ ### 2.2. default_lang
92
+
93
+ This sets the Ruby application's default language. The default value is English ('en').
94
+
95
+ If, for a requested page, the default language parameter is included in the URL, the request is redirected before translating. The default_lang parameter is used for this purpose.
96
+
97
+ If the default_lang is set to 'en', when receiving a request for the following URL,
98
+
99
+ https://wovn.io/en/contact
100
+
101
+ Then the library will redirect to the following URL.
102
+
103
+ https://wovn.io/contact
104
+
105
+ ### 2.3. supported_langs
106
+ This tells the library which languages are being used on the website (including
107
+ the original language). This setting allows for inserting metadata necessary for
108
+ SEO (Search Engine Optimization).
109
+
110
+ If your website is in English and you are using WOVN.io to localize it in
111
+ Japanese, then you should use the following setting:
112
+ ```
113
+ :supported_langs => ['en', 'ja']
114
+ ```
115
+ **Note:** The order of the languages in the list does not matter.
116
+
117
+ ### 2.4. url_pattern
118
+
119
+ The Library works in the Ruby Application by adding new URLs to be translated. You can set the type of url with the `url_pattern` parameter. There are 3 types that can be set.
120
+
121
+ parameters | Translated page's URL | Notes
122
+ ----------- | ------------------------------- | -------
123
+ 'path' | https://wovn.io/ja/contact | Default Value. If no settings have been set, url_pattern defaults to this value.
124
+ 'subdomain' | https://ja.wovn.io/contact | DNS settings must be set.
125
+ 'query' | https://wovn.io/contact?wovn=ja | The least amount of changes to the application required to complete setup.
126
+
127
+ ※ The following is an example of a URL that has been translated by the library using the above URLs.
128
+
129
+ https://wovn.io/contact
130
+
131
+ ### 2.5 lang_param_name
132
+ This parameter is only valid for when `url_pattern` is set to `'query'`.
133
+
134
+ It allows you to set the query parameter name for declaring the language of the
135
+ page. The default value for this setting is `'wovn'`, such that a page URL in
136
+ translated language English has the form
137
+ `https://my-website.com/index.php?wovn=en`. If you instead set the value to
138
+ `'lang'`, then the later URL example would have the form
139
+ `https://my-website.com/index.php?lang=en`.
140
+
141
+ ### 2.6. query
142
+
143
+ WOVN.io ignores query parameters when searching a translated page. If you want to add a query parameter to translated page's URL, you should configure the `query` parameter. (You need to configure WOVN.io too)
144
+
145
+ https://wovn.io/ja/contact?os=mac&keyboard=us
146
+
147
+ If the `default_lang` is 'en', and the query is set to '', the above URL will be modified into the following URL to search for the page's translation.
148
+
149
+ https://wovn.io/contact
150
+
151
+ If the `default_lang` is 'en', and the query is set to 'os', the above URL will be modified into the following URL to search for the page's translation.
152
+
153
+ https://wovn.io/contact?os=mac
154
+
155
+ ### 2.7. ignore_class
156
+
157
+ This sets "Ignore class" which prevents WOVN from translating HTML elements that have a class contained in this array.
158
+
159
+ ### 2.8. translate_fragment
160
+
161
+ This option allows to disable translating partial HTML content. By default,
162
+ partial HTML content is translated but no widget snippet is added. Set
163
+ `translate_fragment` to `false` to prevent translating partial HTML content.
164
+
165
+ ### 2.9 ignore_paths
166
+
167
+ This parameter tells WOVN.rb to not localize content withing given directories.
168
+
169
+ The directories given will only be matched against the beginning of the URL path.
170
+
171
+ For instance, if you want to not localize the admin directory of your website, you should add the following to you WOVN.rb configuration.
172
+ ```
173
+ 'ignore_paths' => ['/admin/']
174
+ ```
data/README.ja.md ADDED
@@ -0,0 +1,180 @@
1
+ # WOVN.io Ruby Library
2
+
3
+ For English users: [English](README.en.md)
4
+
5
+ WOVN.io Rubyライブラリは、翻訳を提供するためにWOVN.ioを利用したライブラリです。
6
+ WOVN.io Ruby ライブラリは Rack Middleware としてパッケージ化されています。
7
+
8
+ このドキュメントでは、WOVN.io Rubyをインストールするまでの手順や、セットアップ・設定の流れを説明しています。
9
+
10
+ ## 1. インストール
11
+
12
+ ### 1.1. WOVN.ioアカウントを作成
13
+
14
+ WOVN.io Ruby Libraryを利用するには、WOVN.ioのアカウントが必要です。
15
+ アカウントをお持ちでない方は、まずはWOVN.ioに登録してください。
16
+
17
+ ### 1.2. ページ追加
18
+
19
+ WOVN.ioにログイン後、翻訳したいページを追加します。
20
+
21
+ ### 1.3. Rubyアプリケーションの設定
22
+
23
+ WOVN.io Ruby ライブラリを使用するには、以下の行を Ruby アプリケーションの Gemfile に挿入します。
24
+ WOVN.ioは現在、バージョン2.0.1以上をサポートしています。
25
+
26
+ ```ruby
27
+ gem 'wovnrb', '>= 2.0.1'
28
+ ```
29
+
30
+ 以上の設定を行った後、以下のコマンドを実行して、WOVN.io Rubyライブラリをインストールします。
31
+
32
+ ```bash
33
+ bundle install
34
+ ```
35
+
36
+ ライブラリをインストールしたら、Rubyアプリケーションの設定ファイルに以下を挿入します。
37
+
38
+ * Ruby on Rails を使っている場合
39
+
40
+ `config/application.rb` または `config/environments/` に以下を挿入します。
41
+
42
+ ```ruby
43
+ ...
44
+
45
+ config.wovnrb = {
46
+ :project_token => 'EnS!t3',
47
+ :default_lang => 'en',
48
+ :supported_langs => ['en'],
49
+ :url_pattern => 'path'
50
+ }
51
+
52
+ ...
53
+ ```
54
+
55
+ * Sinatra を使っている場合
56
+
57
+ アプリケーション・ファイルまたは `config.ru` のいずれかに以下を挿入します。
58
+
59
+ ```ruby
60
+ ...
61
+
62
+ require 'wovnrb'
63
+
64
+ use Wovnrb::Interceptor, {
65
+ :project_token => 'EnS!t3',
66
+ :default_lang => 'en',
67
+ :supported_langs => ['en'],
68
+ :url_pattern => 'path'
69
+ }
70
+
71
+ ...
72
+ ```
73
+
74
+ セットアップが完了したら、Rubyアプリケーションを起動し、WOVN.ioライブラリが正常に動作していることを確認します。
75
+
76
+ ## 2. パラメータ設定
77
+
78
+ 以下に、WOVN.io Ruby ライブラリの有効なパラメータの一覧を示します。
79
+
80
+ パラメータ名 | 必須 | デフォルト設定
81
+ ------------------ | -------- | ----------------
82
+ project_token | yes | ''
83
+ default_lang | yes | 'en'
84
+ supported_langs | yes | ['en']
85
+ url_pattern | yes | 'path'
86
+ lang_param_name | | 'wovn'
87
+ query | | []
88
+ ignore_class | | []
89
+ translate_fragment | | true
90
+ ignore_paths | | []
91
+
92
+ ### 2.1. project_token
93
+
94
+ WOVN.ioアカウントのプロジェクトトークンを設定します。このパラメータは必須です。
95
+
96
+ ### 2.2. default_lang
97
+
98
+ これはRubyアプリケーションのデフォルト言語を設定します。デフォルト値は英語('en')です。
99
+
100
+ リクエストされたページで、デフォルトの言語パラメータが URL に含まれている場合、リクエストは翻訳前にリダイレクトされます。
101
+ このために `default_lang` パラメータを使用します。
102
+
103
+ `default_lang` が 'en' に設定されている時に、以下のURLへのリクエストを受信した場合。
104
+
105
+ https://wovn.io/en/contact
106
+
107
+ ライブラリは以下のURLにリダイレクトされます。
108
+
109
+ https://wovn.io/contact
110
+
111
+ ### 2.3. supported_langs
112
+
113
+ これは、ウェブサイトで使用されている言語(翻訳元を含む)をライブラリに伝えます。
114
+ この設定では、SEO(検索エンジン最適化)に必要なメタデータを挿入することができます。
115
+
116
+ ウェブサイトが英語で、WOVN.ioを使って日本語にローカライズしている場合は、以下の設定にしてください。
117
+ ```
118
+ :supported_langs => ['en', 'ja']
119
+ ```
120
+ **注意:** 配列の言語の順番は関係ありません。
121
+
122
+ ### 2.4. url_pattern
123
+
124
+ ライブラリはRubyアプリケーションの中で、翻訳されるURLを新たに追加することで動作します。
125
+ urlの種類は `url_pattern` パラメータで設定できます。設定できるタイプは3種類。
126
+
127
+
128
+ パラメータ | 翻訳されたページのURL | 備考
129
+ ----------- | ------------------------------- | -------
130
+ 'path' | https://wovn.io/ja/contact | デフォルト値、何も設定されていない場合、 `url_pattern` のデフォルト値はこの値になります
131
+ 'subdomain' | https://ja.wovn.io/contact | DNSの設定が必要です
132
+ 'query' | https://wovn.io/contact?wovn=ja | セットアップを完了するために必要なアプリケーションへの変更の最小量
133
+
134
+ ※ 上記は、以下のURLを使用してライブラリが翻訳したURLの例です。
135
+
136
+ https://wovn.io/contact
137
+
138
+ ### 2.5 lang_param_name
139
+
140
+ このパラメータは `url_pattern` が `query` に設定されている場合のみ有効です。
141
+
142
+ ページの言語を指定するためのクエリパラメータ名を設定することができます。
143
+
144
+ この設定のデフォルト値は `wovn` であり、翻訳された英語のページのURLが `https://my-website.com/index.php?wovn=en` という形式になります。
145
+ 代わりに `lang` に値を設定すると、URLの例は `https://my-website.com/index.php?lang=en` という形式になります。
146
+
147
+ ### 2.6. query
148
+
149
+ WOVN.ioは翻訳されたページを検索する際にクエリパラメータを無視します。
150
+ 翻訳ページのURLにクエリパラメータを追加したい場合は、`query` パラメータを設定する必要があります。(WOVN.ioも設定する必要があります)
151
+
152
+ https://wovn.io/ja/contact?os=mac&keyboard=us
153
+
154
+ `default_lang` が `en` でクエリが空に設定されている場合、上記のURLは以下のURLに変更され、ページの翻訳を検索するようになります。
155
+
156
+ https://wovn.io/contact
157
+
158
+ `default_lang` が `en` でクエリが `os` に設定されている場合、上記のURLは以下のURLに変更され、ページの翻訳を検索するようになります。
159
+
160
+ https://wovn.io/contact?os=mac
161
+
162
+ ### 2.7. ignore_class
163
+
164
+ これは「無視するクラス」を設定し、WOVNがこの配列に含まれるクラスを持つHTML要素を変換できないようにします。
165
+
166
+ ### 2.8. translate_fragment
167
+
168
+ このオプションでは、部分的なHTMLコンテンツの翻訳を無効にすることができます。
169
+ デフォルトでは、部分的なHTMLコンテンツは翻訳されますが、ウィジェットのスニペットは追加されません。
170
+ `translate_fragment` を `false` に設定すると、HTMLの一部が翻訳されないようになります。
171
+
172
+ ### 2.9 ignore_paths
173
+
174
+ このパラメータは、WOVN.rbが指定されたディレクトリ内のコンテンツをローカライズしないように指示します。
175
+ 指定されたディレクトリは、URLパスの先頭にのみマッチします。
176
+ 例えば、ウェブサイトの管理者ディレクトリをローカライズしたくない場合は、WOVN.rbの設定に次のように追加します。
177
+
178
+ ```
179
+ 'ignore_paths' => ['/admin/']
180
+ ```
data/README.md ADDED
@@ -0,0 +1 @@
1
+ ./README.ja.md
data/lib/wovnrb.rb CHANGED
@@ -109,7 +109,7 @@ module Wovnrb
109
109
  end
110
110
 
111
111
  def wovn_ignored?(html_body)
112
- !html_body.xpath('//html[@wovn-ignore]').empty?
112
+ !html_body.xpath('//html[@wovn-ignore or @data-wovn-ignore]').empty?
113
113
  end
114
114
 
115
115
  def ignore_path?(path)
@@ -109,7 +109,7 @@ module Wovnrb
109
109
  end
110
110
 
111
111
  def api_uri
112
- Addressable::URI.parse(@store.settings['api_url'])
112
+ Addressable::URI.parse(@store.settings['api_url'] + '/v0/')
113
113
  end
114
114
 
115
115
  def api_timeout
@@ -8,7 +8,6 @@ module Wovnrb
8
8
  attr_reader :unmasked_pathname
9
9
  attr_reader :pathname
10
10
  attr_reader :pathname_with_trailing_slash_if_present
11
- attr_reader :redis_url
12
11
 
13
12
  # Generates new instance of Wovnrb::Headers.
14
13
  # Its parameters are set by parsing env variable.
@@ -30,7 +29,7 @@ module Wovnrb
30
29
  end
31
30
  # REQUEST_URI is expected to not contain the server name
32
31
  # heroku contains http://...
33
- @env['REQUEST_URI'] = @env['REQUEST_URI'].sub(/^.*:\/\/[^\/]+/, '') if @env['REQUEST_URI'] =~ /:\/\//
32
+ @env['REQUEST_URI'] = @env['REQUEST_URI'].sub(/^https?:\/\/[^\/]+/, '') if @env['REQUEST_URI'] =~ /^https?:\/\//
34
33
  @unmasked_pathname = @env['REQUEST_URI'].split('?')[0]
35
34
  @unmasked_pathname += '/' unless @unmasked_pathname =~ /\/$/ || @unmasked_pathname =~ /\/[^\/.]+\.[^\/.]+$/
36
35
  @unmasked_url = "#{@protocol}://#{@unmasked_host}#{@unmasked_pathname}"
@@ -62,7 +61,6 @@ module Wovnrb
62
61
  @query = remove_lang(@query, lang_code)
63
62
  @pathname_with_trailing_slash_if_present = @pathname
64
63
  @pathname = @pathname.gsub(/\/$/, '')
65
- @redis_url = "#{@host}#{@pathname}#{@query}"
66
64
  end
67
65
 
68
66
  def unmasked_pathname_without_trailing_slash
@@ -98,27 +96,7 @@ module Wovnrb
98
96
  @path_lang
99
97
  end
100
98
 
101
- def browser_lang
102
- if @browser_lang.nil?
103
- match = (@env['HTTP_COOKIE'] || '').match(/wovn_selected_lang\s*=\s*(?<lang>[^;\s]+)/)
104
- if match && match[:lang] && Lang.get_lang(match[:lang])
105
- @browser_lang = match[:lang]
106
- else
107
- # IS THIS RIGHT?
108
- @browser_lang = ''
109
- accept_langs = (@env['HTTP_ACCEPT_LANGUAGE'] || '').split(/[,;]/)
110
- accept_langs.each do |l|
111
- if Lang.get_lang(l)
112
- @browser_lang = l
113
- break
114
- end
115
- end
116
- end
117
- end
118
- @browser_lang
119
- end
120
-
121
- def redirect(lang = browser_lang)
99
+ def redirect(lang)
122
100
  redirect_headers = {}
123
101
  redirect_headers['location'] = redirect_location(lang)
124
102
  redirect_headers['content-length'] = '0'
@@ -193,13 +171,13 @@ module Wovnrb
193
171
  case @settings['url_pattern']
194
172
  when 'query'
195
173
  lang_param_name = @settings['lang_param_name']
196
- return uri.sub(/(^|\?|&)#{lang_param_name}=#{lang_code}(&|$)/, '\1').gsub(/(\?|&)$/, '')
174
+ uri.sub(/(^|\?|&)#{lang_param_name}=#{lang_code}(&|$)/, '\1').gsub(/(\?|&)$/, '')
197
175
  when 'subdomain'
198
176
  rp = Regexp.new('(^|(//))' + lang_code + '\.', 'i')
199
- return uri.sub(rp, '\1')
177
+ uri.sub(rp, '\1')
200
178
  # when 'path'
201
179
  else
202
- return uri.sub(/\/#{lang_code}(\/|$)/, '/')
180
+ uri.sub(/\/#{lang_code}(\/|$)/, '/')
203
181
  end
204
182
  end
205
183