本文整理汇总了Python中plone.app.multilingual.interfaces.ITranslationManager.get_translation方法的典型用法代码示例。如果您正苦于以下问题:Python ITranslationManager.get_translation方法的具体用法?Python ITranslationManager.get_translation怎么用?Python ITranslationManager.get_translation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类plone.app.multilingual.interfaces.ITranslationManager
的用法示例。
在下文中一共展示了ITranslationManager.get_translation方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_content_is_linked
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def test_content_is_linked(self):
items = self._run_transmogrifier()
self.assertEqual(4, len(items))
manager = ITranslationManager(self.folder_en)
self.assertEqual(self.folder_de, manager.get_translation('de'),
'English and German content should be linked.')
self.assertEqual(2, len(manager.get_translations()))
manager = ITranslationManager(self.file_de)
self.assertEqual(self.file_en, manager.get_translation('en'),
'English and German content should be linked.')
self.assertEqual(2, len(manager.get_translations()))
示例2: handle_modified
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def handle_modified(self, content):
fieldmanager = ILanguageIndependentFieldsManager(content)
if not fieldmanager.has_independent_fields():
return
sm = getSecurityManager()
try:
# Do we have permission to sync language independent fields?
if self.bypass_security_checks():
# Clone the current user and assign a new editor role to
# allow edition of all translated objects even if the
# current user whould not have permission to do that.
tmp_user = UnrestrictedUser(
sm.getUser().getId(), '', ['Editor', ], '')
# Wrap the user in the acquisition context of the portal
# and finally switch the user to our new editor
acl_users = getToolByName(content, 'acl_users')
tmp_user = tmp_user.__of__(acl_users)
newSecurityManager(None, tmp_user)
# Copy over all language independent fields
transmanager = ITranslationManager(content)
for translation in self.get_all_translations(content):
trans_obj = transmanager.get_translation(translation)
if fieldmanager.copy_fields(trans_obj):
self.reindex_translation(trans_obj)
finally:
# Restore the old security manager
setSecurityManager(sm)
示例3: handle_remove
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def handle_remove(self, action):
data, errors = self.extractData()
manager = ITranslationManager(self.context)
if not errors:
for language in data['languages']:
content = manager.get_translation(language)
manager.remove_translation(language)
aq_parent(content).manage_delObjects([content.getId()])
return self.request.response.redirect(
self.context.absolute_url() + '/remove_translations')
示例4: test_multilingual_content_is_linked
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def test_multilingual_content_is_linked(self):
english_content = self.portal.get('en').get('accessibility')
self.assertIsNotNone(english_content, 'Missing page /en/accessibility')
german_content = self.portal.get('de').get('barrierefreiheit')
self.assertIsNotNone(german_content,
'Missing page /de/barrierefreiheit')
manager = ITranslationManager(english_content)
self.assertEquals(german_content,
manager.get_translation('de'),
'English and German content should be linked.')
示例5: createdEvent
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def createdEvent(obj, event):
""" Subscriber to set language on the child folder
It can be a
- IObjectRemovedEvent - don't do anything
- IObjectMovedEvent
- IObjectAddedEvent
- IObjectCopiedEvent
"""
if IObjectRemovedEvent.providedBy(event):
return
request = getattr(event.object, 'REQUEST', getRequest())
if not IPloneAppMultilingualInstalled.providedBy(request):
return
# On ObjectCopiedEvent and ObjectMovedEvent aq_parent(event.object) is
# always equal to event.newParent.
parent = aq_parent(event.object)
# special parent handling
if not ITranslatable.providedBy(parent):
set_recursive_language(obj, LANGUAGE_INDEPENDENT)
return
# Normal use case
# We set the tg, linking
language = ILanguage(parent).get_language()
set_recursive_language(obj, language)
request = getattr(event.object, 'REQUEST', getRequest())
try:
ti = request.translation_info
except AttributeError:
return
# AT check
portal = getSite()
portal_factory = getToolByName(portal, 'portal_factory', None)
if (
not IDexterityContent.providedBy(obj)
and portal_factory is not None
and not portal_factory.isTemporary(obj)
):
return
IMutableTG(obj).set(ti['tg'])
modified(obj)
tm = ITranslationManager(obj)
old_obj = tm.get_translation(ti['source_language'])
ILanguageIndependentFieldsManager(old_obj).copy_fields(obj)
示例6: __call__
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def __call__(self, language):
"""
Look for the closest translated folder or siteroot
"""
parent = aq_parent(self.context)
translated_parent = parent
found = False
while not (IPloneSiteRoot.providedBy(parent) and not ILanguageRootFolder.providedBy(parent)) and not found:
parent_translation = ITranslationManager(parent)
if parent_translation.has_translation(language):
translated_parent = \
parent_translation.get_translation(language)
found = True
parent = aq_parent(parent)
return translated_parent
示例7: __call__
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def __call__(self):
if (self.request.method != 'POST' and
not ('field' in self.request.form.keys() and
'lang_source' in self.request.form.keys())):
return _("Need a field")
else:
manager = ITranslationManager(self.context)
registry = getUtility(IRegistry)
settings = registry.forInterface(IMultiLanguageExtraOptionsSchema)
lang_target = ILanguage(self.context).get_language()
lang_source = self.request.form['lang_source']
orig_object = manager.get_translation(lang_source)
field = self.request.form['field'].split('.')[-1]
if hasattr(orig_object, field):
question = getattr(orig_object, field, '')
if hasattr(question, 'raw'):
question = question.raw
else:
return _("Invalid field")
return google_translate(question, settings.google_translation_key, lang_target, lang_source)
示例8: create_translation
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def create_translation(self, *args, **kwargs):
"""Create translation for an object with uid in the given
target_language and return its UID
Usage::
Create translation /plone/en/foo ca title=Translated
"""
disableCSRFProtection()
# Parse arguments:
uid_or_path = args[0]
target_language = args[1]
# BBB: Support keywords arguments with robotframework < 2.8.3
kwargs.update(dict([arg.split('=', 1) for arg in args[2:]]))
# Look up translatable content
pc = getToolByName(self, "portal_catalog")
uid_results = pc.unrestrictedSearchResults(UID=uid_or_path)
path_results = pc.unrestrictedSearchResults(
path={'query': uid_or_path.rstrip('/'), 'depth': 0})
obj = (uid_results or path_results)[0]._unrestrictedGetObject()
# Translate
manager = ITranslationManager(obj)
manager.add_translation(target_language)
translation = manager.get_translation(target_language)
# Update fields
data = constructMessageFromSchemata(obj, iterSchemata(obj))
for key, value in kwargs.items():
del data[key]
data[key] = Header(value, 'utf-8')
del data['language']
initializeObjectFromSchemata(translation, iterSchemata(obj), data)
notify(ObjectModifiedEvent(translation))
# Return uid for the translation
return IUUID(translation)
示例9: __call__
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def __call__(self):
if (self.request.method != 'POST' and
not ('field' in self.request.form.keys() and
'lang_source' in self.request.form.keys())):
return _("Need a field")
else:
manager = ITranslationManager(self.context)
registry = getUtility(IRegistry)
settings = registry.forInterface(IMultiLanguageExtraOptionsSchema)
lang_target = ILanguage(self.context).get_language()
lang_source = self.request.form['lang_source']
orig_object = manager.get_translation(lang_source)
try:
question = orig_object.getField(
self.request.form['field']).get(orig_object)
except AttributeError:
return _("Invalid field")
return google_translate(
question,
settings.google_translation_key,
lang_target, lang_source
)
示例10: getClosestDestination
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def getClosestDestination(self):
"""Get the "closest translated object" URL.
"""
# We should travel the parent chain using the catalog here,
# but I think using the acquisition chain is faster
# (or well, __parent__ pointers) because the catalog
# would require a lot of queries, while technically,
# having done traversal up to this point you should
# have the objects in memory already
# As we don't have any content object we are going to look
# for the best option
site = getSite()
root = getToolByName(site, 'portal_url')
ltool = getToolByName(site, 'portal_languages')
# We are using TranslationManager to get the translations of a
# string tg
manager = TranslationManager(self.tg)
context = None
languages = manager.get_translations()
if len(languages) == 0:
# If there is no results there are no translations
# we move to portal root
return self.wrapDestination(root(), postpath=False)
# We are going to see if there is the preferred language translation
# Otherwise we get the first as context to look for translation
prefered = ltool.getPreferredLanguage(self.request)
if prefered in languages:
context = languages[prefered]
else:
context = languages[languages.keys()[0]]
checkPermission = getSecurityManager().checkPermission
chain = self.getParentChain(context)
for item in chain:
if ISiteRoot.providedBy(item) \
and not ILanguageRootFolder.providedBy(item):
# We do not care to get a permission error
# if the whole of the portal cannot be viewed.
# Having a permission issue on the root is fine;
# not so much for everything else so that is checked there
return self.wrapDestination(item.absolute_url())
elif IFactoryTempFolder.providedBy(item) or \
IFactoryTool.providedBy(item):
# TempFolder or portal_factory, can't have a translation
continue
try:
canonical = ITranslationManager(item)
except TypeError:
if not ITranslatable.providedBy(item):
# In case there it's not translatable go to parent
# This solves the problem when a parent is not
# ITranslatable
continue
else:
raise
translation = canonical.get_translation(self.lang)
if translation and (
INavigationRoot.providedBy(translation) or
bool(checkPermission('View', translation))
):
# Not a direct translation, therefore no postpath
# (the view might not exist on a different context)
return self.wrapDestination(translation.absolute_url(),
postpath=False)
# Site root's the fallback
return self.wrapDestination(root(), postpath=False)
示例11: translate
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def translate(content, target_language='en'):
manager = ITranslationManager(content)
manager.add_translation(target_language)
return manager.get_translation(target_language)
示例12: makeTranslation
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def makeTranslation(content, language="en"):
manager = ITranslationManager(content)
manager.add_translation(language)
return manager.get_translation(language)
示例13: __call__
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def __call__(self):
context = self.context
request = self.request
catalog = context.portal_catalog
portal = api.portal.get()
alsoProvides(request, IDisableCSRFProtection)
root_zh = portal.get('zh')
if not root_zh: return
root_en = portal.get('en')
if not root_en: return
prog_zh = root_zh.get('programs')
if not prog_zh: return
prog_en = root_en.get('programs')
if not prog_en: return
container_zh = prog_zh.get('recipients')
if not container_zh: return
container_en = prog_en.get('recipients')
if not container_en: return
with open('grant-list.csv', "rb") as file:
rows = csv.reader(file, delimiter='\t')
next(rows, None)
for row in rows:
folder_zh = container_zh.get(str(row[1]))
if not folder_zh:
fldr = api.content.create(
type='Folder',
container=container_zh,
id=row[1],
title=row[1]
)
fldr.reindexObject()
if row[2] == '': row[2] = row[3]
if row[4] == '': row[4] = row[5]
if row[3] == '': row[3] = row[2]
if row[5] == '': row[5] = row[4]
ctnt = api.content.create(
type='Grant',
container=container_zh.get(str(row[1])),
id=row[0],
title=safe_unicode(row[2]),
year=row[1],
director=row[4],
description=row[6],
department=row[7],
region=row[8],
category=row[9],
budget=row[10],
currency=row[11],
status=row[12]
)
manager = ITranslationManager(ctnt)
manager.add_translation('en')
trns = manager.get_translation('en')
trns.id = str(row[0])
trns.title = row[3]
trns.year = row[1]
trns.director = row[5]
trns.description = row[6]
trns.department = row[7]
trns.region = row[8]
trns.category = row[9]
trns.budget = row[10]
trns.currency = row[11]
trns.status = row[13]
ctnt.reindexObject()
trns.reindexObject()
示例14: makeTranslation
# 需要导入模块: from plone.app.multilingual.interfaces import ITranslationManager [as 别名]
# 或者: from plone.app.multilingual.interfaces.ITranslationManager import get_translation [as 别名]
def makeTranslation(content, language='en'):
manager = ITranslationManager(content)
manager.add_translation(language)
import transaction; transaction.commit()
return manager.get_translation(language)