R.swift: [Question] how to manually change R.hostingBundle ?

Created on 15 Dec 2017  路  3Comments  路  Source: mac-cain13/R.swift

I need to manually change the language switch in APP

But R.string.localizable like this

  /// en translation: Address copied
  /// 
  /// Locales: en, es, ru
  static let requestAddressCopiedTitle = Rswift.StringResource(key: "request.addressCopied.title", tableName: "Localizable", bundle: R.hostingBundle, locales: ["en", "es", "ru"], comment: nil)

how to switch en er ru ?

Most helpful comment

Unfortunately, there's currently no build-in support in R.swift to manually select a language. It is a feature we'd like to add, though.

As a workaround, this is an extension I currently use in my projects:

import Foundation
import Rswift

extension StringResource {
  public func localized(_ language: String) -> String {
    guard
      let basePath = bundle.path(forResource: "Base", ofType: "lproj"),
      let baseBundle = Bundle(path: basePath)
    else {
      return self.key
    }

    let fallback = baseBundle.localizedString(forKey: key, value: key, table: tableName)

    guard
      let localizedPath = bundle.path(forResource: language, ofType: "lproj"),
      let localizedBundle = Bundle(path: localizedPath)
    else {
      return fallback
    }

    return localizedBundle.localizedString(forKey: key, value: fallback, table: tableName)
  }
}

Usage:

R.string.localizable.welcomeMessage.localized("nl")

All 3 comments

Unfortunately, there's currently no build-in support in R.swift to manually select a language. It is a feature we'd like to add, though.

As a workaround, this is an extension I currently use in my projects:

import Foundation
import Rswift

extension StringResource {
  public func localized(_ language: String) -> String {
    guard
      let basePath = bundle.path(forResource: "Base", ofType: "lproj"),
      let baseBundle = Bundle(path: basePath)
    else {
      return self.key
    }

    let fallback = baseBundle.localizedString(forKey: key, value: key, table: tableName)

    guard
      let localizedPath = bundle.path(forResource: language, ofType: "lproj"),
      let localizedBundle = Bundle(path: localizedPath)
    else {
      return fallback
    }

    return localizedBundle.localizedString(forKey: key, value: fallback, table: tableName)
  }
}

Usage:

R.string.localizable.welcomeMessage.localized("nl")

I am looking forward to this feature in future. Because of developing a multilingual APP. Need to be matched with many languages resources. Like string file , plist file , storyboard , xib file, all this can Localization in different language version. So it should have an interface allow user to custom the Bundle.

We're tracking this feature request further in https://github.com/mac-cain13/R.swift/issues/219

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomlokhorst picture tomlokhorst  路  5Comments

romk1n picture romk1n  路  6Comments

tomlokhorst picture tomlokhorst  路  3Comments

tomlokhorst picture tomlokhorst  路  6Comments

andreadelfante picture andreadelfante  路  6Comments