A platform for community discussion. Free, open, simple.
FIX: Localize text in github oneboxes (#25327)
Jan Cernikd40150283490
diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex b8d20629f61..0b086f6c688 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -5235,6 +5235,22 @@ en:onebox:discourse:user_joined_community: "Joined %{date}"+ gitlab:+ truncated_file: "This file has been truncated."+ show_original: "show original"+ github:+ binary_file: "This file is binary."+ truncated_file: "This file has been truncated."+ show_original: "show original"+ requires_iframe: "Viewer requires iframe."+ committed: "committed"+ more_than_three_files: "There are more than three files."+ opened: "opened"+ closed: "closed"+ commit_by: "Commit by"+ comment_by: "Comment by"+ review_by: "Review by"+ pr_summary: "%{commits} commits changed %{changed_files} files with %{additions} additions and %{deletions} deletions"discourse_push_notifications:popup:diff --git a/lib/onebox/engine/github_blob_onebox.rb b/lib/onebox/engine/github_blob_onebox.rbindex 48a7e6dec05..4b4ed67f271 100644--- a/lib/onebox/engine/github_blob_onebox.rb+++ b/lib/onebox/engine/github_blob_onebox.rb@@ -15,6 +15,15 @@ module Oneboxinclude Onebox::Mixins::GitBlobOnebox+ def i18n+ {+ binary_file: I18n.t("onebox.github.binary_file"),+ truncated_file: I18n.t("onebox.github.truncated_file"),+ show_original: I18n.t("onebox.github.show_original"),+ requires_iframe: I18n.t("onebox.github.requires_iframe"),+ }+ end+def raw_regexp%r{github\.com/(?<user>[^/]+)/(?<repo>[^/]+)/blob/(?<sha1>[^/]+)/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?}mienddiff --git a/lib/onebox/engine/github_commit_onebox.rb b/lib/onebox/engine/github_commit_onebox.rbindex e1fa68547ed..f7c81a103ab 100644--- a/lib/onebox/engine/github_commit_onebox.rb+++ b/lib/onebox/engine/github_commit_onebox.rb@@ -47,6 +47,7 @@ module Oneboxresult["link"] = linkulink = URI(link)result["domain"] = "#{ulink.host}/#{ulink.path.split("/")[1]}/#{ulink.path.split("/")[2]}"+ result["i18n"] = { committed: I18n.t("onebox.github.committed") }resultenddiff --git a/lib/onebox/engine/github_gist_onebox.rb b/lib/onebox/engine/github_gist_onebox.rbindex ad579428d25..5ddcb2bbc84 100644--- a/lib/onebox/engine/github_gist_onebox.rb+++ b/lib/onebox/engine/github_gist_onebox.rb@@ -24,6 +24,15 @@ module Oneboxlink: link,gist_files: gist_files.take(MAX_FILES),truncated_files?: truncated_files?,+ i18n: i18n,+ }+ end++ def i18n+ {+ truncated_file: I18n.t("onebox.github.truncated_file"),+ more_than_three_files: I18n.t("onebox.github.more_than_three_files"),+ show_original: I18n.t("onebox.github.show_original"),}enddiff --git a/lib/onebox/engine/github_issue_onebox.rb b/lib/onebox/engine/github_issue_onebox.rbindex ef09cac8f9b..385c8cbce48 100644--- a/lib/onebox/engine/github_issue_onebox.rb+++ b/lib/onebox/engine/github_issue_onebox.rb@@ -30,6 +30,10 @@ module Onebox)end+ def i18n+ { opened: I18n.t("onebox.github.opened"), closed: I18n.t("onebox.github.closed") }+ end+def datacreated_at = Time.parse(raw["created_at"])closed_at = Time.parse(raw["closed_at"]) if raw["closed_at"]@@ -54,6 +58,7 @@ module Oneboxclosed_by: raw["closed_by"],avatar: "https://avatars1.githubusercontent.com/u/#{raw["user"]["id"]}?v=2&s=96",domain: "#{ulink.host}/#{ulink.path.split("/")[1]}/#{ulink.path.split("/")[2]}",+ i18n: i18n,}endenddiff --git a/lib/onebox/engine/github_pull_request_onebox.rb b/lib/onebox/engine/github_pull_request_onebox.rbindex 77310cc46f9..6ad192fc2d6 100644--- a/lib/onebox/engine/github_pull_request_onebox.rb+++ b/lib/onebox/engine/github_pull_request_onebox.rb@@ -50,10 +50,29 @@ module Oneboxelseresult["pr"] = trueend+ result["i18n"] = i18n+ result["i18n"]["pr_summary"] = I18n.t(+ "onebox.github.pr_summary",+ {+ commits: result["commits"],+ changed_files: result["changed_files"],+ additions: result["additions"],+ deletions: result["deletions"],+ },+ )resultend+ def i18n+ {+ opened: I18n.t("onebox.github.opened"),+ commit_by: I18n.t("onebox.github.commit_by"),+ comment_by: I18n.t("onebox.github.comment_by"),+ review_by: I18n.t("onebox.github.review_by"),+ }+ end+def load_commit(link)if commit_match = link.match(%r{commits/(\h+)})load_json(diff --git a/lib/onebox/engine/gitlab_blob_onebox.rb b/lib/onebox/engine/gitlab_blob_onebox.rbindex c948e5bf6df..a3a3eccac76 100644--- a/lib/onebox/engine/gitlab_blob_onebox.rb+++ b/lib/onebox/engine/gitlab_blob_onebox.rb@@ -15,6 +15,13 @@ module Oneboxinclude Onebox::Mixins::GitBlobOnebox+ def i18n+ {+ truncated_file: I18n.t("onebox.gitlab.truncated_file"),+ show_original: I18n.t("onebox.gitlab.show_original"),+ }+ end+def raw_regexp%r{gitlab\.com/(?<user>[^/]+)/(?<repo>[^/]+)/blob/(?<sha1>[^/]+)/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?}mienddiff --git a/lib/onebox/mixins/git_blob_onebox.rb b/lib/onebox/mixins/git_blob_onebox.rbindex b307cf5681b..6d8b0fd29df 100644--- a/lib/onebox/mixins/git_blob_onebox.rb+++ b/lib/onebox/mixins/git_blob_onebox.rb@@ -220,6 +220,7 @@ module Onebox@data ||= {title: title,link: link,+ i18n: i18n,# IMPORTANT NOTE: All of the other class variables are populated# as *side effects* of the `raw` method! They must all appear# AFTER the call to `raw`! Don't get bitten by this like I did!diff --git a/lib/onebox/templates/githubblob.mustache b/lib/onebox/templates/githubblob.mustacheindex b76dfd2a337..573fb5e651f 100644--- a/lib/onebox/templates/githubblob.mustache+++ b/lib/onebox/templates/githubblob.mustache@@ -4,7 +4,7 @@{{^has_lines}}{{#model_file}}<iframe class="render-viewer" width="{{width}}" height="{{height}}" src="{{content}}" sandbox="allow-scripts allow-same-origin allow-top-navigation ">- Viewer requires iframe.+ {{i18n.requires_iframe}}</iframe>{{/model_file}}@@ -51,9 +51,9 @@{{/binary}}{{#binary}}- This file is binary. <a href="{{link}}" target="_blank" rel="noopener">show original</a>+ {{i18n.binary_file}} <a href="{{link}}" target="_blank" rel="noopener">{{i18n.show_original}}</a>{{/binary}}{{#truncated}}- This file has been truncated. <a href="{{link}}" target="_blank" rel="noopener">show original</a>+ {{i18n.truncated_file}} <a href="{{link}}" target="_blank" rel="noopener">{{i18n.show_original}}</a>{{/truncated}}diff --git a/lib/onebox/templates/githubcommit.mustache b/lib/onebox/templates/githubcommit.mustacheindex b9d15cb76f0..bf563f772b9 100644--- a/lib/onebox/templates/githubcommit.mustache+++ b/lib/onebox/templates/githubcommit.mustache@@ -10,7 +10,7 @@<div class="github-info"><div class="date">- committed <span class="discourse-local-date" data-format="ll" data-date="{{committed_at_date}}" data-time="{{committed_at_time}}" data-timezone="UTC">{{committed_at}}</span>+ {{i18n.committed}} <span class="discourse-local-date" data-format="ll" data-date="{{committed_at_date}}" data-time="{{committed_at_time}}" data-timezone="UTC">{{committed_at}}</span></div><div class="user">diff --git a/lib/onebox/templates/githubgist.mustache b/lib/onebox/templates/githubgist.mustacheindex eaad9530081..4cbd22514fd 100644--- a/lib/onebox/templates/githubgist.mustache+++ b/lib/onebox/templates/githubgist.mustache@@ -4,12 +4,12 @@<h5>{{filename}}</h5><pre><code class="{{language}}">{{content}}</code></pre>{{#truncated?}}- This file has been truncated. <a href="{{link}}" target="_blank" rel="noopener">show original</a>+ {{i18n.truncated_file}} <a href="{{link}}" target="_blank" rel="noopener">{{i18n.show_original}}</a>{{/truncated?}}{{/gist_files}}<p>{{#truncated_files?}}- There are more than three files. <a href="{{link}}" target="_blank" rel="noopener">show original</a>+ {{i18n.more_than_three_files}} <a href="{{link}}" target="_blank" rel="noopener">{{i18n.show_original}}</a>{{/truncated_files?}}</p>diff --git a/lib/onebox/templates/githubissue.mustache b/lib/onebox/templates/githubissue.mustacheindex 54989581c0f..c241e08cefe 100644--- a/lib/onebox/templates/githubissue.mustache+++ b/lib/onebox/templates/githubissue.mustache@@ -10,12 +10,12 @@<div class="github-info"><div class="date">- opened <span class="discourse-local-date" data-format="ll" data-date="{{created_at_date}}" data-time="{{created_at_time}}" data-timezone="UTC">{{created_at}}</span>+ {{i18n.opened}} <span class="discourse-local-date" data-format="ll" data-date="{{created_at_date}}" data-time="{{created_at_time}}" data-timezone="UTC">{{created_at}}</span></div>{{#closed_at}}<div class="date">- closed <span class="discourse-local-date" data-format="ll" data-date="{{closed_at_date}}" data-time="{{closed_at_time}}" data-timezone="UTC">{{closed_at}}</span>+ {{i18n.closed}} <span class="discourse-local-date" data-format="ll" data-date="{{closed_at_date}}" data-time="{{closed_at_time}}" data-timezone="UTC">{{closed_at}}</span></div>{{/closed_at}}diff --git a/lib/onebox/templates/githubpullrequest.mustache b/lib/onebox/templates/githubpullrequest.mustacheindex 520c02d4429..c57f08e8274 100644--- a/lib/onebox/templates/githubpullrequest.mustache+++ b/lib/onebox/templates/githubpullrequest.mustache@@ -31,38 +31,32 @@<div class="github-info"><span>- Commit by+ {{i18n.commit_by}}<a href="{{author.html_url}}" target="_blank" rel="noopener"><img alt="{{author.login}}" src="{{author.avatar_url}}" class="onebox-avatar-inline" width="20" height="20">{{author.login}}- </a>- in- <a href="{{link}}" target="_blank" rel="noopener">{{title}}</a>+ </a> - <a href="{{link}}" target="_blank" rel="noopener">{{title}}</a></span></div>{{/commit}}{{#comment}}<h4>- Comment by+ {{i18n.comment_by}}<a href="{{user.html_url}}" target="_blank" rel="noopener"><img alt="{{user.login}}" src="{{user.avatar_url}}" class="onebox-avatar-inline" width="20" height="20">{{user.login}}- </a>- to- <a href="{{link}}" target="_blank" rel="noopener">{{title}}</a>+ </a> - <a href="{{link}}" target="_blank" rel="noopener">{{title}}</a></h4>{{/comment}}{{#discussion}}<h4>- Review by+ {{i18n.review_by}}<a href="{{user.html_url}}" target="_blank" rel="noopener"><img alt="{{user.login}}" src="{{user.avatar_url}}" class="onebox-avatar-inline" width="20" height="20">{{user.login}}- </a>- in- <a href="{{link}}" target="_blank" rel="noopener">{{title}}</a>+ </a> - <a href="{{link}}" target="_blank" rel="noopener">{{title}}</a></h4>{{/discussion}}@@ -79,7 +73,7 @@{{#pr}}<div class="github-info"><div class="date">- opened <span class="discourse-local-date" data-format="ll" data-date="{{created_at_date}}" data-time="{{created_at_time}}" data-timezone="UTC">{{created_at}}</span>+ {{i18n.opened}} <span class="discourse-local-date" data-format="ll" data-date="{{created_at_date}}" data-time="{{created_at_time}}" data-timezone="UTC">{{created_at}}</span></div><div class="user">@@ -89,7 +83,7 @@</a></div>- <div class="lines" title="{{commits}} commits changed {{changed_files}} files with {{additions}} additions and {{deletions}} deletions">+ <div class="lines" title="{{i18n.pr_summary}}"><a href="{{html_url}}/files" target="_blank" rel="noopener"><span class="added">+{{additions}}</span><span class="removed">-{{deletions}}</span>diff --git a/lib/onebox/templates/gitlabblob.mustache b/lib/onebox/templates/gitlabblob.mustacheindex a24ffdfb4c6..9e3390b4a4f 100644--- a/lib/onebox/templates/gitlabblob.mustache+++ b/lib/onebox/templates/gitlabblob.mustache@@ -17,5 +17,5 @@{{/has_lines}}{{#truncated}}- This file has been truncated. <a href="{{link}}" target="_blank" rel="noopener">show original</a>+ {{i18n.truncated_file}} <a href="{{link}}" target="_blank" rel="noopener">{{i18n.show_original}}</a>{{/truncated}}