File: write_text.py

package info (click to toggle)
cataclysm-dda 0.H-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 710,808 kB
  • sloc: cpp: 524,019; python: 11,580; sh: 1,228; makefile: 1,169; xml: 507; javascript: 150; sql: 56; exp: 41; perl: 37
file content (71 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from .message import Message, messages, occurrences


def append_comment(comments, new_comment):
    if type(new_comment) is str:
        return comments + new_comment.split("\n")
    elif type(new_comment) is list:
        for comment in new_comment:
            if comment:
                comments = append_comment(comments, comment)
        return comments


def write_text(json, origin, context="", comment="",
               plural=False, c_format=True):
    """
    Record a text for translation.

    Parameters:
        json: The text in string or JSON dict form
        origin (str): Path of JSON source location
        context (str): "context" as in GNU gettext
        comment: Translation comments in either string form or list of strings
        plural (bool): Whether the text should be pluralized
        c_format (bool): Whether the text contains C-style format string
    """
    if json is None or json == "":
        return

    comments = append_comment([], comment)
    text = ""
    text_plural = ""

    if type(json) is str:
        text = json
        if plural:
            text_plural = "{}s".format(text)
    elif type(json) is dict:
        if "//~" in json:
            if type(json["//~"]) is str and json["//~"]:
                comments = append_comment(comments, json["//~"])
        if "ctxt" in json:
            if type(json["ctxt"]) is str:
                context = json["ctxt"]
        if "str" in json:
            text = json["str"]
        if plural:
            if "str_sp" in json:
                text = json["str_sp"]
                text_plural = json["str_sp"]
            elif "str_pl" in json:
                text_plural = json["str_pl"]
            else:
                text_plural = "{}s".format(text)

    if not text:
        return

    format_tag = ""
    if "%" in text:
        if c_format:
            format_tag = "c-format"
        else:
            format_tag = "no-c-format"

    if (context, text) not in messages:
        messages[(context, text)] = list()

    messages[(context, text)].append(
        Message(comments, origin, format_tag, context, text, text_plural))
    occurrences.append((context, text))