Skip to content

Commit f94ba49

Browse files
committed
Update extract-i18n for extraction-style string management
- Because we're extracting strings, we need them to be marked correctly and use our translation helper methods - We also don't need to create a file, so let's not - Defaulting to double quotes to avoid writing code to detect inner quotes; we can let RuboCop fix it when we run the tool on our repo - ignore rubymine files
1 parent 50f0f0e commit f94ba49

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
/pkg/
77
/spec/reports/
88
/tmp/
9+
/.idea/
910
spec/.examples.txt
1011
Gemfile.lock

lib/extract_i18n/adapters/ruby_adapter.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def on_dstr(node)
5353
else
5454
inner_source = i.children[0].loc.expression.source.gsub(/^#\{|}$/, '')
5555
interpolate_key = ExtractI18n.key(inner_source)
56-
out_string += "%{#{interpolate_key}}"
56+
# because we're using ICU, interpolation happens just with curly braces, no %
57+
out_string += "{#{interpolate_key}}"
5758
interpolate_arguments[interpolate_key] = inner_source
5859
end
5960
end
@@ -77,7 +78,8 @@ def on_str(node)
7778

7879
def ask_and_continue(i18n_key:, i18n_string:, interpolate_arguments: {}, node:)
7980
change = ExtractI18n::SourceChange.new(
80-
i18n_key: "#{@file_key}.#{i18n_key}",
81+
# we want the key to be the string itself so that we can extract it later
82+
i18n_key: "#{i18n_string}",
8183
i18n_string: i18n_string,
8284
interpolate_arguments: interpolate_arguments,
8385
source_line: node.location.expression.source_line,

lib/extract_i18n/file_processor.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def run
2626
puts Diffy::Diff.new(original_content, result, context: 1).to_s(:color)
2727
if PROMPT.yes?("Save changes?")
2828
File.write(@file_path, result)
29-
update_i18n_yml_file
29+
# We are using a text extraction pipeline, so don't need this file
30+
# update_i18n_yml_file
3031
puts PASTEL.green("Saved #{@file_path}")
3132
end
3233
end

lib/extract_i18n/source_change.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def initialize(
2828
interpolate_arguments:,
2929
source_line:,
3030
remove:,
31-
t_template: %{I18n.t("%s"%s)},
31+
# we are using our own _t method by default
32+
t_template: %{_t("%s"%s)},
3233
interpolation_type: :ruby
3334
)
3435
@i18n_string = i18n_string
@@ -56,7 +57,8 @@ def format
5657
unless @source_line.include?("\n")
5758
s += "\n"
5859
end
59-
s += PASTEL.cyan("add i18n: ") + PASTEL.blue("#{@key}: #{@i18n_string}")
60+
# We are using a text extraction pipeline, so we don't need this line
61+
# s += PASTEL.cyan("add i18n: ") + PASTEL.yellow("#{@key}: #{@i18n_string}")
6062
s
6163
end
6264

@@ -66,6 +68,12 @@ def i18n_t(relative: false)
6668
else
6769
key
6870
end
71+
# let's not have dangling commas for strings with no arguments
72+
@t_template = if i18n_arguments_string.length > 0
73+
%{_t("%s"%s)}
74+
else
75+
%{_t("%s")}
76+
end
6977
sprintf(@t_template, i18n_key, i18n_arguments_string)
7078
end
7179

0 commit comments

Comments
 (0)