Coder Social home page Coder Social logo

reactive-localization's Introduction

Reactive localization with RxSwift and SwiftGen

Swift 5.0 iOS 9+ swiftgen 6.1 RxSwift

Overview

The project demonstrates an approach to localize your project with the ability to select interface language in run-time without the need to drastically change your codebase.

Main requirements for a task were to reduce the number of changes needed to be introduced to support real-time language changing and to work with SwiftGen.

To achieve this, I decided to create a custom stencil for a SwiftGen and cover all the reactiveness there.

How to use

  • Add rx_strings.stencil to your project directory.

  • Tune your swiftgen to use that stencil.

At the time of writing, swiftgen does not support running codegen for strings twice, so instead of running simply swiftgen, you need to run it twice - default one and another one with parameters:

${PODS_ROOT}/SwiftGen/bin/swiftgen
${PODS_ROOT}/SwiftGen/bin/swiftgen strings Example/Supporting\ Files/en.lproj/Localizable.strings --templatePath rx_strings.stencil --output Example/Extensions/Swiftgen/RxStrings.swift
  • Now you can use generated code

was:

label.title = L10n.someTitle

become:

label.localizedTitle = RxL10n.someTitle
  • To change language, run Bundle.setLanguage(with:) passing exact language code.

The Example project demonstrates all the described steps.

Implementation details

The rx_strings.stencil is based on flat-swift4.stencil and extends it to create additional helper methods for UIKit's classes, methods to change and manipulate localization schema. It also changes tr method to return Observable<String>. Nothing more changed, so you can copy-paste the code to any other stencil.

To use proper Bundle (e.g. "localized" one), stencil overrides NSLocalizedString method publicly. Each time language changed through Bundle.setLanguage it changes currentLanguage static variable, which triggers any subscribers that use tr method.

To improve API, stencil adds extensions for major UIKit classes with properties like var localizedText: Observable<String> that can be used to directly bind localized text.

reactive-localization's People

Contributors

dependabot[bot] avatar rnkyr avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

ken4r

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.