Skip to content

Instantly share code, notes, and snippets.

@tskulbru
Created December 22, 2016 13:24
Show Gist options
  • Save tskulbru/c1c0fea561a5a1246d77d4f0d8d33966 to your computer and use it in GitHub Desktop.
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.
#/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
# 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