source: lliurex-mate-welcome/trunk/fuentes/welcome-po.py @ 3931

Last change on this file since 3931 was 3931, checked in by alviboi, 3 years ago
  • Property svn:executable set to *
File size: 5.4 KB
Line 
1#! /usr/bin/python3
2# -*- coding:utf-8 -*-
3
4"""  i18n helper for LliureX Mate Welcome
5
6Perform one of the following functions depending on the command line args
7
8    create a .pot file for the Welcome program and put it in the ./po directory
9    (create this if necessary). To be used when initially creating the .pot file
10    and also when new strings to translate are included in the Welcome program
11
12    create a .po file either for a specified locale or for all locales supported
13    by the system. If a .po file already exists, it will not be overwritten since
14    it may contain translations
15
16    update all .po files with new translatable strings from the .pot file
17
18    compile and install the translations in all available .po file to
19    usr/share/locale
20
21"""
22
23import os,sys,subprocess,shutil,glob
24
25###########################################################
26def show_usage():
27    """ Display the command line options """
28
29    print("\nwelcome-po usage")
30    print("\nUsage: welcome-po [arguments]")
31    print("  --create-pot                Create a lliurex-mate-welcome.pot in the po directory")
32    print("                              (can also be used when whenever new translatable strings")
33    print("                               are added to lliurex-mate-welcome.\n")
34    print("  --update-pos                Update all .po files in the .po directory with new")
35    print("                              translateable strings from the .pot file\n")
36    print("  --install                   Compile all of the .po files in the po directory")       
37    print("                              and install them under ./locale/\n")
38    print("  --help                      Show this message\n")
39    print(" Requirements:                Must be run in the same directory as lliurex-mate-welcome")
40    print("                              Requires xgettext, pot2po, msgmerge, msgfmt \n")
41
42
43
44###########################################################
45def create_pot(po_dir):
46    """ Create .pot file for Lliurex Mate Welcome
47   
48    Expect to find Welcome in the current directory...
49    Create a po directory if one doesn't exist
50
51    Args:
52        po_dir - the location of the po directory
53
54    """
55
56    if not subprocess.call(["which", "pygettext"],
57                       stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0:
58        print("Error: pygettext is not available.")
59        sys.exit(1)
60
61    if not (os.path.exists(po_dir)):
62        os.mkdir(po_dir)
63
64    pot_file = os.path.join(po_dir,"lliurex-mate-welcome.pot")
65
66    subprocess.call(["xgettext",
67                     "-d", "lliurex-mate-welcome",    # domain name
68                     "-o", pot_file,                 # output file
69                     "lliurex-mate-welcome",          # input file
70                     "-L", "Python"])                # Language
71
72    print ("%s created.\n" %pot_file)
73
74
75###########################################################
76def update_pos(po_dir):
77    """ Update all .po files with any new translatable strings from the .pot
78        file
79
80    Use msgmerge in order to preserve any existing translations in the .po
81    files
82
83    Args: po_dir - the directory containing the .po files
84    """
85
86    pot_file = os.path.join(po_dir,"lliurex-mate-welcome.pot")
87    if not os.path.exists(pot_file):
88        print("Error: lliurex-mate-welcome.pot does not exist")
89        exit()
90
91    po_files = glob.glob(os.path.join(po_dir, '*.po'))
92
93    for po_file in po_files:
94        subprocess.call(["msgmerge",
95                        po_file, pot_file,
96                        "-U",                # Update po file
97                        "-q"])               # Quiet mode
98        print("%s updated " %po_file)
99
100    print ("Update completed......")
101
102
103###########################################################
104def compile_and_install(po_dir, locale_dir):
105    """ Compile all of the po files in the po directory into the
106        locale directory, ready for use by the Welcome
107        program
108   
109    Create the locale directory and any required subdirectories
110    if they don't already exist
111    """
112
113    if not (os.path.exists(locale_dir)):
114        os.mkdir(locale_dir)
115
116    po_files = glob.glob(os.path.join(po_dir, '*.po'))
117    for po_file in po_files:
118        locale_name =(os.path.splitext(os.path.split(po_file)[1])[0])
119       
120        #create a directory for the locale if we don't already have one
121        this_locale = os.path.join(locale_dir, locale_name)
122        lcm_dir = os.path.join(this_locale, "LC_MESSAGES")
123
124        if not os.path.exists(this_locale):
125            os.mkdir(this_locale)
126            # also make the LC_MESSAGES directory
127            os.mkdir(lcm_dir)
128       
129        #now compile and install the .po
130        print ("processing %s" %po_file)
131        output_file = os.path.join(lcm_dir, "lliurex-mate-welcome.mo")
132
133        subprocess.call(["msgfmt", po_file,
134                     "--output-file", output_file])
135
136
137    print ("All languages compiled.")
138
139
140if (len(sys.argv)==1) or (sys.argv[1]=="--help"):
141    show_usage()
142    exit()
143
144if not os.path.exists("./lliurex-mate-welcome"):
145    print("Error: Need to be in the same directory as lliurex-mate-welcome...")
146    exit()
147
148source_dir = '.'
149po_dir = os.path.join(source_dir, "po")
150locale_dir = os.path.join(source_dir, "locale")
151
152arg = sys.argv[1]
153if (arg == "--create-pot") or (len(sys.argv)==1):
154    create_pot(po_dir)
155    exit()
156
157if arg=="--update-pos":
158    update_pos(po_dir)
159    exit()
160
161if arg=="--install":
162    compile_and_install(po_dir, locale_dir)
163    exit()
Note: See TracBrowser for help on using the repository browser.