Skip to content

Commit

Permalink
feat: [ST-2294] Implement insert_hreflangs setting (#233)
Browse files Browse the repository at this point in the history
* feat: Implement `insert_hreflangs` setting

* test: Add test

* chore: Bump version

* docs: Update readme

* docs: Replace `WOVN.rb` with `wovnrb`

docs: Fix broken link

* test: Update test name

* Revert "docs: Replace `WOVN.rb` with `wovnrb`"

This reverts commit 2ae9305.

* docs: Replace `wovnrb` with `WOVN.rb`

docs: Fix broken link again
  • Loading branch information
bryanzerrudo authored Aug 21, 2023
1 parent 200750b commit 498d61e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 7 deletions.
13 changes: 10 additions & 3 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ config.wovnrb = {
...
```
The WOVN.rb Rails middleware must also be installed. See [2.10 - install_middleware](#2.10-install_middleware)
The WOVN.rb Rails middleware must also be installed. See [2.10 - install_middleware](#210-install_middleware)
* If you're using Sinatra

Expand Down Expand Up @@ -91,6 +91,7 @@ api_timeout_seconds | | 1.0
api_timeout_search_engine_bots | | 5.0
translate_canonical_tag | | true
custom_domain_langs | | {}
insert_hreflangs | | true
### 2.1. project_token
Expand Down Expand Up @@ -208,7 +209,7 @@ WOVN.rb needs to be added after any compression middleware.
By default, requests to the translation API will be sent with gzip compression. Set to false to disable compression.
### 2.12. api_timeout_seconds
Configures the amount of time in seconds wovnrb will wait for the translation API for a response before the
Configures the amount of time in seconds WOVN.rb will wait for the translation API for a response before the
request is considered timed-out. This setting defaults to `1.0`.
### 2.13. api_timeout_search_engine_bots
Expand All @@ -217,7 +218,7 @@ Currently, bots from Google, Yahoo, Bing, Yandex, DuckDuckGo and Baidu are suppo
defaults to `5.0`.
### 2.14. translate_canonical_tag
Configures if wovnrb should automatically translate existing canonical tag in the HTML. When set to `true`, wovnrb
Configures if WOVN.rb should automatically translate existing canonical tag in the HTML. When set to `true`, WOVN.rb
will translate the canonical URL with the current language code according to your `url_pattern` setting.
This setting defaults to `true`.
Expand Down Expand Up @@ -247,3 +248,9 @@ If this setting is used, each language declared in `supported_langs` must be giv
The path declared for your original language must match the structure of the actual web server.
In other words, you cannot use this setting to change the request path of your content in original language.
### 2.16. `insert_hreflangs`
This parameter tells WOVN.rb to insert link tag with hreflang.
If setting is on, the tag like `<link rel="alternate" hreflang="en" href="https://my-website.com/en/">` will be inserted for published languages.
If setting is off, WOVN.rb doesn't add any change to link tag with hreflang.
9 changes: 8 additions & 1 deletion README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ignore_class | | []
translate_fragment | | true
ignore_paths | | []
custom_domain_langs| | {}
insert_hreflangs | | true
### 2.1. project_token
Expand Down Expand Up @@ -208,4 +209,10 @@ config.wovnrb = {
`supported_langs` で宣言された各言語に `custom_domain_langs` を与えなければなりません。
オリジナル言語のために宣言されたパスは、実際のウェブサーバーの構造と一致していなければなりません。
この設定を使用して、オリジナル言語のリクエストパスを変更することはできません。
この設定を使用して、オリジナル言語のリクエストパスを変更することはできません。
### 2.11. insert_hreflangs
このパラメータはhreflang属性を持つlinkタグを挿入するかどうかを指定します。
例えば設定が有効の場合、`<link rel="alternate" hreflang="en" href="https://my-website.com/en/">`のように、公開されている言語のタグを挿入します。
設定が無効の場合はタグは挿入せず、元からあるhreflang属性を持ったタグに変更は加えません。
5 changes: 5 additions & 0 deletions lib/wovnrb/api_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def build_api_params(body)
'url_pattern' => url_pattern,
'lang_param_name' => lang_param_name,
'translate_canonical_tag' => translate_canonical_tag,
'insert_hreflangs' => insert_hreflangs,
'product' => 'WOVN.rb',
'version' => VERSION,
'body' => body
Expand Down Expand Up @@ -163,6 +164,10 @@ def translate_canonical_tag
@store.settings['translate_canonical_tag']
end

def insert_hreflangs
@store.settings['insert_hreflangs']
end

def custom_domain_langs
@store.custom_domain_langs.to_html_swapper_hash
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wovnrb/services/html_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def replace_dom(marker)
@dom.traverse { |node| transform_node(node, marker) }

insert_snippet(adds_backend_error_mark: true)
insert_hreflang_tags
insert_hreflang_tags if @store.settings['insert_hreflangs']
inject_lang_html_tag
translate_canonical_tag if @store.settings['translate_canonical_tag']

Expand Down
3 changes: 2 additions & 1 deletion lib/wovnrb/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def self.default_settings
'wovn_dev_mode' => false,
'compress_api_requests' => true,
'translate_canonical_tag' => true,
'custom_domain_langs' => {}
'custom_domain_langs' => {},
'insert_hreflangs' => true
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/wovnrb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wovnrb
VERSION = '3.9.0'.freeze
VERSION = '3.10.0'.freeze
end
2 changes: 2 additions & 0 deletions test/lib/api_translator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_translate_without_api_compression_sends_json
'url_pattern' => 'subdomain',
'lang_param_name' => 'lang',
'translate_canonical_tag' => true,
'insert_hreflangs' => true,
'product' => 'WOVN.rb',
'version' => VERSION,
'body' => 'foo',
Expand Down Expand Up @@ -199,6 +200,7 @@ def generate_data(original_html)
'url_pattern' => 'subdomain',
'lang_param_name' => 'lang',
'translate_canonical_tag' => true,
'insert_hreflangs' => true,
'product' => 'WOVN.rb',
'version' => VERSION,
'body' => original_html,
Expand Down
9 changes: 9 additions & 0 deletions test/lib/services/html_converter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,15 @@ def store_headers_factory(setting_opts = {}, url = 'http://my-site.com')
[store, headers]
end

test 'build API compatible html - with insert_hreflangs: false' do
settings = { insert_hreflangs: false }
converter = prepare_html_converter('<html><body><a class="test">hello</a></body></html>', settings)
converted_html, = converter.build_api_compatible_html

expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script></head><body><a class=\"test\">hello</a></body></html>"
assert_equal(expected_html, converted_html)
end

def default_store_settings
{
'project_token' => '123456',
Expand Down

0 comments on commit 498d61e

Please sign in to comment.