예제 #1
0
    def test_duplicate_translations_are_discarded_when_linking(self):
        folder_uhoh = create(Builder('folder').titled('uhoh'))

        self.items = self.items[:2]
        self.items.append({
            '_path': '/uhoh',
            '_canonicalTranslation': False,
            '_translationOf': '/de',
            'language': 'en',
        })
        items = self._run_transmogrifier()
        self.assertEqual(3, len(items))

        uhoh_manager = ITranslationManager(folder_uhoh)
        manager = ITranslationManager(self.folder_en)
        uhoh_manager.update()
        manager.update()

        self.assertEqual('en', ILanguage(folder_uhoh).get_language())
        self.assertEqual(1, len(uhoh_manager.get_translated_languages()))

        languages = manager.get_translated_languages()
        self.assertEqual(2, len(languages))
        self.assertIn('de', languages)
        self.assertIn('en', languages)
def translated_urls(context):
    manager = ITranslationManager(context)
    translated_languages = manager.get_translated_languages()
    content_language = ILanguage(context).get_language()
    if content_language in translated_languages:
        translated_languages.remove(content_language)
    languages = []
    for lang in translated_languages:
        languages.append(SimpleVocabulary.createTerm(lang, lang, \
            manager.get_translation(lang).absolute_url()))
    return SimpleVocabulary(languages)
예제 #3
0
def translated_urls(context):
    manager = ITranslationManager(context)
    translated_languages = manager.get_translated_languages()
    content_language = ILanguage(context).get_language()
    if content_language in translated_languages:
        translated_languages.remove(content_language)
    languages = []
    for lang in translated_languages:
        languages.append(SimpleVocabulary.createTerm(lang, lang, \
            manager.get_translation(lang).absolute_url()))
    return SimpleVocabulary(languages)
def deletable_languages(context):
    manager = ITranslationManager(context)
    translated_languages = manager.get_translated_languages()
    language_tool = getToolByName(context, 'portal_languages')
    language_infos = language_tool.getAvailableLanguages()
    content_language = ILanguage(context).get_language()
    languages = []
    for lang in translated_languages:
        if lang not in content_language:
            languages.append(SimpleVocabulary.createTerm(lang, lang, \
                language_infos[lang].get('name', lang)))
    return SimpleVocabulary(languages)
예제 #5
0
def deletable_languages(context):
    manager = ITranslationManager(context)
    translated_languages = manager.get_translated_languages()
    language_tool = getToolByName(context, 'portal_languages')
    language_infos = language_tool.getAvailableLanguages()
    content_language = ILanguage(context).get_language()
    languages = []
    for lang in translated_languages:
        if lang not in content_language:
            native = language_infos[lang].get('native', None)
            name = language_infos[lang].get('name', lang)
            languages.append(SimpleVocabulary.createTerm(lang, lang, \
                native or name))
    return SimpleVocabulary(languages)
예제 #6
0
def translated_urls(context):
    manager = ITranslationManager(context)
    translated_languages = manager.get_translated_languages()
    content_language = ILanguage(context).get_language()
    if content_language in translated_languages:
        translated_languages.remove(content_language)
    languages = []
    for lang in translated_languages:
        translation = manager.get_restricted_translation(lang)
        if translation is not None:
            term = SimpleVocabulary.createTerm(lang, lang,
                                               translation.absolute_url())
            languages.append(term)
    return SimpleVocabulary(languages)
예제 #7
0
def translated_languages(context):
    language_tool = getToolByName(context, 'portal_languages')
    language_infos = language_tool.getAvailableLanguages()
    manager = ITranslationManager(context)
    translated_languages = manager.get_translated_languages()
    content_language = ILanguage(context).get_language()
    if content_language in translated_languages:
        translated_languages.remove(content_language)
    languages = []
    for lang in translated_languages:
        native = language_infos[lang].get('native', None)
        name = language_infos[lang].get('name', lang)
        languages.append(SimpleVocabulary.createTerm(lang, lang, \
            native or name))
    return SimpleVocabulary(languages)
def untranslated_languages(context):
    language_tool = getToolByName(context, 'portal_languages')
    language_infos = language_tool.getAvailableLanguages()
    default_language = language_tool.getDefaultLanguage()
    available_portal_languages = language_tool.supported_langs
    manager = ITranslationManager(context)
    translated_languages = manager.get_translated_languages()
    content_language = ILanguage(context).get_language()
    filter_default = (content_language == LANGUAGE_INDEPENDENT)
    languages = []
    for lang in available_portal_languages:
        if lang not in translated_languages:
            if not (filter_default and lang == default_language):
                languages.append(SimpleVocabulary.createTerm(lang, lang, \
                    language_infos[lang].get('name', lang)))
    return SimpleVocabulary(languages)
예제 #9
0
def untranslated_languages(context):
    language_tool = getToolByName(context, 'portal_languages')
    language_infos = language_tool.getAvailableLanguages()
    default_language = language_tool.getDefaultLanguage()
    available_portal_languages = language_tool.supported_langs
    manager = ITranslationManager(context)
    translated_languages = manager.get_translated_languages()
    content_language = ILanguage(context).get_language()
    filter_default = (content_language == LANGUAGE_INDEPENDENT)
    languages = []
    for lang in available_portal_languages:
        if lang not in translated_languages:
            native = language_infos[lang].get('native', None)
            name = language_infos[lang].get('name', lang)
            languages.append(SimpleVocabulary.createTerm(lang, lang, \
               native or name))
    return SimpleVocabulary(languages)
def translated_languages(context):
    language_tool = getToolByName(context, 'portal_languages')
    language_infos = language_tool.getAvailableLanguages()
    manager = ITranslationManager(context)
    # take care to filter out translated contents
    # wich do no have supported language information
    translated_languages = [a
                            for a in manager.get_translated_languages()
                            if a in language_infos]
    content_language = ILanguage(context).get_language()
    if content_language in translated_languages:
        translated_languages.remove(content_language)
    languages = []
    for lang in translated_languages:
        native = language_infos[lang].get('native', None)
        name = language_infos[lang].get('name', lang)
        languages.append(SimpleVocabulary.createTerm(lang, lang, \
            native or name))
    return SimpleVocabulary(languages)
예제 #11
0
    def translate_this(self, action):
        status = IStatusMessage(self.request)
        context = Acquisition.aq_inner(self.context)
        data, error = self.extractData()

        attributes_to_copy = data.get('attributes_to_copy', [])
        translation_exists = data.get('translation_exists', False)
        target_languages = data.get('target_languages', [])
        use_parent_languages = data.get('use_parent_languages', False)
        if use_parent_languages:
            parent_tm = ITranslationManager(Acquisition.aq_parent(context))
            target_languages = parent_tm.get_translated_languages()
            msg = u"Translate this object to the parent folder's languages."
        else:
            msg = u"Translate this object to the manually selected languages."

        status.addStatusMessage(msg, type="info")

        info, warnings, errors = utils.translate_this(
            context, attributes_to_copy, translation_exists, target_languages)

        self.handle_status(status, info, warnings, errors)
class BabelEdit(Edit):
    __call__ = ViewPageTemplateFile("babel_edit.pt")

    def __init__(self, context, request):
        self.context = context
        self.request = request
        self.group = ITranslationManager(self.context)

    def getTranslatedLanguages(self):
        return self.group.get_translated_languages()

    def getGroup(self):
        return self.group

    def getPortal(self):
        portal_url = getToolByName(self.context, "portal_url")
        return portal_url

    def objToTranslate(self):
        return self.context

    def languages(self):
        return {}
예제 #13
0
class BabelUtils(BrowserView):

    def __init__(self, context, request):
        self.context = context
        self.request = request
        portal_state = getMultiAdapter((context, request),
                                       name="plone_portal_state")
        self.portal_url = portal_state.portal_url()
        self.group = ITranslationManager(self.context)

    def getGroup(self):
        return self.group

    def getTranslatedLanguages(self):
        return self.group.get_translated_languages()

    def getPortal(self):
        portal_url = getToolByName(self.context, 'portal_url')
        return portal_url

    def objToTranslate(self):
        return self.context

    def gtenabled(self):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMultiLanguageExtraOptionsSchema)
        return settings.google_translation_key != ''

    def languages(self):
        """ Deprecated """
        context = aq_inner(self.context)

        ls = LanguageSelector(context, self.request, None, None)
        ls.update()
        results = ls.languages()

        supported_langs = [v['code'] for v in results]
        missing = set([str(c) for c in supported_langs])

        lsv = LanguageSelectorViewlet(context, self.request, None, None)
        translations = lsv._translations(missing)

        # We want to see the babel_view
        append_path = ('', 'babel_view',)
        non_viewable = set()
        for data in results:
            code = str(data['code'])
            data['translated'] = code in translations.keys()

            appendtourl = '/'.join(append_path)

            if data['translated']:
                trans, direct, has_view_permission = translations[code]
                if not has_view_permission:
                    # shortcut if the user cannot see the item
                    non_viewable.add((data['code']))
                    continue
                data['url'] = trans.absolute_url() + appendtourl
            else:
                non_viewable.add((data['code']))

        # filter out non-viewable items
        results = [r for r in results if r['code'] not in non_viewable]

        return results

    def translated_languages(self):
        context = aq_inner(self.context)
        tool = getToolByName(context, 'portal_languages', None)
        checkPermission = getSecurityManager().checkPermission

        translations = self.group.get_translations()
        translated_info = [dict(code=key, info=tool.getAvailableLanguageInformation()[key], obj=translations[key]) for key in translations]

        default_language = tool.getDefaultLanguage()

        translated_shown = []

        for lang_info in translated_info:
            # Mark the default language as the first translation shown
            if lang_info['code'] == default_language:
                lang_info['isDefault'] = True
            else:
                lang_info['isDefault'] = False

            # Remove the translation of the content currently being translated
            if lang_info['code'] == context.Language():
                continue

            # Remove the translation in case the translator user does not have permissions over it
            has_view_permission = bool(checkPermission('View', lang_info['obj']))
            if not has_view_permission:
                continue

            translated_shown.append(lang_info)
        return translated_shown
예제 #14
0
class BabelUtils(BrowserView):
    def __init__(self, context, request):
        self.context = context
        self.request = request
        portal_state = getMultiAdapter((context, request),
                                       name="plone_portal_state")
        self.portal_url = portal_state.portal_url()
        self.group = ITranslationManager(self.context)

    def getGroup(self):
        return self.group

    def getTranslatedLanguages(self):
        return self.group.get_translated_languages()

    def getPortal(self):
        portal_url = getToolByName(self.context, 'portal_url')
        return portal_url

    def objToTranslate(self):
        return self.context

    def gtenabled(self):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMultiLanguageExtraOptionsSchema)
        return settings.google_translation_key != ''

    def languages(self):
        """ Deprecated """
        context = aq_inner(self.context)

        ls = LanguageSelector(context, self.request, None, None)
        ls.update()
        results = ls.languages()

        supported_langs = [v['code'] for v in results]
        missing = set([str(c) for c in supported_langs])

        lsv = LanguageSelectorViewlet(context, self.request, None, None)
        translations = lsv._translations(missing)

        # We want to see the babel_view
        append_path = (
            '',
            'babel_view',
        )
        non_viewable = set()
        for data in results:
            code = str(data['code'])
            data['translated'] = code in translations.keys()

            appendtourl = '/'.join(append_path)

            if data['translated']:
                trans, direct, has_view_permission = translations[code]
                if not has_view_permission:
                    # shortcut if the user cannot see the item
                    non_viewable.add((data['code']))
                    continue
                data['url'] = trans.absolute_url() + appendtourl
            else:
                non_viewable.add((data['code']))

        # filter out non-viewable items
        results = [r for r in results if r['code'] not in non_viewable]

        return results

    def translated_languages(self):
        context = aq_inner(self.context)
        tool = getToolByName(context, 'portal_languages', None)
        checkPermission = getSecurityManager().checkPermission

        translations = self.group.get_translations()
        translated_info = [
            dict(code=key,
                 info=tool.getAvailableLanguageInformation()[key],
                 obj=translations[key]) for key in translations
        ]

        default_language = tool.getDefaultLanguage()

        translated_shown = []

        for lang_info in translated_info:
            # Mark the default language as the first translation shown
            if lang_info['code'] == default_language:
                lang_info['isDefault'] = True
            else:
                lang_info['isDefault'] = False

            # Remove the translation of the content currently being translated
            if lang_info['code'] == context.Language():
                continue

            # Remove the translation in case the translator user does not have permissions over it
            has_view_permission = bool(
                checkPermission('View', lang_info['obj']))
            if not has_view_permission:
                continue

            translated_shown.append(lang_info)
        return translated_shown
예제 #15
0
class BabelUtils(BrowserView):

    def __init__(self, context, request):
        self.context = context
        self.request = request
        portal_state = getMultiAdapter((context, request),
                                       name="plone_portal_state")
        self.portal_url = portal_state.portal_url()
        self.group = ITranslationManager(self.context)

    def getGroup(self):
        return self.group

    def getTranslatedLanguages(self):
        return self.group.get_translated_languages()

    def getPortal(self):
        portal_url = getToolByName(self.context, 'portal_url')
        return portal_url

    def objToTranslate(self):
        return self.context

    def gtenabled(self):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMultiLanguageExtraOptionsSchema)
        return settings.google_translation_key != ''

    def languages(self):
        context = aq_inner(self.context)

        ls = LanguageSelector(context, self.request, None, None)
        ls.update()
        results = ls.languages()

        supported_langs = [v['code'] for v in results]
        missing = set([str(c) for c in supported_langs])

        lsv = LanguageSelectorViewlet(context, self.request, None, None)
        translations = lsv._translations(missing)

        # We want to see the babel_view
        append_path = ('', 'babel_view',)
        non_viewable = set()
        for data in results:
            code = str(data['code'])
            data['translated'] = code in translations.keys()

            appendtourl = '/'.join(append_path)

            if data['translated']:
                trans, direct, has_view_permission = translations[code]
                if not has_view_permission:
                    # shortcut if the user cannot see the item
                    non_viewable.add((data['code']))
                    continue
                data['url'] = trans.absolute_url() + appendtourl
            else:
                non_viewable.add((data['code']))

        # filter out non-viewable items
        results = [r for r in results if r['code'] not in non_viewable]
        return results
예제 #16
0
class BabelUtils(BrowserView):

    def __init__(self, context, request):
        self.context = context
        self.request = request
        portal_state = getMultiAdapter((context, request),
                                       name="plone_portal_state")
        self.portal_url = portal_state.portal_url()
        # If there is any session tg lets use the session tg
        sdm = self.context.session_data_manager
        session = sdm.getSessionData(create=True)
        if 'tg' in session.keys():
            self.group = TranslationManager(session['tg'])
        else:
            self.group = ITranslationManager(self.context)

    def getGroup(self):
        return self.group

    def getTranslatedLanguages(self):
        return self.group.get_translated_languages()

    def getPortal(self):
        portal_url = getToolByName(self.context, 'portal_url')
        return portal_url

    def objToTranslate(self):
        return self.context

    def gtenabled(self):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMultiLanguageExtraOptionsSchema)
        key = settings.google_translation_key
        return key is not None and len(key.strip()) > 0

    def languages(self):
        """ Deprecated """
        context = aq_inner(self.context)

        ls = LanguageSelector(context, self.request, None, None)
        ls.update()
        results = ls.languages()

        supported_langs = [v['code'] for v in results]
        missing = set([str(c) for c in supported_langs])

        lsv = LanguageSelectorViewlet(context, self.request, None, None)
        translations = lsv._translations(missing)

        # We want to see the babel_view
        append_path = ('', 'babel_view',)
        non_viewable = set()
        for data in results:
            code = str(data['code'])
            data['translated'] = code in translations.keys()

            appendtourl = '/'.join(append_path)

            if data['translated']:
                trans, direct, has_view_permission = translations[code]
                if not has_view_permission:
                    # shortcut if the user cannot see the item
                    non_viewable.add((data['code']))
                    continue
                data['url'] = trans.absolute_url() + appendtourl
            else:
                non_viewable.add((data['code']))

        # filter out non-viewable items
        results = [r for r in results if r['code'] not in non_viewable]

        return results

    def translated_languages(self):
        context = aq_inner(self.context)
        tool = getToolByName(context, 'portal_languages', None)
        checkPermission = getSecurityManager().checkPermission

        translations = self.group.get_translations()
        translated_info = [dict(code=key, info=tool.getAvailableLanguageInformation()[key], obj=translations[key]) for key in translations]

        default_language = tool.getDefaultLanguage()

        translated_shown = []

        for lang_info in translated_info:
            # Mark the default language as the first translation shown
            if lang_info['code'] == default_language:
                lang_info['isDefault'] = True
            else:
                lang_info['isDefault'] = False

            # Remove the translation of the content currently being translated
            # In case it's temporal we show as language is not already set on AT
            if not context.portal_factory.isTemporary(self.context) and lang_info['code'] == ILanguage(context).get_language():
                continue

            # Remove the translation in case the translator user does not have permissions over it
            has_view_permission = bool(checkPermission('View', lang_info['obj']))
            if not has_view_permission:
                continue

            translated_shown.append(lang_info)
        return translated_shown

    def current_language_name(self):
        """ Get the current language native name """
        adapted = ILanguage(self.context)
        lang_code = adapted.get_language()
        util = getUtility(IContentLanguageAvailability)
        data = util.getLanguages(True)
        lang_info = data.get(lang_code)
        return lang_info.get('native', None) or lang_info.get('name')

    def max_nr_of_buttons(self):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMultiLanguageExtraOptionsSchema)
        return settings.buttons_babel_view_up_to_nr_translations
예제 #17
0
class BabelUtils(BrowserView):
    def __init__(self, context, request):
        self.context = context
        self.request = request
        portal_state = getMultiAdapter((context, request),
                                       name="plone_portal_state")
        self.portal_url = portal_state.portal_url()
        self.group = ITranslationManager(self.context)

    def getGroup(self):
        return self.group

    def getTranslatedLanguages(self):
        return self.group.get_translated_languages()

    def getPortal(self):
        portal_url = getToolByName(self.context, 'portal_url')
        return portal_url

    def objToTranslate(self):
        return self.context

    def gtenabled(self):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IMultiLanguageExtraOptionsSchema)
        return settings.google_translation_key != ''

    def languages(self):
        context = aq_inner(self.context)

        ls = LanguageSelector(context, self.request, None, None)
        ls.update()
        results = ls.languages()

        supported_langs = [v['code'] for v in results]
        missing = set([str(c) for c in supported_langs])

        lsv = LanguageSelectorViewlet(context, self.request, None, None)
        translations = lsv._translations(missing)

        # We want to see the babel_view
        append_path = (
            '',
            'babel_view',
        )
        non_viewable = set()
        for data in results:
            code = str(data['code'])
            data['translated'] = code in translations.keys()

            appendtourl = '/'.join(append_path)

            if data['translated']:
                trans, direct, has_view_permission = translations[code]
                if not has_view_permission:
                    # shortcut if the user cannot see the item
                    non_viewable.add((data['code']))
                    continue
                data['url'] = trans.absolute_url() + appendtourl
            else:
                non_viewable.add((data['code']))

        # filter out non-viewable items
        results = [r for r in results if r['code'] not in non_viewable]
        return results