Skip to content

Commit 14916ec

Browse files
committed
allow tagging custom locales
fixes CNVS-28111 test plan: * edit config/locales.yml, and add `custom: true` under one of the locales (such as japanese: ja: locales: ja: Custom Japanese! custom: true * load up Canvas (be sure you're using RAILS_LOAD_ALL_LOCALES=1), and Japanese should no longer show up in the account or users pages to select * go to /plugins/i18n, and enable the plugin, and check the custom locale (it will be the only one listed), and save * the "custom" locale should show up in dropdowns again Change-Id: Iae6c923b50262b27a25487a0d8458b2627809630 Reviewed-on: https://gerrit.instructure.com/74795 Tested-by: Jenkins Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: August Thornton <august@instructure.com> Product-Review: Cody Cutrer <cody@instructure.com>
1 parent 60b3d5e commit 14916ec

5 files changed

Lines changed: 72 additions & 3 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<%= fields_for :settings, OpenObject.new(settings) do |f| %>
2+
<table style="width: 500px;" class="formtable">
3+
<% LocaleSelection.custom_locales.each do |locale| %>
4+
<tr>
5+
<td><%= f.blabel locale, I18n.send(:t, :locales, :locale => locale)[locale] %></td>
6+
<td><%= f.check_box locale, :enabled => settings[locale] %></td>
7+
</tr>
8+
<% end %>
9+
</table>
10+
<% end %>

lib/canvas/plugins/default_plugins.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@
244244
:success_timeout => "600",
245245
:format_type => "instructure_csv" }
246246
})
247+
Canvas::Plugin.register('i18n', nil, {
248+
:name => lambda{ t :name, 'I18n' },
249+
:description => lambda{ t :description, 'Custom Locales' },
250+
:website => 'https://www.instructure.com',
251+
:author => 'Instructure',
252+
:author_website => 'http://www.instructure.com',
253+
:version => '1.0.0',
254+
:settings_partial => 'plugins/i18n_settings',
255+
:validator => 'I18nValidator'
256+
})
247257
Canvas::Plugin.register('sis_import', :sis, {
248258
:name => lambda{ t :name, 'SIS Import' },
249259
:description => lambda{ t :description, 'Import SIS Data' },
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Copyright (C) 2016 Instructure, Inc.
3+
#
4+
# This file is part of Canvas.
5+
#
6+
# Canvas is free software: you can redistribute it and/or modify it under
7+
# the terms of the GNU Affero General Public License as published by the Free
8+
# Software Foundation, version 3 of the License.
9+
#
10+
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
11+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12+
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13+
# details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License along
16+
# with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#
18+
19+
module Canvas::Plugins::Validators::I18nValidator
20+
def self.validate(settings, plugin_setting)
21+
settings.inject({}) do |result, (i18n, enabled)|
22+
result[i18n] = true if Canvas::Plugin::value_to_boolean(enabled)
23+
result
24+
end
25+
end
26+
end

lib/locale_selection.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,20 @@ def infer_browser_locale(accept_language, supported_locales)
8686
# if the locale name is not yet translated, it won't be included (even if
8787
# there are other translations for that locale)
8888
def available_locales
89-
I18n.available_locales.inject({}) do |hash, locale|
89+
result = {}
90+
settings = Canvas::Plugin.find(:i18n).settings || {}
91+
enabled_custom_locales = settings.select { |locale, enabled| enabled }.map(&:first).map(&:to_sym)
92+
I18n.available_locales.each do |locale|
9093
name = I18n.send(:t, :locales, :locale => locale)[locale]
91-
hash[locale.to_s] = name if name
92-
hash
94+
custom = I18n.send(:t, :custom, locale: locale) == true
95+
next if custom && !enabled_custom_locales.include?(locale)
96+
result[locale.to_s] = name if name
9397
end
98+
result
99+
end
100+
101+
def self.custom_locales
102+
@custom_locales ||= I18n.available_locales.select{ |locale| I18n.send(:t, :custom, :locale => locale) == true }.sort
94103
end
95104

96105
def crowdsourced_locales

spec/lib/locale_selection_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,18 @@ def ls
185185
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :context => @account, :user => @user, :session_locale => 'zh')).to eql('de')
186186
end
187187
end
188+
189+
describe "available_locales" do
190+
it "does not include custom locales" do
191+
I18n.stubs(:available_locales).returns([:en, :ja])
192+
I18n.stubs(:t).with(:locales, locale: :en).returns(en: 'English')
193+
I18n.stubs(:t).with(:custom, locale: :en).returns(nil)
194+
I18n.stubs(:t).with(:locales, locale: :ja).returns(ja: 'Japanese')
195+
I18n.stubs(:t).with(:custom, locale: :ja).returns(true)
196+
expect(ls.available_locales).to eq('en' => 'English')
197+
198+
PluginSetting.create!(name: 'i18n', settings: { 'ja' => true })
199+
expect(ls.available_locales).to eq('en' => 'English', 'ja' => 'Japanese')
200+
end
201+
end
188202
end

0 commit comments

Comments
 (0)