~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Functions to help with translation templates."""

__all__ = [
    'make_domain',
    'make_name',
    'make_name_from_path',
    ]


import os

from lp.app.validators.name import sanitize_name


GENERIC_TEMPLATE_NAMES = [
    'en-US.xpi',
    'messages.pot',
    'untitled.pot',
    'template.pot',
    ]
GENERIC_TEMPLATE_DIRS = [
    'po',
    ]


def make_domain(path, default=''):
    """Generate the translation domain name from the path of the template
    file.

    :returns: The translation domain name or an empty string if it could
        not be determined.
    """
    dname, fname = os.path.split(path)
    # Handle generic names and xpi cases
    if fname not in GENERIC_TEMPLATE_NAMES:
        domain, ext = os.path.splitext(fname)
        return domain
    dname1, dname2 = os.path.split(dname)
    if dname2 not in GENERIC_TEMPLATE_DIRS:
        return dname2 or default
    rest, domain = os.path.split(dname1)
    return domain or default


def make_name(domain):
    """Make a template name from a translation domain."""
    return sanitize_name(domain.replace('_', '-').lower())


def make_name_from_path(path, default=''):
    """Make a template name from a file path."""
    return make_name(make_domain(path, default=default))