Skip to content

Instantly share code, notes, and snippets.

@pix0r
Created August 24, 2011 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pix0r/1168738 to your computer and use it in GitHub Desktop.
Save pix0r/1168738 to your computer and use it in GitHub Desktop.
Automates the process of updating localized NIB files
#!/usr/bin/env python
locales = ["en", "fr", "it", "de", "es"]
dev_locale = "en"
base_dir = "ProjectSourceDirectory"
nibs = [
"MainWindow.xib",
"Error/ErrorViewController.xib",
"Home/HomeViewController.xib",
"Home/SettingsViewController.xib",
"LocationSelect/LocationSelectViewController.xib",
]
#!/usr/bin/env python
#
# Update localized NIBs
#
# Given a list of NIB file locations and a list of supported locales, this
# program will attempt to create or update the localized versions of these
# NIB files using the localized strings file associated with each NIB.
#
# Localized strings files for each nib are all placed under the project base
# directory, in <locale>.lproj/<CombinedDirectoryAndNibFileName>.strings.
# CombinedDirectoryAndNibFileName.strings is computed as such:
# <full_path_to_nib>.replace("/", "_").replace(".xib", ".strings")
#
# If the strings file does not exist, this script will attempt to create it.
import os, sys
from localized_nibs_config import *
def update_all_nibs():
for relative_nibfile in nibs:
nibfile = base_dir + "/" + relative_nibfile
base_nib = localized_path(nibfile, dev_locale)
if not os.path.exists(base_nib):
print "Error: NIB file not set up for localization:", nibfile
print "Base nib file should be present:", base_nib
continue
# Strings file location
strings = base_dir + "/" + relative_nibfile.replace("/", "_").replace(".xib", ".strings")
base_strings = localized_path(strings, dev_locale)
if not os.path.exists(base_strings):
print "Error: Strings file not set up:", strings
print "Base strings file should be present:", base_strings
cmd = "ibtool --generate-strings-file \"" + base_strings + "\" \"" + base_nib + "\""
print "Automatically creating this file for you:", cmd
os.system(cmd)
for locale in locales:
if locale == dev_locale: continue
translated_nib = localized_path(nibfile, locale)
translated_strings = localized_path(strings, locale)
if not os.path.exists(translated_strings):
print "Strings file has not been translating; creating:", translated_strings
cmd = "cp \"" + base_strings + "\" \"" + translated_strings + "\""
print "Running command:", cmd
os.system(cmd)
if not os.path.exists(translated_nib):
print "NIB file not yet set up for translation into", locale
print "Copying file; you need to drag this into Xcode project:", translated_nib
cmd = "mkdir -p " + os.path.dirname(translated_nib)
print "Running command:", cmd
os.system(cmd)
cmd = "cp \"" + base_nib + "\" \"" + translated_nib + "\""
print "Running command:", cmd
os.system(cmd)
continue
cmd = "ibtool --strings-file \"" + translated_strings + "\" \"" + base_nib + "\" --write \"" + translated_nib + "\""
print "Running command:", cmd
os.system(cmd)
def localized_path(nonlocalized_file, locale):
return os.path.dirname(nonlocalized_file) + "/" + locale + ".lproj/" + os.path.basename(nonlocalized_file)
if __name__ == '__main__':
update_all_nibs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment