mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 18:52:45 +08:00
FEATURE: Make site texts controller handle pages and locales (#8408)
Some endpoints are returning i18n keys instead of translated messages and with these changes, the site_texts endpoint can help translating those. Pagination part is needed for better wildcard support. For example, looking for 'js.notifications' would set 'has_more' to true, but return only the first 50 messages with no way of fetching the remaining.
This commit is contained in:
parent
455147cabe
commit
57f108899a
|
@ -20,11 +20,15 @@ class Admin::SiteTextsController < Admin::AdminController
|
|||
extras = {}
|
||||
|
||||
query = params[:q] || ""
|
||||
|
||||
locale = params[:locale] || I18n.locale
|
||||
raise Discourse::InvalidParameters.new(:locale) if !I18n.locale_available?(locale)
|
||||
|
||||
if query.blank? && !overridden
|
||||
extras[:recommended] = true
|
||||
results = self.class.preferred_keys.map { |k| record_for(k) }
|
||||
results = I18n.with_locale(locale) { self.class.preferred_keys.map { |k| record_for(k) } }
|
||||
else
|
||||
results = find_translations(query, overridden)
|
||||
results = I18n.with_locale(locale) { find_translations(query, overridden) }
|
||||
|
||||
if results.any?
|
||||
extras[:regex] = I18n::Backend::DiscourseI18n.create_search_regexp(query, as_string: true)
|
||||
|
@ -41,8 +45,15 @@ class Admin::SiteTextsController < Admin::AdminController
|
|||
end
|
||||
end
|
||||
|
||||
extras[:has_more] = true if results.size > 50
|
||||
render_serialized(results[0..49], SiteTextSerializer, root: 'site_texts', rest_serializer: true, extras: extras, overridden_keys: overridden_keys)
|
||||
page = params[:page].to_i
|
||||
raise Discourse::InvalidParameters.new(:page) if page < 0
|
||||
|
||||
per_page = 50
|
||||
first = page * per_page
|
||||
last = first + per_page
|
||||
|
||||
extras[:has_more] = true if results.size > last
|
||||
render_serialized(results[first..last - 1], SiteTextSerializer, root: 'site_texts', rest_serializer: true, extras: extras, overridden_keys: overridden_keys)
|
||||
end
|
||||
|
||||
def show
|
||||
|
|
|
@ -58,6 +58,33 @@ RSpec.describe Admin::SiteTextsController do
|
|||
expect(JSON.parse(response.body)['extras']['has_more']).to be_truthy
|
||||
end
|
||||
|
||||
it 'works with pages' do
|
||||
texts = Set.new
|
||||
|
||||
get "/admin/customize/site_texts.json", params: { q: 'e' }
|
||||
JSON.parse(response.body)['site_texts'].each { |text| texts << text['id'] }
|
||||
expect(texts.size).to eq(50)
|
||||
|
||||
get "/admin/customize/site_texts.json", params: { q: 'e', page: 1 }
|
||||
JSON.parse(response.body)['site_texts'].each { |text| texts << text['id'] }
|
||||
expect(texts.size).to eq(100)
|
||||
end
|
||||
|
||||
it 'works with locales' do
|
||||
get "/admin/customize/site_texts.json", params: { q: 'yes_value', locale: 'en' }
|
||||
value = JSON.parse(response.body)['site_texts'].find { |text| text['id'] == 'js.yes_value' }['value']
|
||||
expect(value).to eq(I18n.with_locale(:en) { I18n.t('js.yes_value') })
|
||||
|
||||
get "/admin/customize/site_texts.json", params: { q: 'yes_value', locale: 'de' }
|
||||
value = JSON.parse(response.body)['site_texts'].find { |text| text['id'] == 'js.yes_value' }['value']
|
||||
expect(value).to eq(I18n.with_locale(:de) { I18n.t('js.yes_value') })
|
||||
end
|
||||
|
||||
it 'returns an error on invalid locale' do
|
||||
get "/admin/customize/site_texts.json", params: { locale: '?' }
|
||||
expect(response.status).to eq(400)
|
||||
end
|
||||
|
||||
it 'normalizes quotes during search' do
|
||||
value = %q|“That’s a ‘magic’ sock.”|
|
||||
put "/admin/customize/site_texts/title.json", params: { site_text: { value: value } }
|
||||
|
|
Loading…
Reference in New Issue
Block a user