Script, written in Swift 3.0, which generates a constants file to enable compile time checking and a more Swift way of localizing strings.
Also available: Fonts
👍 Requires no changes to your existing Localizable.strings.
👏 Generates a Swift enum of your string keys.
✊ Allows compile time checking of your strings.
🙏 More Swifty syntax. Localized.appTesting
instead of NSLocalizedString("app.testing", "app testing")
.
💪 Allows parameters using the .with(...)
method.
❌ Run genstrings.
🚫 Allow language switching via code (still have to change languages in Settings.app).
❗️ Currently doesn't support 'Localizable.stringsdict'
Returns a localized version of the string.
Localized.demoTesting
Returns a localized version of the string, with the parameter 'Dan'.
Localized.demoWelcomeMessage.with("Dan")
Use the built in localisation methods, but with a compile time checked identifier.
NSLocalizedString(Localized.demoGoodbyeMessage.key, comment: "Cya!")
- Open your project, select your Target and select Build Phases
- Add a new Run Script Phase. Maybe name it something like "Generate Localization"
- Copy and paste the build script below into the your new build phase.
- Add your Localizable.strings (or Base.lproj/Localizable.strings if you're localized) to the Input Files section of the build phase script. Example:
$(SRCROOT)/${TARGETNAME}/Base.lproj/Localizable.strings
- Add the Templates/LocalizedTemplate.swift to the Input Files section of the build phase script. Example:
$(SRCROOT)/../Scripts/Templates/LocalizedTemplate.swift
- Add Localized.swift to the Output Files for where your constants will be generated. Example:
$(SRCROOT)/${TARGETNAME}/Classes/Constants/Localized.swift
- Build your project.
Note: The order of your input files does not matter. Just ensure that the template has the word 'template' in its filename. And you maintain the file extensions (.strings, .swift).
SCRIPT_FILE="${SRCROOT}/Scripts/Generate-Swift-L18N-Constants.swift"
echo "Running a custom build phase script: $SCRIPT_FILE"
${SCRIPT_FILE} "${SCRIPT_INPUT_FILE_0}" "${SCRIPT_INPUT_FILE_1}" "${SCRIPT_OUTPUT_FILE_0}"
echo "End of script"
scriptExitStatus=$?
echo "DONE with script: ${SCRIPT_FILE} (exitStatus=${scriptExitStatus})\n\n"
exit "${scriptExitStatus}"
David Casserly - @devedup
Daniel Love - @4eleven7
- Fork
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
GenerateI18NConstants is available under the MIT license. See the LICENSE file for more info.