Commits

Victor Gavro committed 36d11b6

Added language code detection from html, more strict arguments validation

Comments (0)

Files changed (1)

webtranslate/translator.py

         return bool(self._translation_needed_regexp.search(u' '.join(block)))
 
 
+    def parse_language_code(self, document):
+        #in case this is part of document
+        root = document.getroottree().getroot()
+        code = root.attrib.get('lang', '') or root.attrib.get('xml:lang', '')
+        return code.split('-')[0].lower() or None
+
     def translate(self, document, target_language, source_language=None, **kwargs):
         if not isinstance(document, html.HtmlElement):
+            if not isinstance(document, basestring):
+                raise ValueError('Document must be string or lxml.html.HtmlElement')
             document = html.fromstring(document)
 
+        if not source_language:
+            #try to determine language from html
+            source_language = self.parse_language_code(document)
+
         source_blocks = self._parse_blocks(document)
         translation_map = dict.fromkeys(source_blocks)
 
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 ProjectModifiedEvent.java.
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.