<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_locale(ln):
 """Set Babel localization in request context. :param ln: Language identifier. """

    ctx = _request_ctx_stack.top
    if ctx is None:
        raise RuntimeError('Working outside of request context.')
    new_locale = current_app.extensions['babel'].load_locale(ln)
    old_locale = getattr(ctx, 'babel_locale', None)
    setattr(ctx, 'babel_locale', new_locale)
    yield
    setattr(ctx, 'babel_locale', old_locale)