Skip to content

Latest commit

 

History

History
64 lines (50 loc) · 3.11 KB

README.md

File metadata and controls

64 lines (50 loc) · 3.11 KB

GenerateI18NConstants

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

What it does

👍 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.

What it doesn't do

❌ Run genstrings.
🚫 Allow language switching via code (still have to change languages in Settings.app).
❗️ Currently doesn't support 'Localizable.stringsdict'

Usage

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!")

Installation

  • 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).

Build Script

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}"

Authors

David Casserly - @devedup
Daniel Love - @4eleven7

Contribute

  1. Fork
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

GenerateI18NConstants is available under the MIT license. See the LICENSE file for more info.