-
-
Save temnoregg/6121341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
""" | |
MAKESTRINGS – EASIER I18N FOR TITANIUM APPS | |
Auto-generates localized strings.xml for Appcelerator Titanium projects by inspecting | |
the project's JS files looking for L() calls. | |
The scripts adds unlocalized strings to strings.xml, ready for translation. Already | |
localized strings in strings.xml are not touched. | |
SETUP | |
- Place this file in the same directory as tiapp.xml. | |
- Create a i18n/ directory, and a directory for each locale to support. | |
- Create an empty strings.xml in each locale directory: | |
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
</resources> | |
USAGE | |
- Execute ./makestrings.py <locale> | |
- Repeat for each locale and to update the files. | |
LICENSE | |
Copyright (c) 2011, Funkbit AS. | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
* Neither the name of Funkbit nor the | |
names of its contributors may be used to endorse or promote products | |
derived from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
""" | |
import codecs | |
import os | |
import re | |
import sys | |
import xml | |
from xml.dom.minidom import parseString | |
################################### | |
# Improved XML writer for minidom # | |
################################### | |
def fixed_writexml(self, writer, indent="", addindent="", newl=""): | |
""" | |
Fixed version of writexml() with better formatting support. | |
See http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/ | |
""" | |
writer.write(indent+"<" + self.tagName) | |
attrs = self._get_attributes() | |
a_names = attrs.keys() | |
a_names.sort() | |
for a_name in a_names: | |
writer.write(" %s=\"" % a_name) | |
xml.dom.minidom._write_data(writer, attrs[a_name].value) | |
writer.write("\"") | |
if self.childNodes: | |
if len(self.childNodes) == 1 \ | |
and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE: | |
writer.write(">") | |
self.childNodes[0].writexml(writer, "", "", "") | |
writer.write("</%s>%s" % (self.tagName, newl)) | |
return | |
writer.write(">%s"%(newl)) | |
for node in self.childNodes: | |
node.writexml(writer,indent+addindent,addindent,newl) | |
writer.write("%s</%s>%s" % (indent,self.tagName,newl)) | |
else: | |
writer.write("/>%s"%(newl)) | |
# Replace minidom's writexml function with improved | |
xml.dom.minidom.Element.writexml = fixed_writexml | |
############## | |
# Processing # | |
############## | |
if len(sys.argv) < 2: | |
print "Usage:\n ./makestrings.py <locale>" | |
sys.exit(1) | |
# All discovered strings | |
i18n_strings = [] | |
# Open strings.xml for locale specificed by argument | |
strings_path = os.path.join(os.path.dirname(__file__), 'i18n', sys.argv[1], 'strings.xml') | |
if not os.path.exists(strings_path): | |
print "Unable to open strings.xml for locale. Ensure it exists and try again." | |
sys.exit(1) | |
# Get XML contents and strip empty lines | |
with codecs.open(strings_path, 'r', 'utf-8') as strings_file: | |
current_xml = strings_file.read() | |
current_xml.split('\n') | |
current_xml = ''.join( | |
[line for line in current_xml.split('\n') if line.strip() != ''] | |
) | |
# Discover existing strings in file | |
dom = parseString(current_xml.encode('utf-8')) | |
resources = dom.getElementsByTagName('resources')[0] | |
strings = resources.getElementsByTagName('string') | |
for string in strings: | |
i18n_strings.append(string.getAttribute('name')) | |
# Walk through all resources files | |
for dname, dirs, files in os.walk("app"): | |
for fname in files: | |
fpath = os.path.join(dname, fname) | |
with open(fpath) as f: | |
# Find all L(<...>) matches in file | |
matches = re.findall('L\([\"\'](.[^\"\']*)[\"\'](,.?[\"\'](.*)[\"\'])?\)', f.read(), re.MULTILINE) | |
for match_tuple in matches: | |
match, opt, default = match_tuple | |
# Is this a new string? | |
if not match in i18n_strings: | |
# Add to XML | |
val = default or match | |
string = dom.createElement('string') | |
string.setAttribute('name', match) | |
value = dom.createTextNode(val) | |
string.appendChild(value) | |
resources.appendChild(string) | |
# Add to list | |
i18n_strings.append(match) | |
# Write XML to strings.xml | |
with codecs.open(strings_path, 'w', 'utf-8') as strings_file: | |
dom.writexml(strings_file, newl='\n', encoding='utf-8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment