Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Set moment locale globally (#9040)

Benjamin Bädorf64143001fc11
diff --git a/app/controllers/admin/settings/display_settings_controller.rb b/app/controllers/admin/settings/display_settings_controller.rb
index f09a91cba76..01f41911ec4 100644
--- a/app/controllers/admin/settings/display_settings_controller.rb
+++ b/app/controllers/admin/settings/display_settings_controller.rb
@@ -32,6 +32,8 @@ module Admin::Settings
class DisplaySettingsController < ::Admin::SettingsController
menu_item :settings_display
+ before_action :validate_start_of_week_year, only: :update
+
def show
@options = {}
@options[:user_format] = User::USER_FORMATS_STRUCTURE.keys.map { |f| [User.current.name(f), f.to_s] }
@@ -42,5 +44,17 @@ module Admin::Settings
def default_breadcrumb
t(:label_display)
end
+
+ private
+
+ def validate_start_of_week_year
+ start_of_week = params[:settings][:start_of_week]
+ start_of_year = params[:settings][:first_week_of_year]
+
+ if start_of_week.present? ^ start_of_year.present?
+ flash[:error] = I18n.t('settings.display.first_date_of_week_and_year_present')
+ redirect_to action: :show
+ end
+ end
end
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 8650b6e611b..45d1cd156dc 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -428,9 +428,21 @@ module ApplicationHelper
when 6
'6' # Saturday
else
- # use language (pass a blank string into the JSON object,
- # as the datepicker implementation checks for numbers in
- # /frontend/app/misc/datepicker-defaults.js:34)
+ # use language default (pass a blank string) and moment.js will reuse existing info
+ # /frontend/src/main.ts
+ ''
+ end
+ end
+
+ def locale_first_week_of_year
+ case Setting.first_week_of_year.to_i
+ when 1
+ '1' # Monday
+ when 4
+ '4' # Thursday
+ else
+ # use language default (pass a blank string) and moment.js will reuse existing info
+ # /frontend/src/main.ts
''
end
end
diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb
index 0da641f811e..5a5ee633c4b 100644
--- a/app/helpers/settings_helper.rb
+++ b/app/helpers/settings_helper.rb
@@ -83,10 +83,12 @@ module SettingsHelper
choices.map do |choice|
text, value, choice_options = (choice.is_a?(Array) ? choice : [choice, choice])
choice_options = (choice_options || {}).merge(options.except(:id))
+ choice_options[:id] = "#{setting}_#{value}"
content_tag(:label, class: 'form--label-with-check-box') do
styled_check_box_tag("settings[#{setting}][]", value,
Setting.send(setting).include?(value), choice_options) + text.to_s
+
end
end.join.html_safe
end
@@ -100,6 +102,18 @@ module SettingsHelper
end
def setting_text_field(setting, options = {})
+ setting_field_wrapper(setting, options) do
+ styled_text_field_tag("settings[#{setting}]", Setting.send(setting), options)
+ end
+ end
+
+ def setting_number_field(setting, options = {})
+ setting_field_wrapper(setting, options) do
+ styled_number_field_tag("settings[#{setting}]", Setting.send(setting), options)
+ end
+ end
+
+ def setting_field_wrapper(setting, options)
unit = options.delete(:unit)
unit_html = ''
@@ -115,8 +129,7 @@ module SettingsHelper
setting_label(setting, options) +
wrap_field_outer(options) do
- styled_text_field_tag("settings[#{setting}]", Setting.send(setting), options) +
- unit_html
+ yield + unit_html
end
end
diff --git a/app/helpers/static_links_helper.rb b/app/helpers/static_links_helper.rb
index 6b0e718def6..8396795af1b 100644
--- a/app/helpers/static_links_helper.rb
+++ b/app/helpers/static_links_helper.rb
@@ -31,8 +31,12 @@
module StaticLinksHelper
##
# Create a static link to the given key entry
- def static_link_to(key)
+ def static_link_to(key, label: nil)
item = OpenProject::Static::Links.links.fetch key
- link_to t(item[:label]), item[:href], class: 'openproject--static-link', target: '_blank'
+
+ link_to label || t(item[:label]),
+ item[:href],
+ class: 'openproject--static-link',
+ target: '_blank'
end
end
diff --git a/app/views/admin/settings/display_settings/show.html.erb b/app/views/admin/settings/display_settings/show.html.erb
index 423d91e8d39..4f2fd124413 100644
--- a/app/views/admin/settings/display_settings/show.html.erb
+++ b/app/views/admin/settings/display_settings/show.html.erb
@@ -36,17 +36,37 @@ See docs/COPYRIGHT.rdoc for more details.
<%= styled_form_tag(admin_settings_update_display_path, method: :patch) do %>
<section class="form--section">
- <div class="form--field" id="setting_available_languages"><%= setting_multiselect :available_languages, all_lang_options_for_select(false) %></div>
+ <div class="form--field" id="setting_available_languages">
+ <%= setting_multiselect :available_languages, all_lang_options_for_select(false) %>
+ </div>
- <div class="form--field"><%= setting_select :start_of_week, [[day_name(1),'1'], [day_name(6),'6'], [day_name(7),'7']], blank: :label_language_based, container_class: '-slim' %></div>
+ <div class="form--field">
+ <%= setting_select :start_of_week, [[day_name(1),'1'], [day_name(6),'6'], [day_name(7),'7']], blank: :label_language_based, container_class: '-slim' %>
+ </div>
- <div class="form--field"><%= setting_select :date_format, Setting::DATE_FORMATS.collect {|f| [Date.today.strftime(f), f]}, blank: :label_language_based, container_class: '-slim' %></div>
+ <div class="form--field">
+ <%= setting_select :first_week_of_year, [[day_name(1),'1'], [day_name(4),'4']], blank: :label_language_based, container_class: '-slim' %>
+ <span class="form--field-instructions">
+ <%= t('settings.display.first_week_of_year_text') %>
+ <br/>
+ <%= static_link_to :display_settings_documentation, label: t('documentation.see_more_link') %>
+ </span>
+ </div>
- <div class="form--field"><%= setting_select :time_format, Setting::TIME_FORMATS.collect {|f| [Time.now.strftime(f), f]}, blank: :label_language_based, container_class: '-slim' %></div>
+ <div class="form--field">
+ <%= setting_select :date_format, Setting::DATE_FORMATS.collect {|f| [Date.today.strftime(f), f]}, blank: :label_language_based, container_class: '-slim' %>
+ </div>
+
+ <div class="form--field">
+ <%= setting_select :time_format, Setting::TIME_FORMATS.collect {|f| [Time.now.strftime(f), f]}, blank: :label_language_based, container_class: '-slim' %>
+ </div>
- <div class="form--field"><%= setting_select :user_format, @options[:user_format], container_class: '-slim' %></div>
+ <div class="form--field">
+ <%= setting_select :user_format, @options[:user_format], container_class: '-slim' %>
+ </div>
- <div class="form--field"><%= setting_text_field :journal_aggregation_time_minutes, unit: t(:label_minute_plural), container_class: '-xslim' %>
+ <div class="form--field">
+ <%= setting_text_field :journal_aggregation_time_minutes, unit: t(:label_minute_plural), container_class: '-xslim' %>
<span class="form--field-instructions">
<%= t(:text_journal_aggregation_time_explanation) %><br/>
<%= t(:text_hint_disable_with_0) %>
diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb
index 5ec665ba232..626486472af 100644
--- a/app/views/layouts/base.html.erb
+++ b/app/views/layouts/base.html.erb
@@ -64,6 +64,7 @@ See docs/COPYRIGHT.rdoc for more details.
<meta name="openproject_initializer"
data-locale="<%= I18n.locale %>"
+ data-first-week-of-year="<%= locale_first_week_of_year %>"
data-first-day-of-week="<%= locale_first_day_of_week %>"
data-default-locale="<%= I18n.default_locale %>"
data-environment="<%= Rails.env %>"
diff --git a/config/locales/en.yml b/config/locales/en.yml
index a21b98ce971..ae7e5277f07 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -198,6 +198,9 @@ en:
concatenation:
single: 'or'
+ documentation:
+ see_more_link: For more information, please see our documentation on this topic.
+
global_search:
overwritten_tabs:
wiki_pages: "Wiki"
@@ -2275,6 +2278,7 @@ en:
setting_consent_required: "Consent required"
setting_consent_decline_mail: "Consent contact mail address"
setting_cross_project_work_package_relations: "Allow cross-project work package relations"
+ setting_first_week_of_year: "First week in year contains"
setting_date_format: "Date format"
setting_default_language: "Default language"
setting_default_notification_option: "Default notification option"
@@ -2357,6 +2361,13 @@ en:
passwords: "Passwords"
session: "Session"
brute_force_prevention: "Automated user blocking"
+ display:
+ first_date_of_week_and_year_present: >
+ If either options "First date of week" or "First week of year contains" are set,
+ the other has to be set as well to avoid inconsistencies in the frontend.
+ first_week_of_year_text: >
+ Select the date of January that is contained in the first week of the year.
+ This value together with first day of the week determines the total number of weeks in a year.
projects:
section_new_projects: "Settings for new projects"
section_project_overview: "Settings for project overview list"
diff --git a/config/settings.yml b/config/settings.yml
index c8a55155f20..b50203b6dbf 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -317,6 +317,8 @@ emails_footer:
To change your notification preferences, please click here: http://hostname/my/account
start_of_week:
default: ''
+first_week_of_year:
+ default: ''
rest_api_enabled:
default: 1
session_ttl_enabled:
diff --git a/docs/system-admin-guide/system-settings/display-settings/README.md b/docs/system-admin-guide/system-settings/display-settings/README.md
index fa09e43d774..2c4bddb29bb 100644
--- a/docs/system-admin-guide/system-settings/display-settings/README.md
+++ b/docs/system-admin-guide/system-settings/display-settings/README.md
@@ -28,11 +28,17 @@ You can [choose your language in your user profile](../../../getting-started/my-
Change time and date formats in OpenProject and configure the display of journal aggregation.
-1. Configure when the **week starts** (e.g. in the calendar view). Default is based on user's language. You can also choose to start a week always on Monday, Sunday or Saturdays.
-2. Set the **date format**, default is based on user's language. You can choose various formats to display dates in the system.
-3. Set the **time format**, default is based on user's language. You can choose various formats to display time in the system.
-4. Choose the **users display format**, default is Firstname Lastname. You can change to various different formats.
-5. **Display journals as aggregated within** will enable you to choose a time to combine journals of this timespan, e.g. for work package notifications. This way if you do several updates at in a short period of time, you will not receive a notification for each update but only a notification for the chosen period.
-6. Do not forget to **save** your changes.
-
-![Sys-admin-display-settings](Sys-admin-display-settings.png)
+- **Week starts on**: Configure what date the week starts (e.g. in the calendar view). Default is based on user's language. You can also choose to start a week always on Monday, Sunday or Saturdays.
+ If you set Week starts on, you must also set the First week in year or will probably get inconsistencies in week numbering in calendars and the gantt chart.
+- **First week in year contains**: Select the day that needs to be contained in the first week of the year. This value is used together with _Week starts on_ to determine the first week of the year.
+ - For europoean ISO-8601 standard, set Week starts on to `Monday` and First week in year to `Thursday`.
+ - For US and Canda, set Weekt starts on to `Sunday` and Day of Year to `6`.
+ - If you leave both on `Based on user's language`, the information provided by moment.js will be used: For more information, please also see [this document from moment.js](https://momentjscom.readthedocs.io/en/latest/moment/07-customization/16-dow-doy/).
+ - For more information on the way the first week of the year is computed according to ISO standards, see https://en.wikipedia.org/wiki/ISO_week_date#First_week
+- **Date format**: default is based on user's language. You can choose various formats to display dates in the system.
+- **Time format**: default is based on user's language. You can choose various formats to display time in the system.
+- **Users display format**: default is Firstname Lastname. You can change to various different formats.
+- **Display journals as aggregated within** will enable you to choose a time to combine journals of this timespan, e.g. for work package notifications. This way if you do several updates at in a short period of time, you will not receive a notification for each update but only a notification for the chosen period.
+- Do not forget to **save** your changes.
+
+![Sys-admin-display-settings](time-and-date-settings.png)
diff --git a/docs/system-admin-guide/system-settings/display-settings/Sys-admin-display-settings.png b/docs/system-admin-guide/system-settings/display-settings/Sys-admin-display-settings.png
deleted file mode 100644
index 6763390b5db..00000000000
Binary files a/docs/system-admin-guide/system-settings/display-settings/Sys-admin-display-settings.png and /dev/null differ
diff --git a/docs/system-admin-guide/system-settings/display-settings/time-and-date-settings.png b/docs/system-admin-guide/system-settings/display-settings/time-and-date-settings.png
new file mode 100644
index 00000000000..525ff7a2fe8
Binary files /dev/null and b/docs/system-admin-guide/system-settings/display-settings/time-and-date-settings.png differ
diff --git a/frontend/src/app/init-locale.ts b/frontend/src/app/init-locale.ts
index 66155d19c65..c56d3d1cfe4 100644
--- a/frontend/src/app/init-locale.ts
+++ b/frontend/src/app/init-locale.ts
@@ -26,11 +26,25 @@
// See docs/COPYRIGHT.rdoc for more details.
//++
+import * as moment from "moment";
export function initializeLocale() {
const meta = document.querySelector('meta[name=openproject_initializer]') as HTMLMetaElement;
- I18n.locale = meta.dataset.locale || 'en';
- I18n.firstDayOfWeek = parseInt(meta.dataset.firstDayOfWeek!, 10);
+ const locale = meta.dataset.locale || 'en';
+ const firstDayOfWeek = parseInt(meta.dataset.firstDayOfWeek || '', 10);
+ const firstWeekOfYear = parseInt(meta.dataset.firstWeekOfYear || '', 10);
+
+ I18n.locale = locale;
+ I18n.firstDayOfWeek = firstDayOfWeek;
+
+ if (!isNaN(firstDayOfWeek) && !isNaN(firstWeekOfYear)) {
+ moment.updateLocale(locale, {
+ week: {
+ dow: firstDayOfWeek,
+ doy: 7 + firstDayOfWeek - firstWeekOfYear
+ }
+ });
+ }
// Override the default pluralization function to allow
// "other" to be used as a fallback for "one" in languages where one is not set
diff --git a/frontend/src/main.ts b/frontend/src/main.ts
index 1bc55151435..a94092dab17 100644
--- a/frontend/src/main.ts
+++ b/frontend/src/main.ts
@@ -1,12 +1,12 @@
-import {OpenProjectModule} from 'core-app/angular4-modules';
-import {enableProdMode} from '@angular/core';
+import { OpenProjectModule } from 'core-app/angular4-modules';
+import { enableProdMode } from '@angular/core';
import * as jQuery from "jquery";
-import {environment} from './environments/environment';
-import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
-import {SentryReporter} from "core-app/sentry/sentry-reporter";
-import {whenDebugging} from "core-app/helpers/debug_output";
-import {enableReactiveStatesLogging} from "reactivestates";
-import {initializeLocale} from "core-app/init-locale";
+import { environment } from './environments/environment';
+import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
+import { SentryReporter } from "core-app/sentry/sentry-reporter";
+import { whenDebugging } from "core-app/helpers/debug_output";
+import { enableReactiveStatesLogging } from "reactivestates";
+import { initializeLocale } from "core-app/init-locale";
(window as any).global = window;
diff --git a/lib/open_project/static/links.rb b/lib/open_project/static/links.rb
index f563a230242..0351b1706b9 100644
--- a/lib/open_project/static/links.rb
+++ b/lib/open_project/static/links.rb
@@ -191,6 +191,9 @@ module OpenProject
security_badge_documentation: {
href: 'https://docs.openproject.org/system-admin-guide/information/#security-badge'
},
+ display_settings_documentation: {
+ href: 'https://docs.openproject.org/system-admin-guide/system-settings/display-settings/'
+ },
chargebee: {
href: 'https://js.chargebee.com/v2/chargebee.js'
},
diff --git a/spec/features/work_packages/timeline/timeline_dates_spec.rb b/spec/features/work_packages/timeline/timeline_dates_spec.rb
new file mode 100644
index 00000000000..8db706fbc8b
--- /dev/null
+++ b/spec/features/work_packages/timeline/timeline_dates_spec.rb
@@ -0,0 +1,122 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+RSpec.feature 'Work package timeline date formatting',
+ with_settings: { date_format: '%Y-%m-%d' },
+ js: true,
+ selenium: true do
+ shared_let(:type) { FactoryBot.create(:type_bug) }
+ shared_let(:project) { FactoryBot.create(:project, types: [type]) }
+
+ shared_let(:work_package) do
+ FactoryBot.create :work_package,
+ project: project,
+ type: type,
+ start_date: '2020-12-31',
+ due_date: '2021-01-01',
+ subject: 'My subject'
+ end
+
+ let(:wp_timeline) { Pages::WorkPackagesTimeline.new(project) }
+ let!(:query_tl) do
+ query = FactoryBot.build(:query, user: current_user, project: project)
+ query.column_names = ['id', 'type', 'subject']
+ query.filters.clear
+ query.timeline_visible = true
+ query.timeline_zoom_level = 'days'
+ query.name = 'Query with Timeline'
+
+ query.save!
+ query
+ end
+
+ def expect_date_week(date, expected_week)
+ week = page.evaluate_script <<~JS
+ moment('#{date}').format('ww');
+ JS
+
+ expect(week).to eq(expected_week)
+ end
+
+ before do
+ login_as current_user
+
+ wp_timeline.visit_query query_tl
+ end
+
+ describe 'with default settings',
+ with_settings: { start_of_week: '', first_week_of_year: '' } do
+
+ context 'with english locale user' do
+ let(:current_user) { FactoryBot.create :admin, language: 'en' }
+
+ it 'shows english ISO dates' do
+ # expect moment to return week 01 for start date
+ expect_date_week work_package.start_date.iso8601, '01'
+ expect_date_week work_package.due_date.iso8601, '01'
+ # Monday, 4th of january is the second week
+ expect_date_week '2021-01-04', '02'
+ end
+ end
+
+ context 'with german locale user' do
+ let(:current_user) { FactoryBot.create :admin, language: 'de' }
+
+ it 'shows german ISO dates' do
+ expect(page).to have_selector('.wp-timeline--header-element', text: '52')
+ expect(page).to have_selector('.wp-timeline--header-element', text: '53')
+
+ # expect moment to return week 53 for start date
+ expect_date_week work_package.start_date.iso8601, '53'
+ expect_date_week work_package.due_date.iso8601, '53'
+ # Monday, 4th of january is the first week
+ expect_date_week '2021-01-04', '01'
+ end
+ end
+ end
+
+ describe 'with US/CA settings',
+ with_settings: { start_of_week: '7', first_week_of_year: '1' } do
+
+ let(:current_user) { FactoryBot.create :admin }
+
+ it 'shows english ISO dates' do
+ expect(page).to have_selector('.wp-timeline--header-element', text: '01')
+ expect(page).to have_selector('.wp-timeline--header-element', text: '02')
+ expect(page).to have_no_selector('.wp-timeline--header-element', text: '53')
+
+ # expect moment to return week 01 for start date and due date
+ expect_date_week work_package.start_date.iso8601, '01'
+ expect_date_week work_package.due_date.iso8601, '01'
+ # First sunday in january is in second week
+ expect_date_week '2021-01-03', '02'
+ end
+ end
+end