Skip to content

Commit 6f06e9c

Browse files
committed
Add support to split keys on a certain charater for yaml and rails format
1 parent 74849b7 commit 6f06e9c

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

app/javascript/components/forms/AddEditExportConfigForm.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,14 @@ export function AddEditExportConfigForm(props: IAddEditExportConfigFormProps) {
334334
</Select>
335335
</Form.Item>
336336

337-
{selectedFileFormat?.attributes.format === "json" && (
337+
{(selectedFileFormat?.attributes.format === "json" ||
338+
selectedFileFormat?.attributes.format === "yaml" ||
339+
selectedFileFormat?.attributes.format === "rails") && (
338340
<>
339341
<h3>Split keys on</h3>
340342
<p>
341-
Provide a string upon which the JSON keys are split and grouped together. This way you
342-
can created nested JSON.
343+
Provide a string upon which the keys are split and grouped together. This way you can
344+
create nested structures.
343345
</p>
344346
<Form.Item name="splitOn">
345347
<Input placeholder="For example: ." />

app/models/export_config.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,24 @@ def yaml(
528528
end
529529
end
530530

531+
final_data = converted_data
532+
533+
# If the export config has a split_on specified split it.
534+
unless self.split_on.nil?
535+
final_data = {}
536+
converted_data.each do |key, value|
537+
splitted = key.split(self.split_on)
538+
deep_set(final_data, value, *splitted)
539+
end
540+
end
541+
531542
language_file = Tempfile.new(language.id.to_s)
532543
data = {}
533544

534545
if group_by_language_and_country_code
535-
data[language.language_tag] = converted_data
546+
data[language.language_tag] = final_data
536547
else
537-
data = converted_data
548+
data = final_data
538549
end
539550

540551
yaml = YAML.dump(data.deep_stringify_keys)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
de-AT:
3+
a: b
4+
x:
5+
y:
6+
z: d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
a:
3+
zero: zero text
4+
one: one text
5+
two: two text
6+
few: few text
7+
many: many text
8+
other: b
9+
c:
10+
zero: zero text
11+
one: one text
12+
two: two text
13+
few: few text
14+
many: many text
15+
other: d
16+
x:
17+
y:
18+
z: '123'

spec/models/export_config_spec.rb

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,20 +494,21 @@ def export_data_value(text, pluralization_enabled: false, empty_plurals: false)
494494

495495
# YAML
496496
context 'when file format is yaml' do
497-
let(:export_config) do
497+
it 'create yaml file content from parsed data' do
498498
export_config = ExportConfig.new
499499
export_config.file_format = FileFormat.find_by!(format: 'yaml')
500500
export_config.file_path = 'my_file_path'
501-
export_config
502-
end
503501

504-
it 'create yaml file content from parsed data' do
505502
files = export_config.files(@language, { 'a' => export_data_value('b'), 'c' => export_data_value('d') })
506503
files[0][:file].open
507504
expect(files[0][:file].read).to match_snapshot('create_yaml_file_content')
508505
end
509506

510507
it 'create yaml file content from plural data' do
508+
export_config = ExportConfig.new
509+
export_config.file_format = FileFormat.find_by!(format: 'yaml')
510+
export_config.file_path = 'my_file_path'
511+
511512
files =
512513
export_config.files(
513514
@language,
@@ -520,22 +521,49 @@ def export_data_value(text, pluralization_enabled: false, empty_plurals: false)
520521
files[0][:file].open
521522
expect(files[0][:file].read).to match_snapshot('create_yaml_file_content_plural')
522523
end
524+
525+
it 'create yaml file content from plural data with split on' do
526+
export_config = ExportConfig.new
527+
export_config.file_format = FileFormat.find_by!(format: 'yaml')
528+
export_config.file_path = 'my_file_path'
529+
export_config.split_on = '.'
530+
531+
files =
532+
export_config.files(
533+
@language,
534+
{
535+
'a' => export_data_value('b', pluralization_enabled: true),
536+
'c' => export_data_value('d', pluralization_enabled: true),
537+
'x.y.z' => export_data_value('123')
538+
}
539+
)
540+
files[0][:file].open
541+
expect(files[0][:file].read).to match_snapshot('create_yaml_file_content_plural_split_on')
542+
end
523543
end
524544

525545
# Rails
526546
context 'when file format is rails' do
527-
let(:export_config) do
547+
it 'create rails file content from parsed data' do
528548
export_config = ExportConfig.new
529549
export_config.file_format = FileFormat.find_by!(format: 'rails')
530550
export_config.file_path = 'my_file_path'
531-
export_config
532-
end
533551

534-
it 'create rails file content from parsed data' do
535552
files = export_config.files(@language, { 'a' => export_data_value('b'), 'c' => export_data_value('d') })
536553
files[0][:file].open
537554
expect(files[0][:file].read).to match_snapshot('create_rails_file_content')
538555
end
556+
557+
it 'create rails file content from parsed data with split on' do
558+
export_config = ExportConfig.new
559+
export_config.file_format = FileFormat.find_by!(format: 'rails')
560+
export_config.file_path = 'my_file_path'
561+
export_config.split_on = '.'
562+
563+
files = export_config.files(@language, { 'a' => export_data_value('b'), 'x.y.z' => export_data_value('d') })
564+
files[0][:file].open
565+
expect(files[0][:file].read).to match_snapshot('create_rails_file_content_split_on')
566+
end
539567
end
540568

541569
# TOML

0 commit comments

Comments
 (0)