Created
December 22, 2016 13:24
-
-
Save tskulbru/c1c0fea561a5a1246d77d4f0d8d33966 to your computer and use it in GitHub Desktop.
Converts NBLocalization to Swiftgens tr(.). Based on https://gist.github.com/Lutzifer/3e7d967f73e38b57d4355f23274f303d but changed regex to suit my own need.
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 | |
echo $filename | |
grep -o "NBLocalization.stringForKey(\"[^\")]*\")" "$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, index) | |
if index > 0 | |
return string[0].upcase + string[1..-1] | |
end | |
string | |
end | |
# remove all whitespace and "" and , | |
localizable = localizable.gsub(/\s+/, "").gsub("\"", "").gsub(",", "") | |
# remoe leading and trailing code | |
localizable = localizable.gsub("NBLocalization.stringForKey(", "").gsub(")", "") | |
# handle _ like . | |
localizable = localizable.gsub("\_", ".") | |
# uppercase first letters of every word, leaving the rest be as it is | |
localizable = localizable.split(".").map.with_index { |a, i| makeFirstLetterUpper(a, i) }.join("") | |
puts "tr(.#{localizable})" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment