47
46
# Get the newline style that is used in the English Singular.
48
self.newline_style = self._getNewlineStyle(english_singular)
47
self.newline_style = self._getNewlineStyle(
48
english_singular, 'Original')
51
def _getNewlineStyle(cls, text):
50
def _getNewlineStyle(self, text, text_name):
52
51
"""Find out which newline style is used in text."""
53
"%s text (%r) mixes different newline markers." % (
54
56
# To avoid confusing the single-character newline styles for mac and
55
57
# unix with the two-character windows one, remove the windows-style
56
58
# newlines from the text and use that text to search for the other
58
stripped_text = text.replace(cls.windows_style, u'')
60
stripped_text = text.replace(self.windows_style, u'')
59
61
if text != stripped_text:
60
62
# Text contains windows style new lines.
61
style = cls.windows_style
63
style = self.windows_style
63
for one_char_style in (cls.mac_style, cls.unix_style):
65
for one_char_style in (self.mac_style, self.unix_style):
64
66
if one_char_style in stripped_text:
65
67
if style is not None:
66
return cls.mixed_style
68
raise MixedNewlineMarkersError(error_message)
67
69
style = one_char_style
134
136
def normalizeNewlines(self, translation_text):
135
137
"""Return 'translation_text' with newlines sync with english_singular.
137
Raises an exception if the text has mixed newline styles.
139
139
if self.newline_style is None:
140
140
# No newlines in the English singular, so we have nothing to do.
141
141
return translation_text
143
# Get the style that is used in the given text.
144
translation_newline_style = self._getNewlineStyle(translation_text)
146
if translation_newline_style == self.mixed_style:
147
# The translation has mixed newlines in it; that is not allowed.
148
raise MixedNewlineMarkersError(
149
"Translations text (%r) mixes different newline markers." %
143
# Get the style that uses the given text.
144
translation_newline_style = self._getNewlineStyle(
145
translation_text, 'Translations')
152
147
if translation_newline_style is None:
153
# The translation text doesn't contain any newlines, so there is
154
# nothing for us to do.
148
# We don't need to do anything, the text is not changed.
155
149
return translation_text
157
if self.newline_style is self.mixed_style:
158
# The original has mixed newlines (some very old data are like
159
# this, new data with mixed newlines are rejected), so we're just
160
# going to punt and normalize to unix style.
161
return translation_text.replace(
162
translation_newline_style, self.unix_style)
164
# Otherwise the translation text should be normalized to use the
165
# same newline style as the original.
166
return translation_text.replace(
167
translation_newline_style, self.newline_style)
170
def sanitize_translations(
151
# Fix the newline chars.
152
return translation_text.replace(
153
translation_newline_style, self.newline_style)
156
def sanitize_translations_from_webui(
171
157
english_singular, translations, pluralforms):
172
158
"""Sanitize `translations` using sanitize_translation.
204
190
return sanitized_translations
207
def sanitize_translations_from_import(
208
english_singular, translations, pluralforms):
209
# At import time we want to ensure that the english_singular does not
210
# contain mixed newline styles.
211
if Sanitizer._getNewlineStyle(english_singular) is Sanitizer.mixed_style:
212
raise MixedNewlineMarkersError(
213
"Original text (%r) mixes different newline markers." %
215
return sanitize_translations(english_singular, translations, pluralforms)
218
def sanitize_translations_from_webui(
219
english_singular, translations, pluralforms):
220
return sanitize_translations(english_singular, translations, pluralforms)
192
# There will be a different function for translation coming from imports but
193
# for now it is identical to the one used in browser code.
194
sanitize_translations_from_import = sanitize_translations_from_webui