Class: I18nContextGenerator::Writers::SwiftWriter
- Inherits:
-
Object
- Object
- I18nContextGenerator::Writers::SwiftWriter
- Includes:
- Helpers
- Defined in:
- lib/i18n_context_generator/writers/swift_writer.rb
Overview
Writer that updates comment: parameters in Swift localization calls Supports NSLocalizedString, String(localized:), Text(), and custom functions
Instance Method Summary collapse
-
#initialize(functions: nil, context_prefix: 'Context: ', context_mode: 'replace') ⇒ SwiftWriter
constructor
A new instance of SwiftWriter.
-
#update_file(path, results_by_key) ⇒ Object
Update a single Swift file with context comments.
-
#write_to_source_files(results, source_paths, ignore_patterns: []) ⇒ Object
Write context back to all Swift files that contain the keys.
Methods included from Helpers
#find_swift_files, #result_matches_source_path?, #skip_description?, #writable_result?
Constructor Details
#initialize(functions: nil, context_prefix: 'Context: ', context_mode: 'replace') ⇒ SwiftWriter
Returns a new instance of SwiftWriter.
16 17 18 19 20 |
# File 'lib/i18n_context_generator/writers/swift_writer.rb', line 16 def initialize(functions: nil, context_prefix: 'Context: ', context_mode: 'replace') @localization_syntax = LocalizationSyntax.new(swift_functions: functions) @context_prefix = context_prefix @context_mode = context_mode end |
Instance Method Details
#update_file(path, results_by_key) ⇒ Object
Update a single Swift file with context comments
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/i18n_context_generator/writers/swift_writer.rb', line 40 def update_file(path, results_by_key) return unless File.exist?(path) content = File.read(path) original_content = content.dup updated = false results_by_key.each do |key, result| next unless writable_result?(result) new_content = update_comment_for_key(content, key, result.description) if new_content != content content = new_content updated = true end end if updated && content != original_content AtomicFile.replace(path, content) true else false end end |
#write_to_source_files(results, source_paths, ignore_patterns: []) ⇒ Object
Write context back to all Swift files that contain the keys
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/i18n_context_generator/writers/swift_writer.rb', line 25 def write_to_source_files(results, source_paths, ignore_patterns: []) results_by_key = results.to_h { |r| [r.key, r] } source_paths.each do |source_path| swift_files = find_swift_files(source_path, ignore_patterns: ignore_patterns) swift_files.each do |swift_file| update_file(swift_file, results_by_key) end end end |