Skip to content

congncif/LocalizableTextGenerator

Repository files navigation

LocalizableTextGenerator

Your application wants to be popular all over the world. It has to be translated into many different languages. That is when localizable text comes. But when working with localizable text, you often have to insert the localizable key as plain text into your code. When you have a lot of text used it is easy to mess up and it's hard to manage.

Take a look at the example below

  label1.text = "welcome_message".localized
  label2.text = "welcome_title".localized
  • This, in addition to the aesthetics of code, also makes your code quite difficult to change, for example, when you want to change a localizable key, you need to search and replace all the key in your source code.
  • When you want to enter a key, Xcode does not automatically complete the code for you and it creates a lot of annoyance.

To resolve this, create a Text.swift file with structrure:

struct Text {
    static let welcomeMessage = "welcome_message".localized
    static let welcomeTitle = "welcome_title".localized
}

And replace plain localizable key by Text values. The new code looks like:

label1.text = Text.welcomeTitle
label2.text = Text.welcomeMessage

Auto completion code works now

Very cool! 🙌

But this work is not very good because you have to type localizable keys many times. It takes time and is not fun at all. You need a tool to solve all of these problems and that is when Localizable Text Generator comes.

  1. To install, add pod 'SiFUtilities/Localize' to Podfile. Then run pod install.

This command installs Localization feature based on Localize-Swift and a tool for generating Text.swift file by build pharse scripts named localizable2appstrings.

  1. On your application targets’ Build Phases settings tab, click the + icon and choose New Run Script Phase. Create a Run Script in which you specify your shell (ex: /bin/sh), add the following contents to the script area below the shell:
export PATH=/usr/local/bin:$PATH
cd "$PROJECT_DIR"

"${PODS_ROOT}"/SiFUtilities/Localize/localizable2appstrings path/to/Localizable.strings path/to/Text.swift
  1. Press Command + B to build your project. The Text.swift file is generated automatically.
/*Generated by localizable2appstrings automatically. Don't change the content of this file!*/

import SiFUtilities
import Foundation

struct Text {
    static let welcomeMessage = "welcome_message".localized
    static let welcomeTitle = "welcome_title".localized
}

Awesome 💐🌹

Now everytime you change the Localizable.strings file, you just press Command + B and everything will be automatically synced into the Text file for you.

Add a localziable text

The Text file after rebuild

You do not have to worry about Localizaton anymore. Happy coding👏

The full example source code: https://github.com/congncif/LocalizableTextGenerator

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published