lesli_babel 1.1.2 → 1.1.4

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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +3 -0
  3. data/app/assets/stylesheets/lesli_babel/application.css +652 -34
  4. data/app/controllers/lesli_babel/application_controller.rb +14 -0
  5. data/app/controllers/lesli_babel/buckets_controller.rb +1 -7
  6. data/app/controllers/lesli_babel/labels_controller.rb +53 -35
  7. data/app/controllers/lesli_babel/translations_controller.rb +33 -43
  8. data/app/helpers/lesli_babel/labels_helper.rb +51 -2
  9. data/app/models/lesli_babel/application_record.rb +1 -1
  10. data/app/models/lesli_babel/label.rb +19 -2
  11. data/app/services/lesli_babel/label_service.rb +35 -6
  12. data/app/services/lesli_babel/translation_deploy_service.rb +144 -0
  13. data/app/services/lesli_babel/translation_export_service.rb +119 -0
  14. data/app/services/lesli_babel/translation_import_service.rb +184 -0
  15. data/app/services/lesli_babel/translation_service.rb +39 -148
  16. data/app/views/lesli_babel/dashboards/show.html.erb +62 -11
  17. data/app/views/lesli_babel/labels/_copy_path.html.erb +15 -0
  18. data/app/views/lesli_babel/labels/_delete_button.html.erb +14 -0
  19. data/app/views/lesli_babel/labels/_form.html.erb +56 -9
  20. data/app/views/lesli_babel/labels/_module_navigation.html.erb +15 -0
  21. data/app/views/lesli_babel/labels/index.html.erb +65 -36
  22. data/app/views/lesli_babel/labels/panel.html.erb +8 -0
  23. data/app/views/lesli_babel/labels/show.html.erb +10 -2
  24. data/app/views/lesli_babel/partials/_deploy_button.html.erb +21 -0
  25. data/config/locales/navigation.en.yml +5 -0
  26. data/config/locales/navigation.es.yml +5 -0
  27. data/config/locales/translations.en.yml +11 -1
  28. data/config/locales/translations.es.yml +11 -1
  29. data/config/locales/translations.fr.yml +2 -1
  30. data/config/locales/translations.it.yml +2 -1
  31. data/config/locales/translations.pt.yml +2 -1
  32. data/config/routes.rb +1 -2
  33. data/db/migrate/v1.0/0901100110_create_lesli_babel_modules.rb +6 -5
  34. data/db/migrate/v1.0/0901110110_create_lesli_babel_buckets.rb +7 -6
  35. data/db/migrate/v1.0/0901120110_create_lesli_babel_labels.rb +23 -22
  36. data/lib/lesli_babel/engine.rb +0 -4
  37. data/lib/lesli_babel/version.rb +2 -2
  38. data/lib/tasks/lesli_babel_tasks.rake +135 -83
  39. data/readme.md +8 -6
  40. metadata +16 -6
  41. data/app/assets/config/lesli_babel_manifest.js +0 -7
  42. data/app/assets/stylesheets/lesli_babel/application.tailwind.css +0 -2
  43. data/app/views/lesli_babel/partials/_sidebar.html.erb +0 -45
@@ -33,5 +33,19 @@ Building a better future, one line of code at a time.
33
33
  module LesliBabel
34
34
  class ApplicationController < Lesli::ApplicationLesliController
35
35
  protect_from_forgery with: :exception
36
+
37
+ helper_method :babel_locales, :web_deploy_allowed?
38
+
39
+ def babel_locales
40
+ Lesli.config.babel.fetch(:locales)
41
+ end
42
+
43
+ def babel_locale_codes
44
+ babel_locales.keys
45
+ end
46
+
47
+ def web_deploy_allowed?
48
+ Lesli.config.babel.fetch(:allow_web_deploy, !Rails.env.production?)
49
+ end
36
50
  end
37
51
  end
@@ -32,7 +32,7 @@ Building a better future, one line of code at a time.
32
32
 
33
33
  module LesliBabel
34
34
  class BucketsController < ApplicationController
35
- before_action :set_bucket, only: [:show, :edit, :update, :destroy]
35
+ before_action :set_bucket, only: [:show, :edit, :update]
36
36
 
37
37
  # GET /buckets
38
38
  def index
@@ -73,12 +73,6 @@ module LesliBabel
73
73
  end
74
74
  end
75
75
 
76
- # DELETE /buckets/1
77
- def destroy
78
- @bucket.destroy
79
- redirect_to buckets_url, notice: 'Bucket was successfully destroyed.'
80
- end
81
-
82
76
  private
83
77
 
84
78
  # Use callbacks to share common setup or constraints between actions.
@@ -1,17 +1,22 @@
1
1
  module LesliBabel
2
2
  class LabelsController < ApplicationController
3
- before_action :set_label, only: %i[show edit update destroy ]
3
+ before_action :set_label, only: %i[show update destroy]
4
4
  before_action :set_module, only: [:new]
5
5
 
6
6
  # GET /labels
7
7
  def index
8
8
  query[:pagination][:perPage] = 100
9
9
  @labels = respond_with_pagination(LabelService.new(current_user, query).index(params))
10
+ if params[:missing].to_s == "true"
11
+ @missing_translations = true
12
+ @missing_locale = configured_locale_name(params[:locale])
13
+ end
10
14
  end
11
15
 
12
16
  # GET /labels/1
13
17
  def show
14
18
  @label = @label.show
19
+ render(:panel, layout: false) if turbo_frame_request?
15
20
  end
16
21
 
17
22
  # GET /labels/new
@@ -40,57 +45,51 @@ module LesliBabel
40
45
  end
41
46
  end
42
47
 
43
- def edit
44
- end
45
-
46
48
  # PATCH/PUT /labels/1
47
49
  def update
48
50
  return respond_with_not_found unless @label.found?
49
51
 
50
52
  @label = @label.result
53
+ @module = @label.bucket.module
54
+ attributes = label_params
51
55
 
52
- # if status changed
53
- if @label["status"] != label_params["status"]
54
-
55
- # saved update date
56
- @label["last_update_status"] = Time.now
57
-
58
- end
59
-
60
- # if context changed
61
- if @label["context"] != label_params["context"]
62
-
63
- # saved update date
64
- @label["last_update_context"] = Time.now
56
+ update_change_timestamps(@label, attributes)
65
57
 
66
- end
67
-
68
- I18n.available_locales.each do |locale|
69
-
70
- # if translation changed
71
- if @label[locale] != label_params[locale]
72
-
73
- # saved update date
74
- @label["last_update_#{locale}"] = Time.now
75
- end
76
- end
77
-
78
- if @label.update(label_params)
58
+ if @label.update(attributes)
79
59
  respond_with_lesli(
80
60
  :turbo => [
81
- stream_notification_success("Translation label updated successfully!"),
61
+ stream_notification_success("Translations saved successfully."),
82
62
  turbo_stream.replace('lesli-babel-labels-form', partial: 'lesli_babel/labels/form')
83
63
  ]
84
64
  )
85
65
  else
86
- respond_with_lesli(:turbo => stream_notification_success("Error updating translation label"))
66
+ message = @label.errors.full_messages.to_sentence.presence || "Translation label could not be updated."
67
+
68
+ respond_with_lesli(
69
+ turbo: [
70
+ stream_notification_danger(message),
71
+ turbo_stream.replace('lesli-babel-labels-form', partial: 'lesli_babel/labels/form')
72
+ ],
73
+ json: { errors: @label.errors.to_hash },
74
+ html: :show,
75
+ status: :unprocessable_content
76
+ )
87
77
  end
88
78
  end
89
79
 
90
80
  # DELETE /labels/1
91
81
  def destroy
92
- @label.destroy
93
- redirect_to labels_url, notice: "Label was successfully destroyed.", status: :see_other
82
+ return respond_with_not_found unless @label.found?
83
+
84
+ label = @label.result
85
+ module_id = label.bucket.module_id
86
+ label.destroy!
87
+
88
+ redirect_back(
89
+ fallback_location: labels_path(modules_id: module_id),
90
+ notice: "Translation was permanently deleted.",
91
+ status: :see_other
92
+ )
94
93
  end
95
94
 
96
95
  private
@@ -106,6 +105,25 @@ module LesliBabel
106
105
  @module = ModuleService.new(current_user, query).find(modules_id).result
107
106
  end
108
107
 
108
+ def configured_locale_name(locale)
109
+ babel_locales.find { |code, _name| code.to_s == locale.to_s }&.last
110
+ end
111
+
112
+ def update_change_timestamps(label, attributes)
113
+ changed_at = Time.current
114
+
115
+ tracked_attributes.each do |attribute|
116
+ next unless attributes.key?(attribute)
117
+ next if label[attribute] == attributes[attribute]
118
+
119
+ label["last_update_#{attribute}"] = changed_at
120
+ end
121
+ end
122
+
123
+ def tracked_attributes
124
+ %w[status context] + babel_locale_codes.map(&:to_s)
125
+ end
126
+
109
127
  # Only allow a list of trusted parameters through.
110
128
  def label_params
111
129
  params.require(:label).permit(
@@ -115,7 +133,7 @@ module LesliBabel
115
133
  :status,
116
134
  :context,
117
135
  :bucket_id,
118
- I18n.available_locales
136
+ babel_locale_codes
119
137
  )
120
138
  end
121
139
  end
@@ -32,60 +32,50 @@ Building a better future, one line of code at a time.
32
32
 
33
33
  module LesliBabel
34
34
  class TranslationsController < ApplicationController
35
- before_action :set_translation, only: []
35
+ before_action :authorize_web_deploy!, only: :deploy
36
36
 
37
37
  def deploy
38
- TranslationService.new(current_user, query).deploy
38
+ result = begin
39
+ TranslationDeployService.call
40
+ rescue StandardError => exception
41
+ return respond_with_deploy_error(exception.message)
42
+ end
43
+ errors = result.fetch(:errors)
44
+
45
+ return respond_with_deploy_error(errors.join("; "), errors) if errors.any?
39
46
 
40
47
  respond_with_lesli(
41
- :turbo => stream_notification_success("Translation deployed successfully!")
48
+ turbo: stream_notification_success("Translations deployed successfully."),
49
+ json: {
50
+ message: "Translations deployed successfully.",
51
+ files: result.fetch(:files)
52
+ },
53
+ redirect: labels_path
42
54
  )
43
55
  end
44
56
 
45
- def renovate
46
- result = TranslationsSynchronizationService.remote_sync
47
- respond_with_successful if result.success?
48
- respond_with_error if not result.success?
49
- end
50
-
51
- def sync
52
- result = TranslationsSynchronizationService.remote_sync true
53
- respond_with_successful if result.success?
54
- respond_with_error if not result.success?
55
- end
56
-
57
- def download
58
-
59
- engine = params[:engine]
60
- platform = params[:platform]
61
-
62
- translations_result = TranslationsMiddlemanService.build(engine) if platform == "middleman"
63
- translations_result = TranslationsAndroidService.build(engine) if platform == "android"
64
- translations_result = TranslationsFlutterService.build(engine) if platform == "flutter"
65
- translations_result = TranslationsIosService.build(engine) if platform == "ios"
66
- translations_result = TranslationsJsService.build() if platform == "js"
67
-
68
- return respond_with_error if translations_result.blank?
69
-
70
- zip_file = "#{platform}-#{engine}-translations.zip"
71
-
72
- LC::System::IO.zip(zip_file, translations_result.payload)
73
-
74
- redirect_to "/tmp/#{zip_file}"
75
-
76
- end
77
-
78
-
79
57
  private
80
58
 
81
- # Use callbacks to share common setup or constraints between actions.
82
- def set_translation
83
- @translation = Translation.find(params[:id])
59
+ # Web deploy is enabled outside production by default. Applications may
60
+ # explicitly authorize it in another environment through Lesli config.
61
+ def authorize_web_deploy!
62
+ return if web_deploy_allowed?
63
+
64
+ respond_with_lesli(
65
+ turbo: stream_notification_danger("Web translation deploy is disabled."),
66
+ json: { message: "Web translation deploy is disabled." },
67
+ redirect: labels_path,
68
+ status: :forbidden
69
+ )
84
70
  end
85
71
 
86
- # Only allow a trusted parameter "white list" through.
87
- def translation_params
88
- params.fetch(:translation, {})
72
+ def respond_with_deploy_error(message, errors = [message])
73
+ respond_with_lesli(
74
+ turbo: stream_notification_danger(message),
75
+ json: { message: message, errors: errors },
76
+ redirect: labels_path,
77
+ status: :unprocessable_content
78
+ )
89
79
  end
90
80
  end
91
81
  end
@@ -1,4 +1,53 @@
1
1
  module LesliBabel
2
- module LabelsHelper
3
- end
2
+ module LabelsHelper
3
+ TRANSLATION_TOKEN_PATTERN = /%\{[^}]+\}|(?<![\w:]):[a-zA-Z_][a-zA-Z0-9_]*/
4
+ STATUS_BADGE_CLASSES = {
5
+ "ok" => "bg-emerald-50 text-emerald-700 ring-emerald-200",
6
+ "need_help" => "bg-amber-50 text-amber-700 ring-amber-200",
7
+ "need_context" => "bg-sky-50 text-sky-700 ring-sky-200",
8
+ "need_translation" => "bg-rose-50 text-rose-700 ring-rose-200"
9
+ }.freeze
10
+ STATUS_BORDER_CLASSES = {
11
+ "ok" => "border-emerald-300",
12
+ "need_help" => "border-amber-300",
13
+ "need_context" => "border-sky-300",
14
+ "need_translation" => "border-rose-300"
15
+ }.freeze
16
+
17
+ def translation_field_message(label, locale)
18
+ value = label.public_send(locale).to_s
19
+ messages = []
20
+ messages << "Missing translation" if value.blank?
21
+
22
+ tokens = value.scan(TRANSLATION_TOKEN_PATTERN).uniq
23
+ messages << "Tokens: #{tokens.join(', ')}" if tokens.any?
24
+
25
+ messages.join(" · ").presence
26
+ end
27
+
28
+ def missing_translation_languages(label)
29
+ Lesli.config.babel.fetch(:locales).filter_map do |locale, language|
30
+ language if label.public_send(locale).blank?
31
+ end
32
+ end
33
+
34
+ def label_status_name(status)
35
+ return "OK" if status.to_s == "ok"
36
+
37
+ status.to_s.humanize.presence || "Not set"
38
+ end
39
+
40
+ def label_status_badge_classes(status)
41
+ classes = STATUS_BADGE_CLASSES.fetch(
42
+ status.to_s,
43
+ "bg-slate-100 text-slate-600 ring-slate-200"
44
+ )
45
+
46
+ "inline-flex items-center rounded-full px-2.5 py-1 text-xs font-semibold ring-1 ring-inset #{classes}"
47
+ end
48
+
49
+ def label_status_border_class(status)
50
+ STATUS_BORDER_CLASSES.fetch(status.to_s, "border-slate-200")
51
+ end
52
+ end
4
53
  end
@@ -30,7 +30,7 @@ Building a better future, one line of code at a time.
30
30
  // ·
31
31
  =end
32
32
  module LesliBabel
33
- class ApplicationRecord < Lesli::ApplicationLesliRecord
33
+ class ApplicationRecord < Lesli::ApplicationRecord
34
34
  self.abstract_class = true
35
35
  end
36
36
  end
@@ -8,11 +8,10 @@ module LesliBabel
8
8
 
9
9
  enum :status, {
10
10
  :ok => "ok",
11
- :disabled => "disabled",
12
11
  :need_help => "need_help",
13
12
  :need_context => "need_context",
14
13
  :need_translation => "need_translation"
15
- }
14
+ }, validate: { allow_nil: true }
16
15
 
17
16
  enum :prefix, {
18
17
 
@@ -41,6 +40,24 @@ module LesliBabel
41
40
  "ok"
42
41
  end
43
42
 
43
+ def translation_path
44
+ return self[:path] if has_attribute?(:path) && self[:path].present?
45
+
46
+ [translation_module_code, translation_bucket_code, text].join(".")
47
+ end
48
+
49
+ def translation_module_code
50
+ return self[:engine_code] if has_attribute?(:engine_code)
51
+
52
+ bucket.module.code
53
+ end
54
+
55
+ def translation_bucket_code
56
+ return self[:bucket_code] if has_attribute?(:bucket_code)
57
+
58
+ bucket.code
59
+ end
60
+
44
61
  private
45
62
 
46
63
  def prepare_label
@@ -9,9 +9,8 @@ module LesliBabel
9
9
 
10
10
  # get strings with bucket and module information
11
11
  strings = Label
12
- .joins("inner join lesli_babel_buckets on lesli_babel_buckets.id = lesli_babel_labels.bucket_id and lesli_babel_buckets.deleted_at is NULL")
13
- .joins("inner join lesli_babel_modules on lesli_babel_modules.id = lesli_babel_buckets.module_id and lesli_babel_modules.deleted_at is NULL")
14
- .where("lesli_babel_labels.status != 0 OR lesli_babel_labels.status is NULL")
12
+ .joins("inner join lesli_babel_buckets on lesli_babel_buckets.id = lesli_babel_labels.bucket_id")
13
+ .joins("inner join lesli_babel_modules on lesli_babel_modules.id = lesli_babel_buckets.module_id")
15
14
 
16
15
  # filter by specific engines
17
16
  if modules_id
@@ -40,15 +39,45 @@ module LesliBabel
40
39
  end
41
40
 
42
41
  def index params
43
- self.builder(modules_id:params[:modules_id])
42
+ labels = self.builder(modules_id:params[:modules_id])
43
+ condition = missing_condition(params)
44
+
45
+ if condition
46
+ labels = labels.where(condition)
47
+ end
48
+
49
+ labels
44
50
  .page(query[:pagination][:page])
45
51
  .per(query[:pagination][:perPage])
46
- .order(status: :desc )
47
- .order(updated_at: :desc)
52
+ .order(status: :desc, updated_at: :desc, id: :desc)
48
53
  end
49
54
 
50
55
  def show
51
56
  self.builder.find_by_id(self.resource.id)
52
57
  end
58
+
59
+ private
60
+
61
+ def missing_condition(params)
62
+ return unless params[:missing].to_s == "true"
63
+
64
+ locales = Lesli.config.babel.fetch(:locales).keys.map(&:to_s)
65
+ requested_locale = params[:locale].to_s
66
+
67
+ if requested_locale.present?
68
+ return unless locales.include?(requested_locale)
69
+
70
+ locales = [requested_locale]
71
+ end
72
+
73
+ locales
74
+ .map { |locale| missing_value_for(locale) }
75
+ .reduce { |condition, locale| condition.or(locale) }
76
+ end
77
+
78
+ def missing_value_for(locale)
79
+ column = Label.arel_table[locale]
80
+ column.eq(nil).or(column.eq(""))
81
+ end
53
82
  end
54
83
  end
@@ -0,0 +1,144 @@
1
+ require "tempfile"
2
+
3
+ module LesliBabel
4
+ # Writes exported translation documents to their official locale paths.
5
+ #
6
+ # Every document is generated and staged before a destination is replaced.
7
+ # Temporary files live beside their destinations so File.rename performs an
8
+ # atomic replacement on the same filesystem.
9
+ class TranslationDeployService
10
+ # @return [Hash] deployed file paths and export errors
11
+ # @raise [SystemCallError] when a filesystem operation fails
12
+ def self.call
13
+ new.call
14
+ end
15
+
16
+ def call
17
+ documents, errors = build_documents
18
+ return { files: [], errors: errors } if errors.any?
19
+
20
+ verify_destinations!(documents)
21
+ staged_documents = stage_documents(documents)
22
+ files = replace_destinations(staged_documents)
23
+
24
+ { files: files, errors: [] }
25
+ ensure
26
+ cleanup_temporary_files(staged_documents)
27
+ end
28
+
29
+ private
30
+
31
+ # Generate every document before performing filesystem operations
32
+ def build_documents
33
+ documents = []
34
+ errors = []
35
+
36
+ engines.each do |engine|
37
+ locales.each do |locale|
38
+ documents << TranslationExportService.call(
39
+ engine: export_engine(engine),
40
+ platform: platform_for(engine),
41
+ locale: locale
42
+ )
43
+ rescue ActiveRecord::RecordNotFound, ActiveModel::MissingAttributeError => error
44
+ errors << "#{engine[:name]}/#{locale}: #{error.message}"
45
+ end
46
+ end
47
+
48
+ [documents, errors]
49
+ end
50
+
51
+ # Root is the host application; Lesli is the core engine
52
+ def platform_for(engine)
53
+ return "rails_app" if engine[:code] == "root"
54
+ return "lesli_core" if engine[:code] == "lesli"
55
+
56
+ "lesli_engine"
57
+ end
58
+
59
+ # The host application owns main_app even though its catalog code is root
60
+ def export_engine(engine)
61
+ return engine unless engine[:code] == "root"
62
+
63
+ engine.merge(code: "main_app")
64
+ end
65
+
66
+ def engines
67
+ LesliSystem.engines.values.sort_by { |engine| engine[:name].to_s }
68
+ end
69
+
70
+ def locales
71
+ Lesli.config.babel.fetch(:locales).keys.map(&:to_s).sort
72
+ end
73
+
74
+ # Verify every destination before creating or replacing any file
75
+ def verify_destinations!(documents)
76
+ file_paths = documents.map { |document| document.fetch(:file_path) }
77
+ duplicate_path = file_paths.tally.find { |_path, count| count > 1 }&.first
78
+ raise ArgumentError, "Duplicate translation destination: #{duplicate_path}" if duplicate_path
79
+
80
+ file_paths.each do |file_path|
81
+ directory = File.dirname(file_path)
82
+
83
+ raise Errno::ENOENT, directory unless File.directory?(directory)
84
+ raise Errno::EACCES, directory unless File.writable?(directory)
85
+ raise Errno::EACCES, file_path if File.exist?(file_path) && !File.writable?(file_path)
86
+ end
87
+ end
88
+
89
+ # Write every document to a temporary file beside its destination
90
+ def stage_documents(documents)
91
+ staged_documents = []
92
+
93
+ documents.each do |document|
94
+ file_path = document.fetch(:file_path)
95
+ temporary_file = Tempfile.new(
96
+ [".#{File.basename(file_path)}-", ".tmp"],
97
+ File.dirname(file_path)
98
+ )
99
+ staged_document = {
100
+ file_path: file_path,
101
+ temporary_file: temporary_file
102
+ }
103
+ staged_documents << staged_document
104
+
105
+ temporary_file.binmode
106
+ temporary_file.write(document.fetch(:content))
107
+ temporary_file.flush
108
+ temporary_file.fsync
109
+ temporary_file.chmod(file_mode(file_path))
110
+ temporary_file.close
111
+ end
112
+
113
+ staged_documents
114
+ rescue
115
+ cleanup_temporary_files(staged_documents)
116
+ raise
117
+ end
118
+
119
+ def file_mode(file_path)
120
+ return File.stat(file_path).mode & 0o777 if File.exist?(file_path)
121
+
122
+ 0o644
123
+ end
124
+
125
+ # Rename each staged file over its destination atomically
126
+ def replace_destinations(staged_documents)
127
+ staged_documents.map do |document|
128
+ File.rename(
129
+ document.fetch(:temporary_file).path,
130
+ document.fetch(:file_path)
131
+ )
132
+ document.fetch(:file_path)
133
+ end
134
+ end
135
+
136
+ def cleanup_temporary_files(staged_documents)
137
+ Array(staged_documents).each do |document|
138
+ temporary_file = document.fetch(:temporary_file)
139
+ temporary_file.close unless temporary_file.closed?
140
+ temporary_file.unlink if File.exist?(temporary_file.path)
141
+ end
142
+ end
143
+ end
144
+ end