import string
import base64


def encode_base64_ondemand(s):
    """
    Decode string to base64 if it isn't ascii. Also put leading ': ' so that LDAP
    knows the string is b64 encoded.

    >>> encode_base64_ondemand("Hej")
    ' Hej'
    >>> encode_base64_ondemand("Höj")
    ": SMO2ag=="
    """
    if not all(c in string.ascii_letters for c in s): # String not ASCII
        s = ": " + base64.b64encode(s.encode()).decode()
    else:
        s = " " + s
    return s