<s> import collections <EOL> import six <EOL> from autotranslate . compat import goslate , googleapiclient <EOL> from django . conf import settings <EOL> class BaseTranslatorService : <EOL> """<STR_LIT>""" <EOL> def translate_string ( self , text , target_language , source_language = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def translate_strings ( self , strings , target_language , source_language = '<STR_LIT>' , optimized = True ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> class GoSlateTranslatorService ( BaseTranslatorService ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> assert goslate , '<STR_LIT>' <EOL> self . service = goslate . Goslate ( ) <EOL> def translate_string ( self , text , target_language , source_language = '<STR_LIT>' ) : <EOL> assert isinstance ( text , six . string_types ) , '<STR_LIT>' <EOL> return self . service . translate ( text , target_language , source_language ) <EOL> def translate_strings ( self , strings , target_language , source_language = '<STR_LIT>' , optimized = True ) : <EOL> assert isinstance ( strings , collections . Iterable ) , '<STR_LIT>' <EOL> translations = self . service . translate ( strings , target_language , source_language ) <EOL> return translations if optimized else [ _ for _ in translations ] <EOL> class GoogleAPITranslatorService ( BaseTranslatorService ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , max_segments = <NUM_LIT> ) : <EOL> assert googleapiclient , '<STR_LIT>' <EOL> self . developer_key = getattr ( settings , '<STR_LIT>' , None ) <EOL> assert self . developer_key , ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> from googleapiclient . discovery import build <EOL> self . service = build ( '<STR_LIT>' , '<STR_LIT>' , developerKey = self . developer_key ) <EOL> self . max_segments = max_segments <EOL> self . translated_strings = [ ] <EOL> def translate_string ( self , text , target_language , source_language = '<STR_LIT>' ) : <EOL> assert isinstance ( text , six . string_types ) , '<STR_LIT>' <EOL> response = self . service . translations ( ) . list ( source = source_language , target = target_language , q = [ text ] ) . execute ( ) <EOL> return response . get ( '<STR_LIT>' ) . pop ( <NUM_LIT:0> ) . get ( '<STR_LIT>' ) <EOL> def translate_strings ( self , strings , target_language , source_language = '<STR_LIT>' , optimized = True ) : <EOL> assert isinstance ( strings , collections . MutableSequence ) , '<STR_LIT>' <EOL> assert not optimized , '<STR_LIT>' <EOL> if len ( strings ) <= self . max_segments : <EOL> setattr ( self , '<STR_LIT>' , getattr ( self , '<STR_LIT>' , [ ] ) ) <EOL> response = self . service . translations ( ) . list ( source = source_language , target = target_language , q = strings ) . execute ( ) <EOL> self . translated_strings . extend ( [ t . get ( '<STR_LIT>' ) for t in response . get ( '<STR_LIT>' ) ] ) <EOL> return self . translated_strings <EOL> else : <EOL> self . translate_strings ( strings [ <NUM_LIT:0> : self . max_segments ] , target_language , source_language , optimized ) <EOL> _translated_strings = self . translate_strings ( strings [ self . max_segments : ] , <EOL> target_language , source_language , optimized ) <EOL> self . translated_strings = [ ] <EOL> return _translated_strings </s>
