## Script (Python) "insideI18NLayer"
##title=helper to check if inside an i18nlayer
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
REQUEST=context.REQUEST

def portal_factory_support(context):
    """ strip portal factory context """

    parent = None

    # portal factory support
    try: pf = context.portal_factory
    except AttributeError: pass
    else:
        # found portal factory tool
        # now check if the context is temporary
        if pf.isTemporary(context):
            # skip over tempfolder / factory tool / original object
            try:
                parent = context.aq_inner.aq_parent.aq_parent.aq_parent
            except:
                # this should not happen
                parent = None
                
    return parent

def portal_objectmanagement_support(context):
    """ strip shadow context """

    parent = None

    # objectmanagement support
    try: po = context.portal_objectmanagement
    except AttributeError: pass
    else:
        # found portal objectmanagement tool
        # now check if the context is a shadow
        if po.isShadow(context):
            # skip over shadow
            try:
                parent = context.aq_inner.aq_parent.aq_parent
            except:
                # this should not happen
                parent = None
                
    return parent

def parent_default(context):
    """ strip one aq layer """
    # get direct parent
    try: parent = context.aq_inner.aq_parent
    except: parent = None
    return parent

# filter certain known temporary stuff which can exist inside an i18nlayer

# usual
parent = parent_default(context)

# factory
res = portal_factory_support(context)
if res is not None: parent = res
    
# shadow
res = portal_objectmanagement_support(context)
if res is not None: parent = res

# default status
status = False

# when the direct parent is an I18NLayer we are multilanguage wrapped
if parent is not None and getattr(parent.aq_inner.aq_explicit, 'meta_type', None) == 'I18NLayer':
    # wrapped
    status = True 

# return status
return status
