FEATURE: Add localized timezone names (#7004)

Uses github.com/discourse/moment-timezone-names-translations to translate timezone names.
Plugins can also provide their own timezone name translations.
This commit is contained in:
Penar Musaraj 2019-02-25 14:40:02 -05:00 committed by GitHub
parent 42df20e4f0
commit 9bf11a7c02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 628 additions and 5 deletions

View File

@ -156,18 +156,26 @@ module JsLocaleHelper
result << File.read("#{Rails.root}/vendor/assets/javascripts/moment.js")
result << File.read("#{Rails.root}/vendor/assets/javascripts/moment-timezone-with-data.js")
result << moment_locale(locale_str)
result << moment_locale(locale_str, timezone_names: true)
result << moment_formats
result
end
def self.find_moment_locale(locale_chain)
path = "#{Rails.root}/vendor/assets/javascripts/moment-locale"
def self.find_moment_locale(locale_chain, timezone_names: false)
if timezone_names
path = "#{Rails.root}/vendor/assets/javascripts/moment-timezone-names-locale"
type = :moment_js_timezones
else
path = "#{Rails.root}/vendor/assets/javascripts/moment-locale"
type = :moment_js
end
find_locale(locale_chain, path, :moment_js, fallback_to_english: false) do |locale|
find_locale(locale_chain, path, type, fallback_to_english: false) do |locale|
# moment.js uses a different naming scheme for locale files
locale.tr('_', '-').downcase
end
end
def self.find_message_format_locale(locale_chain, fallback_to_english:)
@ -213,8 +221,8 @@ module JsLocaleHelper
"moment.fn.#{name.camelize(:lower)} = function(){ return this.format('#{format}'); };\n"
end
def self.moment_locale(locale)
_, filename = find_moment_locale([locale])
def self.moment_locale(locale, timezone_names: false)
_, filename = find_moment_locale([locale], timezone_names: timezone_names)
filename && File.exist?(filename) ? File.read(filename) << "\n" : ""
end

View File

@ -638,6 +638,10 @@ class Plugin::Instance
opts[:moment_js] = find_locale_file(locale_chain, path)
opts[:moment_js] = JsLocaleHelper.find_moment_locale(locale_chain) unless opts[:moment_js]
path = File.join(lib_locale_path, "moment_js_timezones")
opts[:moment_js_timezones] = find_locale_file(locale_chain, path)
opts[:moment_js_timezones] = JsLocaleHelper.find_moment_locale(locale_chain, timezone_names: true) unless opts[:moment_js_timezones]
if valid_locale?(opts)
DiscoursePluginRegistry.register_locale(locale, opts)
Rails.configuration.assets.precompile << "locales/#{locale}.js"

View File

@ -73,6 +73,9 @@ task 'javascript:update' do
destination: 'moment-locale',
}, {
source: 'moment-timezone/builds/moment-timezone-with-data.js'
}, {
source: 'moment-timezone-names-translations/locales/.',
destination: 'moment-timezone-names-locale'
}, {
source: 'mousetrap/plugins/global-bind/mousetrap-global-bind.js'
}, {

View File

@ -25,6 +25,7 @@
"markdown-it": "8.4.1",
"moment": "2.22.1",
"moment-timezone": "0.5.14",
"moment-timezone-names-translations": "https://github.com/discourse/moment-timezone-names-translations",
"mousetrap": "https://github.com/discourse/mousetrap#firefox-alt-key",
"pikaday": "1.7.0",
"resumablejs": "1.1.0",

View File

@ -65,6 +65,12 @@ export default Ember.Component.extend({
@computed()
allTimezones() {
if (
moment.locale() !== "en" &&
typeof moment.tz.localizedNames === "function"
) {
return moment.tz.localizedNames();
}
return moment.tz.names();
},

View File

@ -358,6 +358,7 @@ describe Plugin::Instance do
expect(DiscoursePluginRegistry.locales.count).to eq(0)
plugin.activate!
expect(DiscoursePluginRegistry.locales.count).to eq(2)
end
@ -377,12 +378,14 @@ describe Plugin::Instance do
expect(locale[:fallbackLocale]).to be_nil
expect(locale[:message_format]).to eq(["foo_BAR", "#{plugin_path}/lib/javascripts/locale/message_format/foo_BAR.js"])
expect(locale[:moment_js]).to eq(["foo_BAR", "#{plugin_path}/lib/javascripts/locale/moment_js/foo_BAR.js"])
expect(locale[:moment_js_timezones]).to eq(["foo_BAR", "#{plugin_path}/lib/javascripts/locale/moment_js_timezones/foo_BAR.js"])
expect(locale[:plural]).to eq(plural.with_indifferent_access)
expect(Rails.configuration.assets.precompile).to include("locales/foo_BAR.js")
expect(JsLocaleHelper.find_message_format_locale(["foo_BAR"], fallback_to_english: true)).to eq(locale[:message_format])
expect(JsLocaleHelper.find_moment_locale(["foo_BAR"])).to eq (locale[:moment_js])
expect(JsLocaleHelper.find_moment_locale(["foo_BAR"], timezone_names: true)).to eq (locale[:moment_js_timezones])
end
it "correctly registers a new locale using a fallback locale" do
@ -394,6 +397,7 @@ describe Plugin::Instance do
expect(locale[:fallbackLocale]).to eq("es")
expect(locale[:message_format]).to eq(["es", "#{Rails.root}/lib/javascripts/locale/es.js"])
expect(locale[:moment_js]).to eq(["es", "#{Rails.root}/vendor/assets/javascripts/moment-locale/es.js"])
expect(locale[:moment_js_timezones]).to eq(["es", "#{Rails.root}/vendor/assets/javascripts/moment-timezone-names-locale/es.js"])
expect(locale[:plural]).to be_nil
expect(Rails.configuration.assets.precompile).to include("locales/es_MX.js")

View File

@ -0,0 +1 @@
// this file should contain the locale configuration for moment.js timezone names

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1611,6 +1611,10 @@ mkdirp@0.5.1, mkdirp@^0.5.1:
dependencies:
minimist "0.0.8"
"moment-timezone-names-translations@https://github.com/discourse/moment-timezone-names-translations":
version "0.0.0"
resolved "https://github.com/discourse/moment-timezone-names-translations#5f576fc6355d8e636783bfb7b0214a5d74a5399d"
moment-timezone@0.5.14:
version "0.5.14"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.14.tgz#4eb38ff9538b80108ba467a458f3ed4268ccfcb1"