1 
2 
3 #include "studiox_includes.h"
4 #include "xml_writer.h"
5 #include "xml_reader.h"
6 #include "xliff_read_write.h"
7 #include "config_languages_dlg.h"
8 
9 ///////////////////////////////////////////////////////////////////////////////
xliff_read_write()10 xliff_read_write::xliff_read_write()
11 {
12 }
13 
14 ///////////////////////////////////////////////////////////////////////////////
ExportLanguage(studiox_project * project)15 BOOL xliff_read_write::ExportLanguage(studiox_project *project)
16 {
17     int index;
18     int src_language = project->mHeader.string_export_src;
19     int target_language = project->mHeader.string_export_target;
20     CString tag_data;
21     CString src_data;
22     CString pathname = project->mHeader.string_export_path;
23     pathname += "\\";
24     pathname += project->mHeader.string_export_filename;
25 
26     xml_writer *writer = new xml_writer();
27 
28     GotoProjectDirectory();
29     if (!writer->OpenFile(CString(pathname)))
30     {
31         delete writer;
32         return FALSE;
33     }
34     writer->WriteHeader(NULL);
35 
36     int display = GetProjectView()->GetActiveDisplay();
37     string_table *table = project->mDisplays[display].stable;
38 
39     if (project->mHeader.string_export_version >= 2)
40     {
41         tag_data.Format(_T("xmlns=\"urn:oasis:names:tc:xliff:document:2.0\" version=\"2.0\"\n  srcLang =\"%s\" trgLang=\"%s\""),
42                 config_languages_dlg::GetLanguageId(src_language),
43                 config_languages_dlg::GetLanguageId(target_language));
44 
45         writer->OpenTag("xliff", tag_data);
46         tag_data.Format(_T("id=\"f1\" original=\"%s\\%s.gxp\""),
47             project->mHeader.project_path,
48             project->mHeader.project_name);
49 
50         // KGM FIXME: Are we going to keep one string table or one
51         // table for each display? Currently we are keeping a table for each display
52         // int the project.... not sure but I'm not consistent about this currently.
53 
54         writer->OpenTag("file", tag_data);
55 
56         src_language = config_languages_dlg::GetStringTableIndexFromLanguageIndex(project, src_language);
57         target_language = config_languages_dlg::GetStringTableIndexFromLanguageIndex(project, target_language);
58 
59         if (src_language == -1)
60         {
61             delete writer;
62             return FALSE;
63         }
64 
65         for (index = 0; index < table->CountStrings(); index++)
66         {
67             src_data = table->GetString(index, src_language);
68             if (!src_data.IsEmpty())
69             {
70                 tag_data.Format(_T("id=\"%d\" name=\"%s\""), index, table->GetStringId(index));
71                 writer->OpenTag("unit", tag_data);
72                 if (!(table->GetNotes(index).IsEmpty()))
73                 {
74                     //write notes
75                     writer->OpenTag("notes");
76                     writer->WriteString("note", table->GetNotes(index));
77                     writer->CloseTag("notes");
78                 }
79                 writer->OpenTag("segment");
80                 writer->WriteString("source", src_data);
81                 if (target_language >= 0)
82                 {
83                     writer->WriteString("target", table->GetString(index, target_language));
84                 }
85                 writer->CloseTag("segment");
86                 writer->CloseTag("unit");
87             }
88         }
89     }
90     else
91     {
92         tag_data = "version=\"1.2\" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\"";
93         writer->OpenTag("xliff", tag_data);
94 
95         tag_data.Format(_T("original=\"%s\\%s.gxp\" source-language=\"%s\" target-language=\"%s\" datatype=\"plaintext\""),
96                 project->mHeader.project_path,
97                 project->mHeader.project_name,
98                 config_languages_dlg::GetLanguageId(src_language),
99                 config_languages_dlg::GetLanguageId(target_language));
100 
101         writer->OpenTag("file", tag_data);
102         writer->OpenTag("body");
103 
104         src_language = config_languages_dlg::GetStringTableIndexFromLanguageIndex(project, src_language);
105         target_language = config_languages_dlg::GetStringTableIndexFromLanguageIndex(project, target_language);
106 
107         for (index = 0; index < table->CountStrings(); index++)
108         {
109             src_data = table->GetString(index, src_language);
110             if (!src_data.IsEmpty())
111             {
112                 tag_data.Format(_T("id=\"%s\""), table->GetStringId(index));
113                 writer->OpenTag("trans-unit", tag_data);
114                 writer->WriteString("source", src_data);
115                 if (target_language >= 0)
116                 {
117                     writer->WriteString("target", table->GetString(index, target_language));
118                 }
119 
120                 if (!(table->GetNotes(index).IsEmpty()))
121                 {
122                     //write notes
123                     writer->WriteString("note", table->GetNotes(index));
124                 }
125                 writer->CloseTag("trans-unit");
126             }
127         }
128         writer->CloseTag("body");
129     }
130     writer->CloseTag("file");
131     writer->CloseTag("xliff");
132     writer->CloseFile();
133     delete writer;
134     return TRUE;
135 }
136 
137 ///////////////////////////////////////////////////////////////////////////////
ImportXliffFile(studiox_project * project,CString & pathname)138 BOOL xliff_read_write::ImportXliffFile(studiox_project *project, CString &pathname)
139 {
140     CString src_language;
141     CString target_language;
142     CString string_id;
143     CString src_string;
144     CString target_string;
145     CString notes;
146 
147     xml_reader reader;
148     int language_index;
149     int target_table_column;
150     int src_table_column;
151 
152     GotoProjectDirectory();
153     if (!reader.ReadFile(pathname))
154     {
155         return FALSE;
156     }
157 
158     int display = GetProjectView()->GetActiveDisplay();
159     string_table *table = project->mDisplays[display].stable;
160 
161     // Get the version information
162 
163     if (!reader.EnterSection("xliff"))
164     {
165         return FALSE;
166     }
167 
168     double version = reader.GetTagValueFloat("version", -1.0);
169     if (version < 0)
170     {
171         return false;
172     }
173     if (version >= 2.0)
174     {
175         if (!reader.GetTagString("srcLang", src_language) ||
176             !reader.GetTagString("trgLang", target_language))
177         {
178             return FALSE;
179         }
180 
181         language_index = config_languages_dlg::GetLanguageIdIndex(src_language);
182         src_table_column = config_languages_dlg::GetStringTableIndexFromLanguageIndex(project, language_index);
183 
184         language_index = config_languages_dlg::GetLanguageIdIndex(target_language);
185         target_table_column = config_languages_dlg::GetStringTableIndexFromLanguageIndex(project, language_index);
186 
187         if (src_table_column == -1)
188         {
189             return FALSE;
190         }
191         if (target_table_column == -1)
192         {
193             // the translation language doesn't exist in our project, so add it
194             //target_table_column = AddNewLanguage(language_index);
195             target_table_column = project->AddLanguage(language_index);
196         }
197 
198         if (reader.EnterSection("file"))
199         {
200             while(reader.EnterSection("unit"))
201             {
202                 reader.GetTagString("name", string_id);
203 
204                 if (reader.EnterSection("notes"))
205                 {
206                     reader.ReadString("note", notes);
207                     reader.CloseSection(TRUE, TRUE);
208                 }
209                 else
210                 {
211                     notes = "";
212                 }
213 
214                 while (reader.EnterSection("segment"))
215                 {
216                     reader.ReadString("source", src_string);
217                     reader.ReadString("target", target_string);
218 
219                     table->ImportString(string_id, src_string, target_string, notes, src_table_column, target_table_column);
220                     reader.CloseSection(TRUE, TRUE);
221                 }
222                 reader.CloseSection(TRUE, TRUE);
223             }
224             reader.CloseSection();
225         }
226     }
227     else
228     {
229         // version 1.2- The file tag contains language info, and the body contains trans-unit tags:
230 
231         if (reader.EnterSection("file"))
232         {
233             reader.GetTagString("source-language", src_language);
234             reader.GetTagString("target-language", target_language);
235 
236             language_index = config_languages_dlg::GetLanguageIdIndex(src_language);
237             src_table_column = config_languages_dlg::GetStringTableIndexFromLanguageIndex(project, language_index);
238 
239             language_index = config_languages_dlg::GetLanguageIdIndex(target_language);
240             target_table_column = config_languages_dlg::GetStringTableIndexFromLanguageIndex(project, language_index);
241 
242             if (src_table_column == -1)
243             {
244                 return FALSE;
245             }
246             if (target_table_column == -1)
247             {
248                 target_table_column = project->AddLanguage(language_index);
249             }
250 
251             if (reader.EnterSection("body"))
252             {
253                 while (reader.EnterSection("trans-unit"))
254                 {
255                     reader.GetTagString("id", string_id);
256                     reader.ReadString("source", src_string);
257                     reader.ReadString("target", target_string);
258                     reader.ReadString("note", notes);
259 
260                     table->ImportString(string_id, src_string, target_string, notes, src_table_column, target_table_column);
261 
262                     reader.CloseSection(TRUE, TRUE);
263                 }
264                 reader.CloseSection();
265             }
266             reader.CloseSection();
267         }
268     }
269     reader.CloseSection();
270     table->Sort();
271     table->UpdateGuixLanguageTable();
272     return TRUE;
273 }