Skip to content

Commit f6f835f

Browse files
committed
Add source language metadata handling in merge command
Implemented logic to set the `source_language` field in resources during the merge process. The source language is determined from the first resource's metadata or defaults to "en" if not available. This enhancement ensures compatibility with xcstrings format and improves resource metadata management.
1 parent b294c93 commit f6f835f

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

langcodec-cli/src/merge.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ pub fn run_merge_command(
6666
match converter::infer_format_from_path(output.clone()) {
6767
Some(format) => {
6868
println!("Converting resources to format: {:?}", format);
69+
// Set source_language field in the resources to make sure xcstrings format would not throw an error
70+
// First, try to get the source language from the first resource if it exists; otherwise, the first resource's language
71+
// would be used as the source language. If the two checks fail, the default value "en" would be used.
72+
let source_language = codec
73+
.resources
74+
.first()
75+
.and_then(|r| r.metadata.custom.get("source_language").cloned())
76+
.unwrap_or_else(|| {
77+
codec
78+
.resources
79+
.first()
80+
.map(|r| r.metadata.language.clone())
81+
.unwrap_or("en".to_string())
82+
});
83+
84+
println!("Setting metadata.source_language to: {}", source_language);
85+
86+
codec.iter_mut().for_each(|r| {
87+
r.metadata
88+
.custom
89+
.insert("source_language".to_string(), source_language.clone());
90+
});
91+
6992
if let Err(e) = converter::convert_resources_to_format(codec.resources, &output, format)
7093
{
7194
println!("❌ Error converting resources to format");

0 commit comments

Comments
 (0)