Skip to content

Instantly share code, notes, and snippets.

@sadiq81
Created October 7, 2016 21:33
Show Gist options
  • Save sadiq81/83f3e7656fbc1b8fb2eccdc7352e63a5 to your computer and use it in GitHub Desktop.
Save sadiq81/83f3e7656fbc1b8fb2eccdc7352e63a5 to your computer and use it in GitHub Desktop.
Generate enums for Localizable.strings
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# generate_assets.py
# punchcards
#
# Created by Privat on 13/07/2016.
# Copyright © 2016 Eazy IT. All rights reserved.
import re
LOCALIZABLE_PATH = "./en.lproj/Localizable.strings"
STRINGS_FILE_PATH = "../Classes/Ressources/Strings.swift"
print "Path to strings file ",LOCALIZABLE_PATH
print "Path to Swift file ",STRINGS_FILE_PATH
with open(STRINGS_FILE_PATH, "w") as f:
f.write("//\n\
// Created by Privat on 13/07/2016.\n\
// Copyright (c) 2016 Eazy IT. All rights reserved.\n\
//\n\
\n\
import Foundation\n\
\n\
enum Strings: String {\n\n")
with open(LOCALIZABLE_PATH) as strings:
for line in strings:
searchObj = re.search( r'\".+?\"', line, re.M|re.I)
if searchObj:
string = searchObj.group()
string2 = string[1:-1]
string3 = " case " + string2 + "\n"
f.write(string3)
f.write("\n\
var localized: String {\n\
return Bundle.main.localizedString(forKey: self.rawValue, value: nil, table: nil)\n\
}\n\
}")
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment