def test_pseudo_file(self): """Test Pseudo translation generation based on FORMATS var dict.""" for i18n_type, v in FORMATS.items(): #if i18n_type != "INI": continue # Set i18n_type for resource self.resource.i18n_type = i18n_type self.resource.save() # Set a file, resource and language for the resource handler = registry.handler_for(i18n_type) handler.bind_file(v['file']) handler.bind_resource(self.resource) handler.set_language(self.language) handler.parse_file(is_source=True) handler.save2db(is_source=True) # For each pseudo type that exists, try to generate files in the # supported i18n formats supported. for pseudo_type in settings.PSEUDO_TYPES.keys(): #if pseudo_type != "MIXED": continue # Get Pseudo type class pseudo_class = import_to_python( settings.PSEUDO_TYPE_CLASSES[pseudo_type]) # Create a PseudoType instance and set it into the handler handler.bind_pseudo_type(pseudo_class(self.resource.i18n_type)) # Compile file and check encoding handler.compile() file_content = handler.compiled_template if not isinstance(file_content, unicode): try: file_content = file_content.decode('utf-8') except UnicodeDecodeError: file_content = file_content.decode('iso-8859-1') #FIXME: We have a bug related to spaces being escaped in # .properties files. This can be dropped after fixing it. if i18n_type == 'PROPERTIES' and \ pseudo_type in ['PLANGUAGE', 'UNICODE']: file_content = file_content.replace('\\ ', ' ') # Assert expected value in the generated file for message in v['pseudo_messages'][pseudo_type]: logger.debug(file_content) logger.debug("-----------------") logger.debug(message) logger.debug(i18n_type) logger.debug(pseudo_type) logger.debug("-----------------") self.assertIn(message, file_content)
def get_i18n_handler_from_type(i18n_type): """ The same as above but takes a i18n_type as input. Useful for getting a handler from a resource. """ assert i18n_type in settings.I18N_METHODS.keys(), "I18n '%s' is not registered as a supported one." % i18n_type class_name = settings.I18N_HANDLER_CLASS_NAMES[i18n_type] return import_to_python(class_name)
def __init__(self, methods=None, handlers=None): """It initializes the variables of the registry. The variables are: methods: A dictionary of the available methods. handlers: A dictionary of the available handlers. """ self.methods = methods or settings.I18N_METHODS self.handlers = {} handlers = handlers or settings.I18N_HANDLER_CLASS_NAMES for method, klass in handlers.iteritems(): self.handlers[method] = import_to_python(klass)
def _create_validators(i18n_type, type_): """Create a generator of validators for the specific i18n_type and errors/warning check we need. Args: i18n_type: The i18n type forthe validators. type_: A string with the name of the type of the validators we need. Currently, either I18N_ERROR_VALIDATORS or I18N_WARNING_VALIDATOR. Returns: A generator with validator objects. """ type_validators = getattr(settings, type_) if i18n_type in type_validators: key = i18n_type else: key = 'DEFAULT' return (import_to_python(klass) for klass in type_validators[key])
def test_pseudo_file(self): """Test Pseudo translation generation based on FORMATS var dict.""" for i18n_type, v in FORMATS.items(): # Set i18n_type for resource self.resource.i18n_type = i18n_type self.resource.save() # Set a file, resource and language for the resource parser = get_i18n_handler_from_type(i18n_type) handler = parser(v['file'], resource=self.resource, language=self.language) handler.parse_file(is_source=True) handler.save2db(is_source=True) # For each pseudo type that exists, try to generate files in the # supported i18n formats supported. for pseudo_type in settings.PSEUDO_TYPES.keys(): # Get Pseudo type class pseudo_class = import_to_python( settings.PSEUDO_TYPE_CLASSES[pseudo_type]) # Create a PseudoType instance and set it into the handler handler.bind_pseudo_type(pseudo_class(self.resource.i18n_type)) # Compile file and check encoding handler.compile() file_content = handler.compiled_template if type(file_content) != unicode: file_content = file_content.decode('utf-8') #FIXME: We have a bug related to spaces being escaped in # .properties files. This can be dropped after fixing it. if i18n_type == 'PROPERTIES' and \ pseudo_type in ['PLANGUAGE', 'UNICODE']: file_content = file_content.replace('\\ ', ' ') # Assert expected value in the generated file self.assertTrue( v['pseudo_messages'][pseudo_type] in file_content)
def get_pseudo_class(ptype): """Return pseudo type class.""" return import_to_python(settings.PSEUDO_TYPE_CLASSES[ptype])