Skip to content

Instantly share code, notes, and snippets.

@Lutzifer
Last active September 18, 2018 15:13
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lutzifer/3e7d967f73e38b57d4355f23274f303d to your computer and use it in GitHub Desktop.
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'
#/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
# 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})"
@madsb
Copy link

madsb commented Jan 22, 2017

Thanks for this! I had to change the regex string to NSLocalizedString(\"[^\")]*\", comment:\s*\"[^\"]*\") before I could get it to work, though.

@seasox
Copy link

seasox commented Jun 20, 2018

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