decidim-voca 0.0.6 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE.md +161 -161
- data/README.md +42 -133
- data/Rakefile +46 -39
- data/app/cells/decidim/footer_topics/show_overrides.erb +8 -0
- data/app/cells/decidim/meetings/dates_and_map/show_overrides.erb +23 -0
- data/app/commands/decidim/voca/admin/code_census/save_code_list.rb +34 -0
- data/app/commands/decidim/voca/code_census/confirm_code_authorization.rb +16 -0
- data/app/commands/decidim/voca/create_editor_file.rb +36 -0
- data/app/commands/decidim/voca/rack_attack_configurator.rb +244 -0
- data/app/commands/decidim/voca/sync_redis_routing.rb +120 -0
- data/app/commands/decidim/voca/user_fields_configurator.rb +40 -0
- data/app/controllers/decidim/voca/admin/application_controller.rb +4 -6
- data/app/controllers/decidim/voca/admin/code_census/code_lists_controller.rb +40 -0
- data/app/controllers/decidim/voca/application_controller.rb +4 -6
- data/app/controllers/decidim/voca/code_census/authorizations_controller.rb +53 -0
- data/app/controllers/decidim/voca/editor_files_controller.rb +47 -0
- data/app/controllers/decidim/voca/geolocation_controller.rb +32 -0
- data/app/forms/decidim/voca/admin/code_census/code_list_form.rb +21 -0
- data/app/forms/decidim/voca/admin/organization_config_form.rb +24 -0
- data/app/forms/decidim/voca/code_census/code_form.rb +41 -0
- data/app/forms/decidim/voca/editor_file_form.rb +18 -0
- data/app/jobs/decidim/voca/machine_translate_awesome_menu_label_job.rb +63 -0
- data/app/jobs/decidim/voca/machine_translate_component_setting_job.rb +58 -0
- data/app/jobs/decidim/voca/machine_translate_content_block_setting_job.rb +85 -0
- data/app/models/decidim/voca/conversation_uuid.rb +18 -0
- data/app/models/decidim/voca/editor_file.rb +18 -0
- data/app/models/decidim/voca/validation_code.rb +13 -0
- data/app/overrides/admin_add_machine_translation_flag.rb +36 -0
- data/app/overrides/insert_assets.rb +26 -0
- data/app/overrides/insert_weglot_scripts.rb +36 -0
- data/app/overrides/proposal_camera.rb +18 -0
- data/app/overrides/show_votes.rb +27 -0
- data/app/overrides/voca_footer.rb +52 -0
- data/app/packs/entrypoints/admin-decidim-voca.js +2 -0
- data/app/packs/entrypoints/admin-decidim-voca.scss +1 -0
- data/app/packs/entrypoints/decidim-voca-fixes.scss +1 -0
- data/app/packs/entrypoints/decidim-voca.js +1 -0
- data/app/packs/entrypoints/decidim-voca.scss +1 -0
- data/app/packs/images/voca-logo-primary-x3.png +0 -0
- data/app/packs/src/decidim/direct_uploads/upload_field.js +150 -0
- data/app/packs/src/decidim/voca/admin/voca.js +16 -0
- data/app/packs/src/decidim/voca/proposal_better_ux/use_camera_button.js +39 -0
- data/app/packs/src/decidim/voca/proposal_better_ux/utils/get_current_position.js +21 -0
- data/app/packs/src/decidim/voca/proposal_better_ux/utils/get_proposal_form.js +13 -0
- data/app/packs/src/decidim/voca/proposal_better_ux/utils/loading.js +23 -0
- data/app/packs/src/decidim/voca/proposal_better_ux/utils/locate_coordinates.js +23 -0
- data/app/packs/src/decidim/voca/proposal_custom_fields/custom_field_renderer_override.js +152 -0
- data/app/packs/src/decidim/voca/proposal_custom_fields/editor_budget_field.js +329 -0
- data/app/packs/src/decidim/voca/proposal_custom_fields/editor_upload_field.js +85 -0
- data/app/packs/src/decidim/voca/proposal_custom_fields/proposal_custom_fields.js +96 -0
- data/app/packs/src/decidim/voca/proposal_custom_fields/render_budget_field.js +76 -0
- data/app/packs/src/decidim/voca/proposal_fields/render_budget_field.js +76 -0
- data/app/packs/src/decidim/voca/questionnaire/scroll_questionnaire_steps_to_top.js +18 -0
- data/app/packs/src/decidim/voca/voca.js +4 -0
- data/app/packs/src/decidim/voca/weglot/index.js +13 -0
- data/app/packs/src/decidim/voca/weglot/start_weglot.js +112 -0
- data/app/packs/src/decidim/voca/weglot/switcher.js +92 -0
- data/app/packs/src/decidim/voca/weglot/wait_for_weglot.js +10 -0
- data/app/packs/stylesheets/decidim/voca/_custom_proposal_fields.scss +111 -0
- data/app/packs/stylesheets/decidim/voca/_decidim_voca.scss +5 -0
- data/app/packs/stylesheets/decidim/voca/_decidim_voca_fixes.scss +25 -0
- data/app/packs/stylesheets/decidim/voca/_primary_footer.scss +77 -0
- data/app/packs/stylesheets/decidim/voca/_proposal_camera.scss +14 -0
- data/app/packs/stylesheets/decidim/voca/_weglot.scss +61 -0
- data/app/packs/stylesheets/decidim/voca/admin/_decidim_voca.scss +5 -0
- data/app/packs/stylesheets/decidim/voca/admin/_full_machine_translation.scss +34 -0
- data/app/permissions/decidim/voca/admin/permissions.rb +31 -0
- data/app/permissions/decidim/voca/permissions.rb +17 -0
- data/app/views/decidim/voca/admin/code_census/authorizations/new.html.erb +33 -0
- data/app/views/decidim/voca/admin/code_census/code_lists/edit.html.erb +27 -0
- data/app/views/decidim/voca/code_census/authorizations/new.html.erb +24 -0
- data/app/views/decidim/voca/deepl/overrides/_admin_machine_translation_alert.html.erb +14 -0
- data/app/views/decidim/voca/rack_attack/_blocked.html.erb +151 -0
- data/app/views/decidim/voca/weglot/_js_configuration.html.erb +14 -0
- data/app/views/decidim/voca/weglot/_locale_switcher.html.erb +14 -0
- data/app/views/decidim/voca/weglot/_locale_switcher_mobile.erb +14 -0
- data/app/views/decidim/voca/weglot/_topbar_locale_switcher.html.erb +14 -0
- data/config/assets.rb +12 -0
- data/config/i18n-tasks.yml +14 -5
- data/config/locales/cs.yml +94 -0
- data/config/locales/custom_fields.cs.yml +30 -0
- data/config/locales/custom_fields.en.yml +41 -0
- data/config/locales/custom_fields.fr.yml +30 -0
- data/config/locales/custom_fields.it.yml +30 -0
- data/config/locales/en.yml +97 -4
- data/config/locales/fr.yml +102 -0
- data/config/locales/it.yml +94 -0
- data/lib/decidim/voca/awesome_menu_label_machine_translation.rb +73 -0
- data/lib/decidim/voca/awesome_menu_labels.rb +28 -0
- data/lib/decidim/voca/cache_namespace.rb +21 -0
- data/lib/decidim/voca/code_census/admin_engine.rb +22 -0
- data/lib/decidim/voca/code_census/engine.rb +29 -0
- data/lib/decidim/voca/code_census/workflow.rb +7 -0
- data/lib/decidim/voca/code_census.rb +4 -0
- data/lib/decidim/voca/component_setting_manifest.rb +27 -0
- data/lib/decidim/voca/component_setting_pending_locales.rb +33 -0
- data/lib/decidim/voca/component_translated_settings_machine_translation.rb +86 -0
- data/lib/decidim/voca/configuration.rb +23 -0
- data/lib/decidim/voca/content_block_setting_manifest.rb +67 -0
- data/lib/decidim/voca/content_block_translated_settings_machine_translation.rb +100 -0
- data/lib/decidim/voca/deepl/deepl_active_job_context.rb +27 -0
- data/lib/decidim/voca/deepl/deepl_context.rb +78 -0
- data/lib/decidim/voca/deepl/deepl_form_builder_overrides.rb +82 -0
- data/lib/decidim/voca/deepl/deepl_machine_translator.rb +61 -0
- data/lib/decidim/voca/deepl/deepl_middleware.rb +83 -0
- data/lib/decidim/voca/deepl/engine_config.rb +179 -0
- data/lib/decidim/voca/deepl/translation_bar_overrides.rb +20 -0
- data/lib/decidim/voca/deepl.rb +9 -0
- data/lib/decidim/voca/engine.rb +318 -11
- data/lib/decidim/voca/export/comment_serializer_localized_csv.rb +34 -0
- data/lib/decidim/voca/export/csv_with_locale_transformer.rb +53 -0
- data/lib/decidim/voca/export/proposal_serializer_localized_csv.rb +45 -0
- data/lib/decidim/voca/export/user_answers_serializer_localized_csv.rb +54 -0
- data/lib/decidim/voca/good_job_settings.rb +74 -0
- data/lib/decidim/voca/installation.rb +33 -0
- data/lib/decidim/voca/machine_translation/translate_string.rb +121 -0
- data/lib/decidim/voca/machine_translation_human_source.rb +29 -0
- data/lib/decidim/voca/machine_translation_resource_job_voca.rb +65 -0
- data/lib/decidim/voca/organization_voca_extensions.rb +31 -0
- data/lib/decidim/voca/overrides/csv_export_serializers.rb +51 -0
- data/lib/decidim/voca/overrides/meetings/dates_and_map_cell_overrides.rb +20 -0
- data/lib/decidim/voca/overrides/menu_breadcrumb_last_activity_cache.rb +31 -0
- data/lib/decidim/voca/overrides/mod_secure/conversation_controller_overrides.rb +19 -0
- data/lib/decidim/voca/overrides/mod_secure/conversation_sanitize.rb +29 -0
- data/lib/decidim/voca/overrides/mod_secure/conversation_uuid.rb +34 -0
- data/lib/decidim/voca/overrides/mod_secure/user_conversations_redirect_overrides.rb +53 -0
- data/lib/decidim/voca/overrides/mod_secure/user_profile_verification_override.rb +31 -0
- data/lib/decidim/voca/overrides/newsletter_helper_overrides.rb +30 -0
- data/lib/decidim/voca/overrides/resource_presenter_overrides.rb +30 -0
- data/lib/decidim/voca/overrides/sanitize_helper_overrides.rb +39 -0
- data/lib/decidim/voca/sync_locales/README.md +13 -0
- data/lib/decidim/voca/sync_locales/awesome_menu_label_sync.rb +89 -0
- data/lib/decidim/voca/sync_locales/command.rb +54 -0
- data/lib/decidim/voca/sync_locales/component_setting_sync.rb +72 -0
- data/lib/decidim/voca/sync_locales/content_block_setting_sync.rb +86 -0
- data/lib/decidim/voca/sync_locales/field_hash_normalizer.rb +98 -0
- data/lib/decidim/voca/sync_locales/locale_context.rb +589 -0
- data/lib/decidim/voca/sync_locales/machine_translation_enqueuer.rb +60 -0
- data/lib/decidim/voca/sync_locales/relation_lint_runner.rb +55 -0
- data/lib/decidim/voca/sync_locales/runner.rb +352 -0
- data/lib/decidim/voca/sync_locales.rb +11 -0
- data/lib/decidim/voca/update_column_without_callbacks.rb +21 -0
- data/lib/decidim/voca/version.rb +13 -3
- data/lib/decidim/voca.rb +42 -13
- data/lib/decidim-voca.rb +5 -0
- data/lib/tasks/decidim/voca/voca_anonymize.rake +41 -0
- data/lib/tasks/decidim/voca/voca_clean_machine_translations.rake +22 -0
- data/lib/tasks/decidim/voca/voca_enhanced_invalid_record.rake +141 -0
- data/lib/tasks/decidim/voca/voca_install_migration.rake +45 -0
- data/lib/tasks/decidim/voca/voca_relation_lint.rake +12 -0
- data/lib/tasks/decidim/voca/voca_sync_locales.rake +10 -0
- data/lib/tasks/decidim/voca/voca_upgrade.rake +13 -0
- data/lib/tasks/decidim/voca/voca_webpacker.rake +58 -0
- metadata +248 -55
- data/app/assets/config/voca_manifest.js +0 -0
- data/app/assets/images/decidim/voca/icon.svg +0 -1
- data/app/commands/decidim/voca/organization/command.rb +0 -16
- data/app/commands/decidim/voca/organization/organization_command.rb +0 -17
- data/app/commands/decidim/voca/organization/update_color_command.rb +0 -35
- data/app/commands/decidim/voca/organization/update_environment_command.rb +0 -23
- data/app/commands/decidim/voca/organization/update_feature_command.rb +0 -32
- data/app/commands/decidim/voca/organization/update_file_upload_command.rb +0 -45
- data/app/commands/decidim/voca/organization/update_locale_command.rb +0 -53
- data/app/commands/decidim/voca/organization/update_naming_command.rb +0 -30
- data/app/commands/decidim/voca/organization/update_permission_command.rb +0 -31
- data/app/commands/decidim/voca/organization/update_smtp_command.rb +0 -51
- data/app/jobs/decidim/voca/command_job.rb +0 -36
- data/app/jobs/decidim/voca/webhook_notifier_job.rb +0 -57
- data/app/rpc/decidim/voca/decidim_service_controller.rb +0 -68
- data/app/rpc/decidim/voca/rpc/enum_casting.rb +0 -58
- data/app/rpc/decidim/voca/rpc/get_settings.rb +0 -144
- data/app/rpc/decidim/voca/rpc/health.rb +0 -18
- data/app/rpc/decidim/voca/rpc/seed_admin.rb +0 -75
- data/app/rpc/decidim/voca/rpc/set_settings.rb +0 -56
- data/app/rpc/decidim/voca/rpc/setup_db.rb +0 -38
- data/config/environments/development.rb +0 -6
- data/config/spring.rb +0 -2
- data/lib/decidim/voca/admin.rb +0 -10
- data/lib/decidim/voca/admin_engine.rb +0 -27
- data/lib/decidim/voca/backup/app_backup.rb +0 -169
- data/lib/decidim/voca/backup/app_decrypt_backup_file.rb +0 -50
- data/lib/decidim/voca/backup/app_encrypt_backup_file.rb +0 -49
- data/lib/decidim/voca/backup/app_upload_to_s3.rb +0 -49
- data/lib/decidim/voca/middleware/dynamic_config_middleware.rb +0 -42
- data/lib/decidim/voca/rpc/decidim_pb.rb +0 -134
- data/lib/decidim/voca/rpc/decidim_services_pb.rb +0 -30
- data/lib/decidim/voca/test/factories.rb +0 -9
- data/lib/decidim/voca/updater.rb +0 -50
- data/lib/tasks/voca.rake +0 -88
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Voca
|
|
5
|
+
# Enqueues {MachineTranslateContentBlockSettingJob} when translated settings change.
|
|
6
|
+
module ContentBlockTranslatedSettingsMachineTranslation
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
# Use after_save (not after_commit) so +saved_changes+ is present; nested MT persists
|
|
11
|
+
# via +update_column+ and does not re-fire these callbacks.
|
|
12
|
+
after_create :enqueue_content_block_translated_settings_machine_translation
|
|
13
|
+
after_update :enqueue_content_block_translated_settings_machine_translation
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def enqueue_content_block_translated_settings_machine_translation
|
|
17
|
+
return unless enqueue_content_block_translated_settings_mt_prerequisites?
|
|
18
|
+
|
|
19
|
+
change = saved_changes["settings"] || saved_changes[:settings]
|
|
20
|
+
return if change.blank?
|
|
21
|
+
|
|
22
|
+
org = organization
|
|
23
|
+
keys = ContentBlockSettingManifest.translated_keys(manifest)
|
|
24
|
+
return if keys.empty?
|
|
25
|
+
|
|
26
|
+
locales = org.available_locales.map(&:to_s)
|
|
27
|
+
old_s = extract_settings(change[0], keys, locales)
|
|
28
|
+
new_s = extract_settings(change[1], keys, locales)
|
|
29
|
+
|
|
30
|
+
keys.each do |key|
|
|
31
|
+
enqueue_setting_if_default_locale_changed(key, old_s, new_s, org)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def enqueue_content_block_translated_settings_mt_prerequisites?
|
|
38
|
+
saved_change_to_settings? &&
|
|
39
|
+
Decidim.machine_translation_service_klass &&
|
|
40
|
+
organization&.enable_machine_translations?
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def extract_settings(raw, keys, locales)
|
|
44
|
+
return {} if raw.blank?
|
|
45
|
+
|
|
46
|
+
hash =
|
|
47
|
+
if raw.is_a?(Hash)
|
|
48
|
+
raw
|
|
49
|
+
elsif raw.respond_to?(:to_h)
|
|
50
|
+
raw.to_h
|
|
51
|
+
else
|
|
52
|
+
{}
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
settings = hash.deep_stringify_keys.deep_dup
|
|
56
|
+
ContentBlockSettingManifest.coalesce_flat_keys!(settings, keys, locales)
|
|
57
|
+
settings
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def enqueue_setting_if_default_locale_changed(key, old_s, new_s, org)
|
|
61
|
+
old_h = old_s[key]
|
|
62
|
+
new_h = new_s[key]
|
|
63
|
+
return unless new_h.is_a?(Hash)
|
|
64
|
+
return unless setting_default_locale_changed?(old_h, new_h, org.default_locale.to_s)
|
|
65
|
+
|
|
66
|
+
schedule_setting_jobs(key, new_h, org)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def setting_default_locale_changed?(old_h, new_h, default)
|
|
70
|
+
old_h = old_h.deep_stringify_keys if old_h.is_a?(Hash)
|
|
71
|
+
new_h = new_h.deep_stringify_keys
|
|
72
|
+
old_v = old_h.is_a?(Hash) ? old_h[default] : nil
|
|
73
|
+
new_v = new_h[default]
|
|
74
|
+
old_v != new_v
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def schedule_setting_jobs(key, field_hash, org)
|
|
78
|
+
default = org.default_locale.to_s
|
|
79
|
+
pending = ComponentSettingPendingLocales.for(field_hash, org)
|
|
80
|
+
html = rich_text_content_block_setting?(key)
|
|
81
|
+
|
|
82
|
+
pending.each do |target|
|
|
83
|
+
MachineTranslateContentBlockSettingJob
|
|
84
|
+
.set(wait: Decidim.config.machine_translation_delay)
|
|
85
|
+
.perform_later(id, key, target, default, html:)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def rich_text_content_block_setting?(key)
|
|
90
|
+
attr = manifest.settings.attributes[key.to_sym]
|
|
91
|
+
return false unless attr
|
|
92
|
+
return false unless attr.type == :text
|
|
93
|
+
|
|
94
|
+
attr.editor?({}) == true
|
|
95
|
+
rescue StandardError
|
|
96
|
+
false
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Voca
|
|
5
|
+
module DeepL
|
|
6
|
+
module ActiveJobContext
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
alias_method :voca_serialize_without_deepl, :serialize
|
|
11
|
+
alias_method :voca_deserialize_without_deepl, :deserialize
|
|
12
|
+
|
|
13
|
+
def serialize
|
|
14
|
+
voca_serialize_without_deepl.merge(
|
|
15
|
+
"deepl_context" => Decidim::Voca::DeepL::Context.attributes
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def deserialize(job_data)
|
|
20
|
+
Decidim::Voca::DeepL::Context.attributes = job_data["deepl_context"]
|
|
21
|
+
voca_deserialize_without_deepl(job_data)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Voca
|
|
5
|
+
module DeepL
|
|
6
|
+
class Context < ActiveSupport::CurrentAttributes
|
|
7
|
+
attribute :organization
|
|
8
|
+
attribute :participatory_space
|
|
9
|
+
attribute :current_component
|
|
10
|
+
attribute :current_locale
|
|
11
|
+
include ::Decidim::TranslatableAttributes
|
|
12
|
+
include ActionView::Helpers::SanitizeHelper
|
|
13
|
+
include ActionView::Helpers::TagHelper
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
def with_organization(organization)
|
|
17
|
+
previous = attributes
|
|
18
|
+
self.organization = organization.to_global_id.to_s if organization
|
|
19
|
+
yield
|
|
20
|
+
ensure
|
|
21
|
+
self.attributes = previous
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
##
|
|
26
|
+
# @return String Context for Deepl API
|
|
27
|
+
def deepl_context
|
|
28
|
+
(
|
|
29
|
+
["Context: the text is written from a participatory platform, organized in participatory spaces, components and users."] +
|
|
30
|
+
organization_context +
|
|
31
|
+
participatory_space_context +
|
|
32
|
+
current_component_context
|
|
33
|
+
).compact_blank.join("\n")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def located_organization
|
|
39
|
+
@located_organization ||= GlobalID::Locator.locate(organization)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def located_participatory_space
|
|
43
|
+
@located_participatory_space ||= GlobalID::Locator.locate(participatory_space)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def located_current_component
|
|
47
|
+
@located_current_component ||= GlobalID::Locator.locate(current_component)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def organization_context
|
|
51
|
+
return [] unless organization
|
|
52
|
+
|
|
53
|
+
[
|
|
54
|
+
"- Platform Name: #{translated_attribute(located_organization.name) || "undefined"}",
|
|
55
|
+
"- Platform Description: #{strip_tags(sanitize(translated_attribute(located_organization.description)) || "undefined")}"
|
|
56
|
+
]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def participatory_space_context
|
|
60
|
+
return [] unless participatory_space
|
|
61
|
+
|
|
62
|
+
[
|
|
63
|
+
"- Participatory Space Name: #{translated_attribute(located_participatory_space.name) || "undefined"}",
|
|
64
|
+
"- Participatory Space Description: #{strip_tags(sanitize(translated_attribute(located_participatory_space.description)) || "undefined")}"
|
|
65
|
+
]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def current_component_context
|
|
69
|
+
return [] unless current_component
|
|
70
|
+
|
|
71
|
+
[
|
|
72
|
+
"- Component Name: #{translated_attribute(located_current_component.name) || "undefined"}"
|
|
73
|
+
]
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Voca
|
|
5
|
+
module DeepL
|
|
6
|
+
module DeepLFormBuilderOverrides
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
alias_method :voca_original_translated, :translated
|
|
11
|
+
##
|
|
12
|
+
# Rewrite of the translated method to avoid passing value to fields
|
|
13
|
+
# translated by machine translation.
|
|
14
|
+
#
|
|
15
|
+
def translated(type, name, options = {})
|
|
16
|
+
return voca_original_translated(type, name, options) if organization_context.minimalistic_deepl?
|
|
17
|
+
return translated_one_locale(type, name, locales.first, options.merge(label: (options[:label] || label_for(name)))) if locales.count == 1
|
|
18
|
+
|
|
19
|
+
safe_join [
|
|
20
|
+
label_tabs_tag(name, options),
|
|
21
|
+
tabs_content_tag(type, name, options)
|
|
22
|
+
]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def organization_context
|
|
28
|
+
# deserialize the global id to get the organization
|
|
29
|
+
@organization_context ||= GlobalID::Locator.locate(Decidim::Voca::DeepL::Context.organization)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def tabs_content_tag(type, name, options = {})
|
|
33
|
+
tabs_id = sanitized_tabs_id(name, options)
|
|
34
|
+
hashtaggable = options.delete(:hashtaggable)
|
|
35
|
+
default_locale = organization_context&.default_locale || ::Decidim.default_locale
|
|
36
|
+
content_tag(:div, class: "tabs-content", data: { tabs_content: tabs_id }) do
|
|
37
|
+
locales.each_with_index.inject("".html_safe) do |string, (locale, index)|
|
|
38
|
+
options_for_field = options.deep_dup
|
|
39
|
+
# If locale is not the default locale, value should be empty
|
|
40
|
+
# to let machine translation do its job
|
|
41
|
+
options_for_field[:value] = "".html_safe if locale.to_s != default_locale.to_s
|
|
42
|
+
tab_content_id = "#{tabs_id}-#{name}-panel-#{index}"
|
|
43
|
+
options_for_field[:"data-machine-tranlated"] = locale.to_s != default_locale.to_s
|
|
44
|
+
string + content_tag(:div, class: tab_element_class_for("panel", index), id: tab_content_id, "aria-hidden": tab_attr_aria_hidden_for(index)) do
|
|
45
|
+
if hashtaggable
|
|
46
|
+
hashtaggable_text_field(type, name, locale, options_for_field.merge(label: false))
|
|
47
|
+
elsif type.to_sym == :editor
|
|
48
|
+
send(type, name_with_locale(name, locale), options_for_field.merge(label: false, hashtaggable:))
|
|
49
|
+
else
|
|
50
|
+
send(type, name_with_locale(name, locale), options_for_field.merge(label: false))
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def sanitized_tabs_id(name, options = {})
|
|
58
|
+
sanitize_tabs_selector(options[:tabs_id] || "#{object_name}-#{name}-tabs")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def label_tabs_tag(name, options = {})
|
|
62
|
+
tabs_id = sanitized_tabs_id(name, options)
|
|
63
|
+
|
|
64
|
+
content_tag(
|
|
65
|
+
:div,
|
|
66
|
+
class: "label--tabs",
|
|
67
|
+
"data-machine-translated": organization_context&.enable_machine_translations?,
|
|
68
|
+
"data-minimalistic-deepl": organization_context&.minimalistic_deepl?
|
|
69
|
+
) do
|
|
70
|
+
field_label = label_i18n(name, options[:label] || label_for(name), required: options[:required])
|
|
71
|
+
|
|
72
|
+
language_selector = "".html_safe
|
|
73
|
+
language_selector = create_language_selector(locales, tabs_id, name) if options[:label] != false
|
|
74
|
+
|
|
75
|
+
safe_join [field_label, language_selector]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# DeepL is conditionally required from +decidim/voca.rb+ when enabled.
|
|
4
|
+
require "active_support"
|
|
5
|
+
require_relative "../machine_translation/translate_string"
|
|
6
|
+
|
|
7
|
+
module Decidim
|
|
8
|
+
module Voca
|
|
9
|
+
module DeepL
|
|
10
|
+
class MachineTranslator
|
|
11
|
+
attr_reader :text, :source_locale, :target_locale, :resource, :field_name
|
|
12
|
+
|
|
13
|
+
include Decidim::TranslatableAttributes
|
|
14
|
+
|
|
15
|
+
def initialize(resource, field_name, text, target_locale, source_locale)
|
|
16
|
+
@resource = resource
|
|
17
|
+
@field_name = field_name
|
|
18
|
+
@text = text
|
|
19
|
+
@target_locale = target_locale
|
|
20
|
+
@source_locale = source_locale
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def translate
|
|
24
|
+
return if text.blank?
|
|
25
|
+
|
|
26
|
+
translation = Decidim::Voca::MachineTranslation::TranslateString.call(
|
|
27
|
+
text:,
|
|
28
|
+
source_locale:,
|
|
29
|
+
target_locale:,
|
|
30
|
+
html: true,
|
|
31
|
+
context: deepl_context
|
|
32
|
+
)
|
|
33
|
+
return if translation.nil?
|
|
34
|
+
|
|
35
|
+
Decidim::MachineTranslationSaveJob.perform_later(
|
|
36
|
+
resource,
|
|
37
|
+
field_name,
|
|
38
|
+
target_locale,
|
|
39
|
+
translation
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def deepl_context
|
|
46
|
+
base = Decidim::Voca::DeepL::Context.deepl_context
|
|
47
|
+
"This is a text for a #{resource.class.name.demodulize.titleize} #{name_context}, field #{field_name}. #{base}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def name_context
|
|
51
|
+
return "" if field_name.to_s.in?(%w(title name))
|
|
52
|
+
|
|
53
|
+
return ", named #{translated_attribute(resource.title)}" if resource.respond_to?(:title)
|
|
54
|
+
return ", named #{translated_attribute(resource.name)}" if resource.respond_to?(:name)
|
|
55
|
+
|
|
56
|
+
""
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Voca
|
|
5
|
+
# Before rendering the page, we set the Deepl Context from
|
|
6
|
+
# request.env attributes set by decidim
|
|
7
|
+
module DeepL
|
|
8
|
+
class Middleware
|
|
9
|
+
def initialize(app)
|
|
10
|
+
@app = app
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call(env)
|
|
14
|
+
return @app.call(env) if env["PATH_INFO"]&.start_with?("/rails/active_storage/")
|
|
15
|
+
|
|
16
|
+
begin
|
|
17
|
+
deepl_context!(env)
|
|
18
|
+
rescue StandardError => e
|
|
19
|
+
log_error("Failed to set Deepl context", e)
|
|
20
|
+
# return [500, { "Content-Type" => "text/html" }, ["Internal Server Error"]]
|
|
21
|
+
end
|
|
22
|
+
@app.call(env)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def deepl_context!(env)
|
|
28
|
+
return unless deepl_enabled?(env)
|
|
29
|
+
|
|
30
|
+
organization_context!(env)
|
|
31
|
+
participatory_space_context!(env)
|
|
32
|
+
component_context!(env)
|
|
33
|
+
locale_context!
|
|
34
|
+
Decidim::Voca::DeepL::Context.attributes
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def deepl_enabled?(env)
|
|
38
|
+
organization = env["decidim.current_organization"]
|
|
39
|
+
organization.respond_to?(:deepl_enabled?) && organization.deepl_enabled?
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def organization_context!(env)
|
|
43
|
+
organization = env["decidim.current_organization"]
|
|
44
|
+
return unless organization
|
|
45
|
+
|
|
46
|
+
Decidim::Voca::DeepL::Context.organization = organization.to_global_id.to_s
|
|
47
|
+
rescue StandardError => e
|
|
48
|
+
log_error("Failed to set organization context", e)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def participatory_space_context!(env)
|
|
52
|
+
participatory_space = env["decidim.current_participatory_space"]
|
|
53
|
+
return unless participatory_space
|
|
54
|
+
|
|
55
|
+
Decidim::Voca::DeepL::Context.participatory_space = participatory_space.to_global_id.to_s
|
|
56
|
+
rescue StandardError => e
|
|
57
|
+
log_error("Failed to set participatory space context", e)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def component_context!(env)
|
|
61
|
+
component = env["decidim.current_component"]
|
|
62
|
+
return unless component
|
|
63
|
+
|
|
64
|
+
Decidim::Voca::DeepL::Context.current_component = component.to_global_id.to_s
|
|
65
|
+
rescue StandardError => e
|
|
66
|
+
log_error("Failed to set component context", e)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def locale_context!
|
|
70
|
+
Decidim::Voca::DeepL::Context.current_locale = I18n.locale.to_s
|
|
71
|
+
rescue StandardError => e
|
|
72
|
+
log_error("Failed to set locale context", e)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def log_error(message, error)
|
|
76
|
+
Rails.logger.error("#{self.class.name}: #{message}")
|
|
77
|
+
Rails.logger.error("Error: #{error.message}")
|
|
78
|
+
Rails.logger.error("Backtrace: #{error.backtrace&.first(5)&.join("\n")}")
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Voca
|
|
5
|
+
module DeepL
|
|
6
|
+
# Single source of truth for VOCA `merge_translatable_fields` widenings when DeepL is enabled.
|
|
7
|
+
# See {#apply_mergeable_fields!}; component `settings` / step locale hashes stay in ComponentSettingSync.
|
|
8
|
+
class EngineConfig
|
|
9
|
+
# @return [Array<Hash>] Each row: :class_name (String), :fields (Array<String>),
|
|
10
|
+
# :include_translatable_resource (Boolean, default false), :if (optional proc — skip row when falsey).
|
|
11
|
+
MERGEABLE_FIELD_REGISTRY = [
|
|
12
|
+
{
|
|
13
|
+
class_name: "Decidim::Component",
|
|
14
|
+
include_translatable_resource: true,
|
|
15
|
+
component_settings_mt: true,
|
|
16
|
+
fields: %w(name)
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
class_name: "Decidim::ContentBlock",
|
|
20
|
+
content_block_settings_mt: true,
|
|
21
|
+
fields: []
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
class_name: "Decidim::DecidimAwesome::AwesomeConfig",
|
|
25
|
+
awesome_menu_labels_mt: true,
|
|
26
|
+
fields: [],
|
|
27
|
+
if: -> { Decidim::Voca::Installation.decidim_awesome_installed? }
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
class_name: "Decidim::Budgets::Budget",
|
|
31
|
+
include_translatable_resource: true,
|
|
32
|
+
fields: %w(title description)
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
class_name: "Decidim::Proposals::ProposalState",
|
|
36
|
+
fields: %w(announcement_title)
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
class_name: "Decidim::Proposals::Proposal",
|
|
40
|
+
fields: %w(answer cost_report execution_period)
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
class_name: "Decidim::Organization",
|
|
44
|
+
fields: %w(short_name)
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
class_name: "Decidim::Category",
|
|
48
|
+
fields: %w(description)
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
class_name: "Decidim::Forms::DisplayCondition",
|
|
52
|
+
include_translatable_resource: true,
|
|
53
|
+
fields: %w(condition_value)
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
class_name: "Decidim::Templates::Template",
|
|
57
|
+
include_translatable_resource: true,
|
|
58
|
+
fields: %w(name description)
|
|
59
|
+
}
|
|
60
|
+
].freeze
|
|
61
|
+
|
|
62
|
+
class << self
|
|
63
|
+
# Wire model hooks and overrides that must be loaded after the app is initialized.
|
|
64
|
+
# @param config [Rails::Application] host app (+Rails.application+; first block arg of +initializer+)
|
|
65
|
+
def initialize!
|
|
66
|
+
configure_deepl!
|
|
67
|
+
configure_machine_translation!
|
|
68
|
+
Rails.application.config.middleware.use ::Decidim::Voca::DeepL::Middleware
|
|
69
|
+
|
|
70
|
+
ActiveSupport.on_load(:active_job) { include Decidim::Voca::DeepL::ActiveJobContext }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Configure the DeepL SDK + Decidim machine translation config (+middleware, ActiveJob hook).
|
|
74
|
+
# Called from {Decidim::Voca::Engine}'s +decidim.voca.deepl+ initializer (middleware stack not frozen yet).
|
|
75
|
+
def configure!
|
|
76
|
+
apply_mergeable_fields!
|
|
77
|
+
fix_accountability_timeline_entry_translatable_fields!
|
|
78
|
+
|
|
79
|
+
unless Decidim::MachineTranslationResourceJob.ancestors.include?(Decidim::Voca::MachineTranslationResourceJobVoca)
|
|
80
|
+
Decidim::MachineTranslationResourceJob.prepend(Decidim::Voca::MachineTranslationResourceJobVoca)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
::Decidim::TranslationBarCell.include(Decidim::Voca::DeepL::TranslationBarOverrides)
|
|
84
|
+
::Decidim::FormBuilder.include(Decidim::Voca::DeepL::DeepLFormBuilderOverrides)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def apply_mergeable_fields!
|
|
88
|
+
MERGEABLE_FIELD_REGISTRY.each { |row| apply_mergeable_registry_row!(row) }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
def apply_mergeable_registry_row!(row)
|
|
94
|
+
return if row[:if] && !row[:if].call
|
|
95
|
+
|
|
96
|
+
klass = row[:class_name].safe_constantize
|
|
97
|
+
return unless klass
|
|
98
|
+
|
|
99
|
+
maybe_include_component_settings_mt!(klass, row)
|
|
100
|
+
maybe_include_content_block_settings_mt!(klass, row)
|
|
101
|
+
maybe_include_awesome_menu_labels_mt!(klass, row)
|
|
102
|
+
maybe_include_translatable_resource!(klass, row)
|
|
103
|
+
merge_applicable_column_fields!(klass, row)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def maybe_include_component_settings_mt!(klass, row)
|
|
107
|
+
return unless row[:component_settings_mt]
|
|
108
|
+
return if klass.included_modules.include?(Decidim::Voca::ComponentTranslatedSettingsMachineTranslation)
|
|
109
|
+
|
|
110
|
+
klass.include(Decidim::Voca::ComponentTranslatedSettingsMachineTranslation)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def maybe_include_content_block_settings_mt!(klass, row)
|
|
114
|
+
return unless row[:content_block_settings_mt]
|
|
115
|
+
return if klass.included_modules.include?(Decidim::Voca::ContentBlockTranslatedSettingsMachineTranslation)
|
|
116
|
+
|
|
117
|
+
klass.include(Decidim::Voca::ContentBlockTranslatedSettingsMachineTranslation)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def maybe_include_awesome_menu_labels_mt!(klass, row)
|
|
121
|
+
return unless row[:awesome_menu_labels_mt]
|
|
122
|
+
return if klass.included_modules.include?(Decidim::Voca::AwesomeMenuLabelMachineTranslation)
|
|
123
|
+
|
|
124
|
+
klass.include(Decidim::Voca::AwesomeMenuLabelMachineTranslation)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def maybe_include_translatable_resource!(klass, row)
|
|
128
|
+
return unless row[:include_translatable_resource]
|
|
129
|
+
return if klass.included_modules.include?(Decidim::TranslatableResource)
|
|
130
|
+
|
|
131
|
+
klass.include(Decidim::TranslatableResource)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def merge_applicable_column_fields!(klass, row)
|
|
135
|
+
return unless database_table_ready?(klass)
|
|
136
|
+
|
|
137
|
+
applicable = row[:fields].map(&:to_s).select { |f| klass.column_names.include?(f) }
|
|
138
|
+
Decidim::Voca.merge_translatable_fields(klass, *applicable) if applicable.any?
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def database_table_ready?(klass)
|
|
142
|
+
klass.table_exists?
|
|
143
|
+
rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished, PG::ConnectionBad
|
|
144
|
+
false
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def fix_accountability_timeline_entry_translatable_fields!
|
|
148
|
+
%w(Decidim::Accountability::TimelineEntry Decidim::Accountability::Milestone).each do |class_name|
|
|
149
|
+
klass = class_name.safe_constantize
|
|
150
|
+
next unless klass
|
|
151
|
+
|
|
152
|
+
list = klass.translatable_fields_list.map(&:to_s)
|
|
153
|
+
next if list.include?("title") && list.include?("description")
|
|
154
|
+
|
|
155
|
+
klass.translatable_fields(:title, :description)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def configure_deepl!
|
|
160
|
+
require "deepl"
|
|
161
|
+
::DeepL.configure do |deepl|
|
|
162
|
+
deepl.auth_key = ENV.fetch("DECIDIM_DEEPL_API_KEY", "")
|
|
163
|
+
deepl.host = ENV.fetch("DECIDIM_DEEPL_HOST", "https://api.deepl.com")
|
|
164
|
+
deepl.version = ENV.fetch("DECIDIM_DEEPL_VERSION", "v2")
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def configure_machine_translation!
|
|
169
|
+
Decidim.configure do |decidim_config|
|
|
170
|
+
decidim_config.enable_machine_translations = true
|
|
171
|
+
decidim_config.machine_translation_service = "Decidim::Voca::DeepL::MachineTranslator"
|
|
172
|
+
decidim_config.machine_translation_delay = 3.seconds
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Voca
|
|
5
|
+
module DeepL
|
|
6
|
+
module TranslationBarOverrides
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
alias_method :voca_original_show, :show
|
|
11
|
+
def show
|
|
12
|
+
return if current_organization.minimalistic_deepl? && I18n.locale.to_s == current_organization.default_locale.to_s
|
|
13
|
+
|
|
14
|
+
voca_original_show
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "deepl/deepl_active_job_context"
|
|
4
|
+
require_relative "deepl/deepl_context"
|
|
5
|
+
require_relative "deepl/deepl_form_builder_overrides"
|
|
6
|
+
require_relative "deepl/deepl_machine_translator"
|
|
7
|
+
require_relative "deepl/deepl_middleware"
|
|
8
|
+
require_relative "deepl/translation_bar_overrides"
|
|
9
|
+
require_relative "deepl/engine_config"
|