Skip to content

Use a temporary_locale context manager #1347

Closed
@smartass101

Description

While working on my i18n_subsites plugin, I came up with an idea how to simplify some code that is often used in Pelican: doing stuff with a temporarily different locale.
This could be neatly solved by a context manager:

from contextlib import contextmanager
import locale

@contextmanager
def temporary_locale(temp_locale=None):
    '''Enable code to run in a context with a temporary locale

    Resets the locale back when existing context.
    Can set a temporary locale if provided
    '''
    orig_locale = locale.setlocale(locale.LC_ALL)
    if temp_locale is not None:
        locale.setlocale(locale.LC_ALL, temp_locale)
    yield
    locale.setlocale(locale.LC_ALL, orig_locale)

This would enable stuff like

with temporary_locale('C'):
     pass

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions