Last active
September 18, 2018 15:13
-
-
Save Lutzifer/3e7d967f73e38b57d4355f23274f303d to your computer and use it in GitHub Desktop.
Automatically replace NSLocalizedString(...) in Swift with corresponding tr(...) calls for https://github.com/AliSoftware/SwiftGen. Copy both into the project and call 'sh replaceNSlocalized.sh'
This file contains 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
#/bin/sh | |
# Run this inside the project to replace NSLocalizedString calls with swiftgen calls in all .swift files. | |
# Do not forget to make a backup before. | |
find . -type f | grep ".swift" > swiftindex.temp | |
while IFS= read -r filename | |
do | |
grep -o "NSLocalizedString(\"[^\")]*\", comment:\s*\"\")" "$filename" > strings.temp | |
while IFS= read -r localizable | |
do | |
echo $localizable | |
replacement=$(ruby transformToTR.rb $localizable) | |
echo "$replacement" | |
sed -i .bak "s/$localizable/$replacement/g" $filename | |
rm "$filename.bak" | |
done < strings.temp | |
rm strings.temp | |
done < swiftindex.temp | |
rm swiftindex.temp |
This file contains 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
# takeas an NSLocalizedString(...) string and transforms it to the tr(...) format | |
localizable=ARGV[0] | |
def makeFirstLetterUpper(string) | |
string[0].upcase + string[1..-1] | |
end | |
# remove all whitespace and "" and , | |
localizable = localizable.gsub(/\s+/, "").gsub("\"", "").gsub(",", "") | |
# remoe leading and trailing code | |
localizable = localizable.gsub("NSLocalizedString(", "").gsub("comment:)", "") | |
# handle _ like . | |
localizable = localizable.gsub("\_", ".") | |
# uppercase first letters of every word, leaving the rest be as it is | |
localizable = localizable.split(".").map { |a| makeFirstLetterUpper(a) }.join("") | |
puts "tr(.#{localizable})" |
Thanks! I modified your scripts to create R.swift.localizable calls: https://github.com/seasox/RswiftStringTransform
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! I had to change the regex string to
NSLocalizedString(\"[^\")]*\", comment:\s*\"[^\"]*\")
before I could get it to work, though.