forked from JeremyDurnell/locbaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceTextWriter.cs
More file actions
151 lines (121 loc) · 4.57 KB
/
ResourceTextWriter.cs
File metadata and controls
151 lines (121 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//---------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Description: ResourceTextWriter class
// It writes values to a CSV file or tab-separated TXT file
//
//---------------------------------------------------------------------------
using System;
using System.IO;
using System.Text;
using System.Resources;
using System.Collections;
using System.Globalization;
using System.Diagnostics;
using System.Windows.Markup.Localizer;
namespace BamlLocalization
{
/// <summary>
/// the class that writes to a text file either tab delimited or comma delimited.
/// </summary>
internal sealed class ResourceTextWriter : ITranslationWriter, IDisposable
{
//-------------------------------
// constructor
//-------------------------------
internal ResourceTextWriter(TranslationFileType fileType, Stream output)
{
_delimiter = LocBamlConst.GetDelimiter(fileType);
if (output == null)
throw new ArgumentNullException("output");
// show utf8 byte order marker
UTF8Encoding encoding = new UTF8Encoding(true);
_writer = new StreamWriter(output, encoding);
_firstColumn = true;
}
#region internal methods
//-----------------------------------
// Internal methods
//-----------------------------------
internal void WriteColumn(string value)
{
if (value == null)
value = string.Empty;
// if it contains delimeter, quote, newline, we need to escape them
if (value.IndexOfAny(new char[]{'\"', '\r', '\n', _delimiter}) >= 0)
{
// make a string builder at the minimum required length;
StringBuilder builder = new StringBuilder(value.Length + 2);
// put in the opening quote
builder.Append('\"');
// double quote each quote
for (int i = 0; i < value.Length; i++)
{
builder.Append(value[i]);
if (value[i] == '\"')
{
builder.Append('\"');
}
}
// put in the closing quote
builder.Append('\"');
value = builder.ToString();
}
if (!_firstColumn)
{
// if we are not the first column, we write delimeter
// to seperate the new cell from the previous ones.
_writer.Write(_delimiter);
}
else
{
_firstColumn = false; // set false
}
_writer.Write(value);
}
internal void EndLine()
{
// write a new line
_writer.WriteLine();
// set first column to true
_firstColumn = true;
}
internal void Close()
{
if (_writer != null)
{
_writer.Close();
}
}
#endregion
void IDisposable.Dispose()
{
Close();
}
public void WriteResource(string bamlStreamName, string resourceKey, BamlLocalizableResource resource)
{
// column 1: baml stream name
WriteColumn(bamlStreamName);
// column 2: localizable resource key
WriteColumn(resourceKey);
// column 3: localizable resource's category
WriteColumn(resource.Category.ToString());
// column 4: localizable resource's readability
WriteColumn(resource.Readable.ToString());
// column 5: localizable resource's modifiability
WriteColumn(resource.Modifiable.ToString());
// column 6: localizable resource's localization comments
WriteColumn(resource.Comments);
// column 7: localizable resource's content
WriteColumn(resource.Content);
// Done. finishing the line
EndLine();
}
#region private members
private char _delimiter;
private TextWriter _writer;
private bool _firstColumn;
#endregion
}
}