def _test_1(self, dtype, i, name, source_lang, lang): pkg = self.packages[dtype][i][-1] md = factory_for_metadata(dtype)() md.from_dict(pkg[dtype], is_flat=0, opts={'unserialize-values': 1}) assert md.identifier and check_uuid(md.identifier) translator = translator_for(md, source_lang) assert translator assert IMetadataTranslator in zope.interface.providedBy(translator) # Read a translated view md_1 = translator.get(lang) assert md_1 and isinstance(md_1, type(md)) assert ITranslatedMetadata in zope.interface.providedBy(md_1) assert md_1 == md, 'Nothing is translated yet, should be identical' # Tranlate a few fields translations = {} for key in self.translatable_fields[dtype]: yf = md.get_field(key) translations[key] = ' ** TRANSLATE ** ' + yf.context.value yf.context.key = (dtype,) + key tr = translator.get_field_translator(yf) yf1 = tr.translate(lang, translations[key]) assert yf1 and yf1.context.value assert yf1.context.value == translations[key] # Re-read a translated view md_2 = translator.get(lang) assert md_2 and isinstance(md_2, type(md)) assert ITranslatedMetadata in zope.interface.providedBy(md_2) assert md_2 != md, 'Some fields should be translated' changes = list(dictdiffer.diff(md.to_dict(), md_2.to_dict())) translated_keys = set(translations.keys()) assert len(changes) > 0 for changetype, skey, (changed_from, changed_to) in changes: assert changetype == 'change' key = tuple(skey.split('.')) assert key in translations assert translations[key] == changed_to yf0, yf2 = md.get_field(key), md_2.get_field(key) assert yf0.context.value == changed_from assert yf2.context.value == changed_to assert key in translated_keys translated_keys.remove(key) assert not translated_keys, 'All keys should be consumed' pass
def _test_1(self, dtype, i, name, source_lang, lang): pkg = self.packages[dtype][i][-1] md = factory_for_metadata(dtype)() md.from_dict(pkg[dtype], is_flat=0, opts={'unserialize-values': 1}) assert md.identifier and check_uuid(md.identifier) translator = translator_for(md, source_lang) assert translator assert IMetadataTranslator in zope.interface.providedBy(translator) # Read a translated view md_1 = translator.get(lang) assert md_1 and isinstance(md_1, type(md)) assert ITranslatedMetadata in zope.interface.providedBy(md_1) assert md_1 == md, 'Nothing is translated yet, should be identical' # Tranlate a few fields translations = {} for key in self.translatable_fields[dtype]: yf = md.get_field(key) translations[key] = ' ** TRANSLATE ** ' + yf.context.value yf.context.key = (dtype, ) + key tr = translator.get_field_translator(yf) yf1 = tr.translate(lang, translations[key]) assert yf1 and yf1.context.value assert yf1.context.value == translations[key] # Re-read a translated view md_2 = translator.get(lang) assert md_2 and isinstance(md_2, type(md)) assert ITranslatedMetadata in zope.interface.providedBy(md_2) assert md_2 != md, 'Some fields should be translated' changes = list(dictdiffer.diff(md.to_dict(), md_2.to_dict())) translated_keys = set(translations.keys()) assert len(changes) > 0 for changetype, skey, (changed_from, changed_to) in changes: assert changetype == 'change' key = tuple(skey.split('.')) assert key in translations assert translations[key] == changed_to yf0, yf2 = md.get_field(key), md_2.get_field(key) assert yf0.context.value == changed_from assert yf2.context.value == changed_to assert key in translated_keys translated_keys.remove(key) assert not translated_keys, 'All keys should be consumed' pass
def __init__(self, package, source_language=None): if isinstance(package, dict): package_id = str(package['id']) else: package_id = str(package) package_id = check_uuid(package_id) if not package_id: raise ValueError('package: Expected a UUID identifier') self._package_id = package_id if not source_language and isinstance(package, dict): source_language = package.get('language') if not source_language: source_language = pylons.config['ckan.locale_default'] log1.info('package %s: source-language is missing and cannot be deduced' % ( package_id)) self._source_language = check_language(source_language) self._defer_commit = False