• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
pytGenStrings script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pytGenStrings script


  • Subject: pytGenStrings script
  • From: Xin <email@hidden>
  • Date: Mon, 2 Mar 2009 18:44:45 +0100

First, sorry for my english, I'm catalan.


After months searching a script that combine old Localizable ".strings" with new ".strings", I  created a simple python script for automatize it. It's not the better, but is valid for me.


Basic Features:
 - Maintain old localized entries
 - Delete removed entries
 - Add new entries appended at end of file with datetime comment
 - You can translate and reorder entries and add comments

Other Features:
 - Discard unchanged files
 - Maintain previous order
 - Maintain additional spaces
 - Maintain aditional comments
 - Maintain lateral comments
 - Maintain removed comments
 - Change comment for existing entries (only if it is in previous line and it's /* */ type)

Knowed Limitations:
 - First entry do not must be in first line (Add a comment o return)
 - Entry must be in new line and first char must be (")
 - Not UTF-16 files discarted. (you save changes with utf-16 always)

Usage:
 - Install into $PATH folder or add path into script
 - Into target script add lines:
pyGenStrings.py dirOut files ...

   * dirOut:   Folder for check .strings files and used with "genstrings" "-o" option
   * files: Files (with wildcards) used with "genstrings" files


Code pyGenStrings.py:

#! /usr/bin/env python

import os
import re
import sys
import time
import codecs


def combine_strings(previous,path_to):
    # Charge new string files
    string_files = {}
    for f in os.listdir(path_to):
        file_path = os.path.join(path_to,f)
        if os.path.isfile(file_path) and f.endswith('.strings'):
            try:
                content = codecs.open(file_path,'r','utf-16').read()
                entries = re.findall(ur'(.*?\n"(.*?)"\s*?=\s*?".*?"\s*?;[^\n]*)',content,re.S)
                byname = {}
                for e in entries:
                    byname[e[1]] = e
                string_files[f] ={'content':content, 'entries':entries, 'byname':byname}
            except:
                pass

    

    # Print old entries and replace comment if exists
    for f,d in previous.items():
        # Discard files with same content
        if d['content'] == string_files[f]['content']: continue

        old_entries = d['byname'].keys()
        new_entries = string_files[f]['byname'].keys()
        new_file_content = u''
        # Add previous entries, and change comment if needed
        for e in d['entries']:
            if e[1] not in new_entries: continue;
            new = string_files[f]['byname'][e[1]]
            new_comment = re.findall(ur'\s*?/\*(.*?)\*/\s*?\n"',new[0],re.S)
            old_comment = re.findall(ur'\s*?/\*(.*?)\*/\s*?\n"',e[0],re.S)

            if len(old_comment) and new_comment[0] != old_comment[0]:
                # re.sub delete extra '\'. Must duplicate
                new_comment[0] = new_comment[0].replace('\\','\\\\')
                new_file_content += re.sub(ur'(\s*?/\*).*?(\*/\s*?\n")',ur'\1%s\2'%new_comment[0],e[0],re.S)
            else:
                new_file_content += e[0]
        new_file_content += '\n'*4

        

        # Check for new entries
        new_entries = set(new_entries).difference(old_entries)
        if new_entries: 
            new_file_content += '// New entries: %s\n\n' % time.strftime('%F %T',time.localtime())
        for k in string_files[f]['entries']:
            if k[1] not in new_entries: continue;
            new_file_content += k[0]
        new_file_content += '\n'*2

        

        file_path = os.path.join(path_to,f)
        codecs.open(file_path,'w','utf-16').write(new_file_content)


def gen_strings(list_from,path_to):
    os.system('genstrings -o %s %s' % (path_to,' '.join(list_from)))


def load_string_from(path):
    string_files = {}
    for f in os.listdir(path):
        file_path = os.path.join(path,f)
        if os.path.isfile(file_path) and f.endswith('.strings'):
            try:
                content = codecs.open(file_path,'r','utf-16').read()
                entries = re.findall(ur'(.*?\n"(.*?)"\s*?=\s*?".*?"\s*?;[^\n]*)',content,re.S)
                byname = {}
                for e in entries:
                    byname[e[1]] = e
                string_files[f] ={'content':content, 'entries':entries, 'byname':byname}
            except Exception, e:
                pass
    return string_files


if __name__ == '__main__':
    if len(sys.argv) < 3:
        print "pyGenStrings usage: pyGenStrings pathOut files ..."
        sys.exit(1) 

    

    load = load_string_from(sys.argv[1])
    gen_strings(sys.argv[2:],sys.argv[1])
    combine_strings(load,sys.argv[1])



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: Error message on import to svn repository
  • Next by Date: Re: Information request on DWARF dsym reader
  • Previous by thread: Re: Error message on import to svn repository
  • Next by thread: Newbie needs help with an exercise from the Book Programming in Objective-C 2.0
  • Index(es):
    • Date
    • Thread