Frank Smit avatar Frank Smit committed 0ac8964

detect_encoding can only be used on PO files.

Comments (0)

Files changed (1)

 # }}}
 # function detect_encoding() {{{
-def detect_encoding(translation_file, *args, **kwargs):
+def detect_encoding(po_file, *args, **kwargs):
     PATTERN = r'"?Content-Type:.+? charset=([\w_\-:\.]+)'
     re_u = re.compile(u(PATTERN))
     re_b = re.compile(b(PATTERN))
     close_file = False
-    if isinstance(translation_file, str):  # Only unicode string
+    if isinstance(po_file, str):  # Only unicode string
         close_file = True
-        if '\n' not in translation_file and os.path.exists(translation_file):
-            # It's a file path, check if MO or PO and open
-            if translation_file.endswith('.mo'):
-                translation_file = open(translation_file, 'rb')
-            else:
-                translation_file = open(translation_file, 'r')
+        if '\n' not in po_file and os.path.exists(po_file):
+            po_file = open(po_file, 'r')
-            # It's the contents of a PO file, put in StringIO
-            translation_file = StringIO(translation_file)
+            po_file = StringIO(po_file)
-    if hasattr(translation_file, 'mode'):
-        re_charset = re_b if translation_file.mode == 'rb' else re_u
-    elif isinstance(translation_file, StringIO):
+    if hasattr(po_file, 'mode'):
+        re_charset = re_b if po_file.mode == 'rb' else re_u
+    elif isinstance(po_file, StringIO):
         re_charset = re_u
-    elif isinstance(translation_file, BytesIO):
+    elif isinstance(po_file, BytesIO):
         re_charset = re_b
-    for line in translation_file:
+    for line in po_file:
         match =
         if match:
     # If the file object was opened here, close it
     # If not, set the position back to the beginning of the file
     if close_file:
-        translation_file.close()
+        po_file.close()
     return charset
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.