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.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +161 -161
  3. data/README.md +42 -133
  4. data/Rakefile +46 -39
  5. data/app/cells/decidim/footer_topics/show_overrides.erb +8 -0
  6. data/app/cells/decidim/meetings/dates_and_map/show_overrides.erb +23 -0
  7. data/app/commands/decidim/voca/admin/code_census/save_code_list.rb +34 -0
  8. data/app/commands/decidim/voca/code_census/confirm_code_authorization.rb +16 -0
  9. data/app/commands/decidim/voca/create_editor_file.rb +36 -0
  10. data/app/commands/decidim/voca/rack_attack_configurator.rb +244 -0
  11. data/app/commands/decidim/voca/sync_redis_routing.rb +120 -0
  12. data/app/commands/decidim/voca/user_fields_configurator.rb +40 -0
  13. data/app/controllers/decidim/voca/admin/application_controller.rb +4 -6
  14. data/app/controllers/decidim/voca/admin/code_census/code_lists_controller.rb +40 -0
  15. data/app/controllers/decidim/voca/application_controller.rb +4 -6
  16. data/app/controllers/decidim/voca/code_census/authorizations_controller.rb +53 -0
  17. data/app/controllers/decidim/voca/editor_files_controller.rb +47 -0
  18. data/app/controllers/decidim/voca/geolocation_controller.rb +32 -0
  19. data/app/forms/decidim/voca/admin/code_census/code_list_form.rb +21 -0
  20. data/app/forms/decidim/voca/admin/organization_config_form.rb +24 -0
  21. data/app/forms/decidim/voca/code_census/code_form.rb +41 -0
  22. data/app/forms/decidim/voca/editor_file_form.rb +18 -0
  23. data/app/jobs/decidim/voca/machine_translate_awesome_menu_label_job.rb +63 -0
  24. data/app/jobs/decidim/voca/machine_translate_component_setting_job.rb +58 -0
  25. data/app/jobs/decidim/voca/machine_translate_content_block_setting_job.rb +85 -0
  26. data/app/models/decidim/voca/conversation_uuid.rb +18 -0
  27. data/app/models/decidim/voca/editor_file.rb +18 -0
  28. data/app/models/decidim/voca/validation_code.rb +13 -0
  29. data/app/overrides/admin_add_machine_translation_flag.rb +36 -0
  30. data/app/overrides/insert_assets.rb +26 -0
  31. data/app/overrides/insert_weglot_scripts.rb +36 -0
  32. data/app/overrides/proposal_camera.rb +18 -0
  33. data/app/overrides/show_votes.rb +27 -0
  34. data/app/overrides/voca_footer.rb +52 -0
  35. data/app/packs/entrypoints/admin-decidim-voca.js +2 -0
  36. data/app/packs/entrypoints/admin-decidim-voca.scss +1 -0
  37. data/app/packs/entrypoints/decidim-voca-fixes.scss +1 -0
  38. data/app/packs/entrypoints/decidim-voca.js +1 -0
  39. data/app/packs/entrypoints/decidim-voca.scss +1 -0
  40. data/app/packs/images/voca-logo-primary-x3.png +0 -0
  41. data/app/packs/src/decidim/direct_uploads/upload_field.js +150 -0
  42. data/app/packs/src/decidim/voca/admin/voca.js +16 -0
  43. data/app/packs/src/decidim/voca/proposal_better_ux/use_camera_button.js +39 -0
  44. data/app/packs/src/decidim/voca/proposal_better_ux/utils/get_current_position.js +21 -0
  45. data/app/packs/src/decidim/voca/proposal_better_ux/utils/get_proposal_form.js +13 -0
  46. data/app/packs/src/decidim/voca/proposal_better_ux/utils/loading.js +23 -0
  47. data/app/packs/src/decidim/voca/proposal_better_ux/utils/locate_coordinates.js +23 -0
  48. data/app/packs/src/decidim/voca/proposal_custom_fields/custom_field_renderer_override.js +152 -0
  49. data/app/packs/src/decidim/voca/proposal_custom_fields/editor_budget_field.js +329 -0
  50. data/app/packs/src/decidim/voca/proposal_custom_fields/editor_upload_field.js +85 -0
  51. data/app/packs/src/decidim/voca/proposal_custom_fields/proposal_custom_fields.js +96 -0
  52. data/app/packs/src/decidim/voca/proposal_custom_fields/render_budget_field.js +76 -0
  53. data/app/packs/src/decidim/voca/proposal_fields/render_budget_field.js +76 -0
  54. data/app/packs/src/decidim/voca/questionnaire/scroll_questionnaire_steps_to_top.js +18 -0
  55. data/app/packs/src/decidim/voca/voca.js +4 -0
  56. data/app/packs/src/decidim/voca/weglot/index.js +13 -0
  57. data/app/packs/src/decidim/voca/weglot/start_weglot.js +112 -0
  58. data/app/packs/src/decidim/voca/weglot/switcher.js +92 -0
  59. data/app/packs/src/decidim/voca/weglot/wait_for_weglot.js +10 -0
  60. data/app/packs/stylesheets/decidim/voca/_custom_proposal_fields.scss +111 -0
  61. data/app/packs/stylesheets/decidim/voca/_decidim_voca.scss +5 -0
  62. data/app/packs/stylesheets/decidim/voca/_decidim_voca_fixes.scss +25 -0
  63. data/app/packs/stylesheets/decidim/voca/_primary_footer.scss +77 -0
  64. data/app/packs/stylesheets/decidim/voca/_proposal_camera.scss +14 -0
  65. data/app/packs/stylesheets/decidim/voca/_weglot.scss +61 -0
  66. data/app/packs/stylesheets/decidim/voca/admin/_decidim_voca.scss +5 -0
  67. data/app/packs/stylesheets/decidim/voca/admin/_full_machine_translation.scss +34 -0
  68. data/app/permissions/decidim/voca/admin/permissions.rb +31 -0
  69. data/app/permissions/decidim/voca/permissions.rb +17 -0
  70. data/app/views/decidim/voca/admin/code_census/authorizations/new.html.erb +33 -0
  71. data/app/views/decidim/voca/admin/code_census/code_lists/edit.html.erb +27 -0
  72. data/app/views/decidim/voca/code_census/authorizations/new.html.erb +24 -0
  73. data/app/views/decidim/voca/deepl/overrides/_admin_machine_translation_alert.html.erb +14 -0
  74. data/app/views/decidim/voca/rack_attack/_blocked.html.erb +151 -0
  75. data/app/views/decidim/voca/weglot/_js_configuration.html.erb +14 -0
  76. data/app/views/decidim/voca/weglot/_locale_switcher.html.erb +14 -0
  77. data/app/views/decidim/voca/weglot/_locale_switcher_mobile.erb +14 -0
  78. data/app/views/decidim/voca/weglot/_topbar_locale_switcher.html.erb +14 -0
  79. data/config/assets.rb +12 -0
  80. data/config/i18n-tasks.yml +14 -5
  81. data/config/locales/cs.yml +94 -0
  82. data/config/locales/custom_fields.cs.yml +30 -0
  83. data/config/locales/custom_fields.en.yml +41 -0
  84. data/config/locales/custom_fields.fr.yml +30 -0
  85. data/config/locales/custom_fields.it.yml +30 -0
  86. data/config/locales/en.yml +97 -4
  87. data/config/locales/fr.yml +102 -0
  88. data/config/locales/it.yml +94 -0
  89. data/lib/decidim/voca/awesome_menu_label_machine_translation.rb +73 -0
  90. data/lib/decidim/voca/awesome_menu_labels.rb +28 -0
  91. data/lib/decidim/voca/cache_namespace.rb +21 -0
  92. data/lib/decidim/voca/code_census/admin_engine.rb +22 -0
  93. data/lib/decidim/voca/code_census/engine.rb +29 -0
  94. data/lib/decidim/voca/code_census/workflow.rb +7 -0
  95. data/lib/decidim/voca/code_census.rb +4 -0
  96. data/lib/decidim/voca/component_setting_manifest.rb +27 -0
  97. data/lib/decidim/voca/component_setting_pending_locales.rb +33 -0
  98. data/lib/decidim/voca/component_translated_settings_machine_translation.rb +86 -0
  99. data/lib/decidim/voca/configuration.rb +23 -0
  100. data/lib/decidim/voca/content_block_setting_manifest.rb +67 -0
  101. data/lib/decidim/voca/content_block_translated_settings_machine_translation.rb +100 -0
  102. data/lib/decidim/voca/deepl/deepl_active_job_context.rb +27 -0
  103. data/lib/decidim/voca/deepl/deepl_context.rb +78 -0
  104. data/lib/decidim/voca/deepl/deepl_form_builder_overrides.rb +82 -0
  105. data/lib/decidim/voca/deepl/deepl_machine_translator.rb +61 -0
  106. data/lib/decidim/voca/deepl/deepl_middleware.rb +83 -0
  107. data/lib/decidim/voca/deepl/engine_config.rb +179 -0
  108. data/lib/decidim/voca/deepl/translation_bar_overrides.rb +20 -0
  109. data/lib/decidim/voca/deepl.rb +9 -0
  110. data/lib/decidim/voca/engine.rb +318 -11
  111. data/lib/decidim/voca/export/comment_serializer_localized_csv.rb +34 -0
  112. data/lib/decidim/voca/export/csv_with_locale_transformer.rb +53 -0
  113. data/lib/decidim/voca/export/proposal_serializer_localized_csv.rb +45 -0
  114. data/lib/decidim/voca/export/user_answers_serializer_localized_csv.rb +54 -0
  115. data/lib/decidim/voca/good_job_settings.rb +74 -0
  116. data/lib/decidim/voca/installation.rb +33 -0
  117. data/lib/decidim/voca/machine_translation/translate_string.rb +121 -0
  118. data/lib/decidim/voca/machine_translation_human_source.rb +29 -0
  119. data/lib/decidim/voca/machine_translation_resource_job_voca.rb +65 -0
  120. data/lib/decidim/voca/organization_voca_extensions.rb +31 -0
  121. data/lib/decidim/voca/overrides/csv_export_serializers.rb +51 -0
  122. data/lib/decidim/voca/overrides/meetings/dates_and_map_cell_overrides.rb +20 -0
  123. data/lib/decidim/voca/overrides/menu_breadcrumb_last_activity_cache.rb +31 -0
  124. data/lib/decidim/voca/overrides/mod_secure/conversation_controller_overrides.rb +19 -0
  125. data/lib/decidim/voca/overrides/mod_secure/conversation_sanitize.rb +29 -0
  126. data/lib/decidim/voca/overrides/mod_secure/conversation_uuid.rb +34 -0
  127. data/lib/decidim/voca/overrides/mod_secure/user_conversations_redirect_overrides.rb +53 -0
  128. data/lib/decidim/voca/overrides/mod_secure/user_profile_verification_override.rb +31 -0
  129. data/lib/decidim/voca/overrides/newsletter_helper_overrides.rb +30 -0
  130. data/lib/decidim/voca/overrides/resource_presenter_overrides.rb +30 -0
  131. data/lib/decidim/voca/overrides/sanitize_helper_overrides.rb +39 -0
  132. data/lib/decidim/voca/sync_locales/README.md +13 -0
  133. data/lib/decidim/voca/sync_locales/awesome_menu_label_sync.rb +89 -0
  134. data/lib/decidim/voca/sync_locales/command.rb +54 -0
  135. data/lib/decidim/voca/sync_locales/component_setting_sync.rb +72 -0
  136. data/lib/decidim/voca/sync_locales/content_block_setting_sync.rb +86 -0
  137. data/lib/decidim/voca/sync_locales/field_hash_normalizer.rb +98 -0
  138. data/lib/decidim/voca/sync_locales/locale_context.rb +589 -0
  139. data/lib/decidim/voca/sync_locales/machine_translation_enqueuer.rb +60 -0
  140. data/lib/decidim/voca/sync_locales/relation_lint_runner.rb +55 -0
  141. data/lib/decidim/voca/sync_locales/runner.rb +352 -0
  142. data/lib/decidim/voca/sync_locales.rb +11 -0
  143. data/lib/decidim/voca/update_column_without_callbacks.rb +21 -0
  144. data/lib/decidim/voca/version.rb +13 -3
  145. data/lib/decidim/voca.rb +42 -13
  146. data/lib/decidim-voca.rb +5 -0
  147. data/lib/tasks/decidim/voca/voca_anonymize.rake +41 -0
  148. data/lib/tasks/decidim/voca/voca_clean_machine_translations.rake +22 -0
  149. data/lib/tasks/decidim/voca/voca_enhanced_invalid_record.rake +141 -0
  150. data/lib/tasks/decidim/voca/voca_install_migration.rake +45 -0
  151. data/lib/tasks/decidim/voca/voca_relation_lint.rake +12 -0
  152. data/lib/tasks/decidim/voca/voca_sync_locales.rake +10 -0
  153. data/lib/tasks/decidim/voca/voca_upgrade.rake +13 -0
  154. data/lib/tasks/decidim/voca/voca_webpacker.rake +58 -0
  155. metadata +248 -55
  156. data/app/assets/config/voca_manifest.js +0 -0
  157. data/app/assets/images/decidim/voca/icon.svg +0 -1
  158. data/app/commands/decidim/voca/organization/command.rb +0 -16
  159. data/app/commands/decidim/voca/organization/organization_command.rb +0 -17
  160. data/app/commands/decidim/voca/organization/update_color_command.rb +0 -35
  161. data/app/commands/decidim/voca/organization/update_environment_command.rb +0 -23
  162. data/app/commands/decidim/voca/organization/update_feature_command.rb +0 -32
  163. data/app/commands/decidim/voca/organization/update_file_upload_command.rb +0 -45
  164. data/app/commands/decidim/voca/organization/update_locale_command.rb +0 -53
  165. data/app/commands/decidim/voca/organization/update_naming_command.rb +0 -30
  166. data/app/commands/decidim/voca/organization/update_permission_command.rb +0 -31
  167. data/app/commands/decidim/voca/organization/update_smtp_command.rb +0 -51
  168. data/app/jobs/decidim/voca/command_job.rb +0 -36
  169. data/app/jobs/decidim/voca/webhook_notifier_job.rb +0 -57
  170. data/app/rpc/decidim/voca/decidim_service_controller.rb +0 -68
  171. data/app/rpc/decidim/voca/rpc/enum_casting.rb +0 -58
  172. data/app/rpc/decidim/voca/rpc/get_settings.rb +0 -144
  173. data/app/rpc/decidim/voca/rpc/health.rb +0 -18
  174. data/app/rpc/decidim/voca/rpc/seed_admin.rb +0 -75
  175. data/app/rpc/decidim/voca/rpc/set_settings.rb +0 -56
  176. data/app/rpc/decidim/voca/rpc/setup_db.rb +0 -38
  177. data/config/environments/development.rb +0 -6
  178. data/config/spring.rb +0 -2
  179. data/lib/decidim/voca/admin.rb +0 -10
  180. data/lib/decidim/voca/admin_engine.rb +0 -27
  181. data/lib/decidim/voca/backup/app_backup.rb +0 -169
  182. data/lib/decidim/voca/backup/app_decrypt_backup_file.rb +0 -50
  183. data/lib/decidim/voca/backup/app_encrypt_backup_file.rb +0 -49
  184. data/lib/decidim/voca/backup/app_upload_to_s3.rb +0 -49
  185. data/lib/decidim/voca/middleware/dynamic_config_middleware.rb +0 -42
  186. data/lib/decidim/voca/rpc/decidim_pb.rb +0 -134
  187. data/lib/decidim/voca/rpc/decidim_services_pb.rb +0 -30
  188. data/lib/decidim/voca/test/factories.rb +0 -9
  189. data/lib/decidim/voca/updater.rb +0 -50
  190. data/lib/tasks/voca.rake +0 -88
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module SyncLocales
6
+ # Normalizes + machine-translates Awesome menu item +label+ hashes inline
7
+ # (like {MachineTranslationEnqueuer} / {ContentBlockSettingSync}) so
8
+ # +machine_translations+ is populated during +sync_locales+ without workers.
9
+ class AwesomeMenuLabelSync
10
+ def initialize(record)
11
+ @record = record
12
+ end
13
+
14
+ def call
15
+ return unless syncable?
16
+
17
+ context = LocaleContext.for(@record)
18
+ value = FieldHashNormalizer.deep_stringify(@record.value).deep_dup
19
+ return unless value.is_a?(Array)
20
+
21
+ changed = sync_items!(value, context)
22
+ return unless changed
23
+
24
+ UpdateColumnWithoutCallbacks.call(@record, :value, value)
25
+ end
26
+
27
+ private
28
+
29
+ def syncable?
30
+ defined?(Decidim::DecidimAwesome::AwesomeConfig) &&
31
+ @record.is_a?(Decidim::DecidimAwesome::AwesomeConfig) &&
32
+ AwesomeMenuLabels.menu_config_value?(@record.value)
33
+ end
34
+
35
+ def sync_items!(value, context)
36
+ changed = false
37
+ value.each do |item|
38
+ next unless syncable_item?(item)
39
+
40
+ changed = true if sync_item_label!(item, context)
41
+ end
42
+ changed
43
+ end
44
+
45
+ def syncable_item?(item)
46
+ item.is_a?(Hash) && item["label"].is_a?(Hash) && item["url"].present?
47
+ end
48
+
49
+ def sync_item_label!(item, context)
50
+ raw = item["label"]
51
+ stringy = FieldHashNormalizer.deep_stringify(raw)
52
+ normalized = FieldHashNormalizer.call(raw, context)
53
+ translate_pending_inline!(item["url"], normalized, context)
54
+ return false if normalized == stringy
55
+
56
+ item["label"] = normalized
57
+ true
58
+ end
59
+
60
+ def translate_pending_inline!(item_url, normalized, context)
61
+ return unless Decidim.machine_translation_service_klass
62
+ return unless context.enable_machine_translations?
63
+
64
+ default = context.default_locale
65
+ source_text = normalized.stringify_keys[default]
66
+ return if source_text.blank?
67
+
68
+ ComponentSettingPendingLocales.for(normalized, context.organization).each do |target_locale|
69
+ next if normalized.dig("machine_translations", target_locale).present?
70
+
71
+ translated = Decidim::Voca::DeepL::Context.with_organization(context.organization) do
72
+ MachineTranslation::TranslateString.call(
73
+ text: source_text,
74
+ source_locale: default,
75
+ target_locale:,
76
+ html: false,
77
+ context: "Decidim::DecidimAwesome::AwesomeConfig id=#{@record.id} url=#{item_url}"
78
+ )
79
+ end
80
+ next if translated.nil?
81
+
82
+ normalized["machine_translations"] ||= {}
83
+ normalized["machine_translations"][target_locale] = translated
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module SyncLocales
6
+ # Invokes decidim:locales:rebuild_search, runs locale normalization over all translatable rows, then rebuilds search again.
7
+ class Command < Decidim::Command
8
+ def call
9
+ ensure_minimalistic_deepl!
10
+ ensure_rake_tasks!
11
+ rebuild_search_task.reenable
12
+ rebuild_search_task.invoke
13
+ Rails.logger.debug "=" * 80
14
+ Rails.logger.debug "Starting sync locales"
15
+ Runner.new.call
16
+ Rails.logger.debug "Sync locales completed"
17
+ Rails.logger.debug "=" * 80
18
+ rebuild_search_task.reenable
19
+ rebuild_search_task.invoke
20
+ broadcast(:ok)
21
+ rescue StandardError => e
22
+ broadcast(:invalid, e.message)
23
+ raise
24
+ end
25
+
26
+ private
27
+
28
+ def ensure_minimalistic_deepl!
29
+ return if organization.minimalistic_deepl?
30
+
31
+ raise(StandardError, "organization.minimalistic_deepl? must be true to run #{self.class.name}")
32
+ end
33
+
34
+ def ensure_rake_tasks!
35
+ return if Rake::Task.task_defined?("decidim:locales:rebuild_search")
36
+
37
+ Rails.application.load_tasks
38
+ end
39
+
40
+ def rebuild_search_task
41
+ @rebuild_search_task ||= Rake::Task["decidim:locales:rebuild_search"]
42
+ end
43
+
44
+ def organization
45
+ Decidim::Organization.first
46
+ end
47
+ end
48
+
49
+ def self.call
50
+ Command.call
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module SyncLocales
6
+ # Normalizes + enqueues MT for translated global keys under +Component#settings+.
7
+ class ComponentSettingSync
8
+ def initialize(record)
9
+ @record = record
10
+ end
11
+
12
+ def call
13
+ return unless @record.is_a?(Decidim::Component)
14
+
15
+ keys = Decidim::Voca::ComponentSettingManifest.translated_global_keys(@record.manifest)
16
+ return if keys.empty?
17
+
18
+ settings = (@record.read_attribute(:settings) || {}).deep_dup.deep_stringify_keys
19
+ global = settings["global"] ||= {}
20
+ changed = false
21
+ context = LocaleContext.for(@record)
22
+
23
+ keys.each do |key|
24
+ raw = global[key]
25
+ next unless raw.is_a?(Hash)
26
+
27
+ stringy = FieldHashNormalizer.deep_stringify(raw)
28
+ normalized = FieldHashNormalizer.call(raw, context)
29
+ if normalized != stringy
30
+ global[key] = normalized
31
+ changed = true
32
+ end
33
+ enqueue_for_key(key, normalized, context)
34
+ end
35
+
36
+ return unless changed
37
+
38
+ UpdateColumnWithoutCallbacks.call(@record, :settings, settings)
39
+ end
40
+
41
+ private
42
+
43
+ def enqueue_for_key(key, normalized, context)
44
+ return unless Decidim.machine_translation_service_klass
45
+ return unless context.enable_machine_translations?
46
+
47
+ default = context.default_locale
48
+ source_text = normalized.stringify_keys[default]
49
+ return if source_text.blank?
50
+
51
+ ComponentSettingPendingLocales.for(normalized, context.organization).each do |target_locale|
52
+ html = rich_text_component_setting?(key)
53
+ Decidim::Voca::MachineTranslateComponentSettingJob
54
+ .set(wait: Decidim.config.machine_translation_delay)
55
+ .perform_later(@record.id, key, target_locale, default, html:)
56
+ end
57
+ end
58
+
59
+ def rich_text_component_setting?(key)
60
+ attr = @record.manifest.settings(:global).attributes[key.to_sym]
61
+ return true unless attr
62
+ return false unless attr.type == :text
63
+
64
+ ctx = { component: @record, participatory_space: @record.participatory_space }
65
+ attr.editor?(ctx) == true
66
+ rescue StandardError
67
+ true
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module SyncLocales
6
+ # Normalizes + machine-translates translated keys under +ContentBlock#settings+.
7
+ # Translates inline (like {MachineTranslationEnqueuer}) and persists Decidim flat keys
8
+ # (+welcome_text_fr+ / +welcome_text_en+) so EN is populated after +sync_locales+.
9
+ class ContentBlockSettingSync
10
+ def initialize(record)
11
+ @record = record
12
+ end
13
+
14
+ def call
15
+ return unless @record.is_a?(Decidim::ContentBlock)
16
+
17
+ keys = Decidim::Voca::ContentBlockSettingManifest.translated_keys(@record.manifest)
18
+ return if keys.empty?
19
+
20
+ context = LocaleContext.for(@record)
21
+ settings = (@record.read_attribute(:settings) || {}).deep_dup.deep_stringify_keys
22
+ Decidim::Voca::ContentBlockSettingManifest.coalesce_flat_keys!(
23
+ settings,
24
+ keys,
25
+ context.allowed_locales
26
+ )
27
+
28
+ keys.each do |key|
29
+ raw = settings[key]
30
+ next unless raw.is_a?(Hash)
31
+
32
+ normalized = FieldHashNormalizer.call(raw, context)
33
+ translate_pending_inline!(key, normalized, context)
34
+ settings[key] = normalized
35
+ end
36
+
37
+ Decidim::Voca::ContentBlockSettingManifest.expand_to_flat_keys!(settings, keys)
38
+ original = FieldHashNormalizer.deep_stringify(@record.read_attribute(:settings) || {})
39
+ return if settings == original
40
+
41
+ UpdateColumnWithoutCallbacks.call(@record, :settings, settings)
42
+ end
43
+
44
+ private
45
+
46
+ def translate_pending_inline!(key, normalized, context)
47
+ return unless Decidim.machine_translation_service_klass
48
+ return unless context.enable_machine_translations?
49
+
50
+ default = context.default_locale
51
+ source_text = normalized.stringify_keys[default]
52
+ return if source_text.blank?
53
+
54
+ html = rich_text_content_block_setting?(key)
55
+ ComponentSettingPendingLocales.for(normalized, context.organization).each do |target_locale|
56
+ next if normalized.dig("machine_translations", target_locale).present?
57
+
58
+ translated = Decidim::Voca::DeepL::Context.with_organization(context.organization) do
59
+ MachineTranslation::TranslateString.call(
60
+ text: source_text,
61
+ source_locale: default,
62
+ target_locale:,
63
+ html:,
64
+ context: "Decidim::ContentBlock id=#{@record.id} setting=#{key}"
65
+ )
66
+ end
67
+ next if translated.nil?
68
+
69
+ normalized["machine_translations"] ||= {}
70
+ normalized["machine_translations"][target_locale] = translated
71
+ end
72
+ end
73
+
74
+ def rich_text_content_block_setting?(key)
75
+ attr = @record.manifest.settings.attributes[key.to_sym]
76
+ return false unless attr
77
+ return false unless attr.type == :text
78
+
79
+ attr.editor?({}) == true
80
+ rescue StandardError
81
+ false
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module SyncLocales
6
+ # Pure transforms for translatable JSON hashes: rules 3 → 2 → 4 → 5 → 3 (top-level).
7
+ class FieldHashNormalizer
8
+ class << self
9
+ def call(field_hash, context)
10
+ new(context).normalize(field_hash)
11
+ end
12
+
13
+ # Deep stringify keys
14
+ # Example: `[{fr: "Bonjour"}]` -> `[{"fr": "Bonjour"}]`
15
+ def deep_stringify(obj)
16
+ case obj
17
+ when Hash
18
+ obj.each_with_object({}) { |(k, v), acc| acc[k.to_s] = deep_stringify(v) }
19
+ when Array
20
+ obj.map { |e| deep_stringify(e) }
21
+ else
22
+ obj
23
+ end
24
+ end
25
+ end
26
+
27
+ def initialize(context)
28
+ @context = context
29
+ end
30
+
31
+ def normalize(field_hash)
32
+ return field_hash unless field_hash.is_a?(Hash)
33
+
34
+ locale_hash = FieldHashNormalizer.deep_stringify(field_hash).deep_dup
35
+ allowed = @context.allowed_locales
36
+ default = @context.default_locale
37
+
38
+ raw_mt = locale_hash.delete("machine_translations")
39
+ mt_hash = raw_mt.is_a?(Hash) ? FieldHashNormalizer.deep_stringify(raw_mt) : {}
40
+
41
+ prune_mt_keys_not_in_allowed!(mt_hash, allowed)
42
+ promote_default_from_mt_to_root!(locale_hash, mt_hash, default)
43
+ strip_default_from_mt!(mt_hash, default)
44
+ move_non_default_roots_to_mt!(locale_hash, mt_hash, allowed, default)
45
+ prune_top_level_keys_not_in_allowed!(locale_hash, allowed)
46
+
47
+ locale_hash["machine_translations"] = mt_hash if mt_hash.present?
48
+
49
+ locale_hash
50
+ end
51
+
52
+ private
53
+
54
+ # Remove machine translation keys that are not in allowed locales
55
+ def prune_mt_keys_not_in_allowed!(mt_hash, allowed_locales)
56
+ mt_hash.keys.each do |key|
57
+ mt_hash.delete(key) unless allowed_locales.include?(key)
58
+ end
59
+ end
60
+
61
+ # If default locale is not human-filled but exists in machine translations, promote it to the root
62
+ def promote_default_from_mt_to_root!(locale_hash, mt_hash, default_locale)
63
+ return if locale_hash[default_locale].present?
64
+
65
+ promoted = mt_hash.delete(default_locale)
66
+ return if promoted.blank?
67
+
68
+ locale_hash[default_locale] = promoted
69
+ end
70
+
71
+ # Never machine-translate default locale
72
+ def strip_default_from_mt!(mt_hash, default_locale)
73
+ mt_hash.delete(default_locale)
74
+ end
75
+
76
+ # All non-default locales must be machine-translated
77
+ def move_non_default_roots_to_mt!(locale_hash, mt_hash, allowed_locales, default_locale)
78
+ allowed_locales.each do |locale|
79
+ next if locale == default_locale
80
+
81
+ val = locale_hash[locale]
82
+ next if val.blank?
83
+
84
+ mt_hash[locale] = val
85
+ locale_hash[locale] = ""
86
+ end
87
+ end
88
+
89
+ # Remove top-level keys that are not in allowed locales
90
+ def prune_top_level_keys_not_in_allowed!(locale_hash, allowed_locales)
91
+ locale_hash.keys.each do |key|
92
+ locale_hash.delete(key) unless allowed_locales.include?(key)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end