Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius/localization.py
blob: 5c64310fc4d49d8d796da8d999203c922bb7a35b (plain)
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
# Copyright (C) 2009, Tutorius.org
# Copyright (C) 2009, Michael Janelle-Montcalm <michael.jmontcalm@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os

class LocalizationHelper(object):

    @classmethod
    def _write_addon_strings_to_file(cls, addon, output_file):
        """
        For a given addon, writes a pot file entry for every string property
        it has.

        @param addon The addon from which we want to get the string properties
        @param output_file The file in which we should write the strings
        @return None
        """
        for (prop_name, prop_value) in addon._props.items():
            prop_type = getattr(type(addon), prop_name).type
            if prop_type == "string":
                prop_value = prop_value.replace("\n", "\\n")
                prop_value = prop_value.replace("\r", "\\r")
                output_file.write('msgid "%s"\nmsgstr ""\n\n'%(prop_value))

    @classmethod
    def write_translation_file(cls, tutorial, output_file):
        """
        Writes the translation file to the given file, according to the .pot
        files specifications, for the given tutorial.

        This will generate a pair of line for each TStringProperty in the following
        format : 
        msgid "<string>"
        msgstr ""

        This will enable the translator to create a localization for this tutorial.

        @param tutorial The executable reprensentation of the tutorial 
        @param output_file An opened file object to which the strings translation
                        template will be written
        @return Nothing
        """
        state_dict = tutorial.get_state_dict()

        for state_name in state_dict.keys():
            actions = tutorial.get_action_dict(state_name)
            events = tutorial.get_transition_dict(state_name)

            for action in actions.values():
                cls._write_addon_strings_to_file(action, output_file)

            for (event, next_state) in events.values():
                cls._write_addon_strings_to_file(event, output_file)