andresilvagomez / localize Goto Github PK
View Code? Open in Web Editor NEWLocalize is a framework writed in swift to localize your projects easier improves i18n, including storyboards and strings.
License: MIT License
Localize is a framework writed in swift to localize your projects easier improves i18n, including storyboards and strings.
License: MIT License
I have a static table that loads localized properly.
Once the language change, the rows' text change to the default (used in the cell xib)
My localized JSON files are in a sub-folder
// Localize
let localize = Localize.shared
localize.update(provider: .json) // Set your localize provider.
localize.update(fileName: "lang") // Set your file name
// Set language based on system
let locale = Locale.preferredLanguages.first?.components(separatedBy: "-").first?.lowercased() ?? "en"
localize.update(language: locale)
Here the code I'm using for creating the table
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "configCellIdentifier", for: indexPath) as! CellWithIconTableViewCell
cell.accessoryType = .disclosureIndicator
// THIS IS THE JSON FILE
//"account": {
// "update-language": "Choose the new language",
// "static-table": {
// "ranking": "Check your rank",
// "account": "Account",
// "support": "Support",
// "about": "About",
// "language": "App language"
// }
//},
switch indexPath.section {
case 0:
switch indexPath.row {
case 0:
cell.setProperties(icon: UIImage(named: "glyphicons-basic-393-medal"), text: "account.static-table.ranking".localized)
return cell
case 1:
cell.setProperties(icon: UIImage(named: "glyphicons-basic-4-user"), text: "account.static-table.account".localized)
return cell
default:
break
}
case 1:
switch indexPath.row {
case 0:
cell.setProperties(icon: UIImage(named: "glyphicons-basic-587-bug"), text: "account.static-table.support".localized)
return cell
case 1:
cell.setProperties(icon: UIImage(named: "glyphicons-basic-196-circle-empty-info"), text: "account.static-table.about".localized)
return cell
case 2:
cell.setProperties(icon: UIImage(named: "glyphicons-basic-419-worl-east"), text: "account.static-table.language".localized)
cell.accessoryType = .none
return cell
default:
break
}
default:
break
}
return UITableViewCell()
}
This is the code used for changing the language
private func chooseLanguage() {
let actionSheet = UIAlertController(
title: nil,
message: "account.update-language".localize(),
preferredStyle: UIAlertController.Style.actionSheet
)
for language in Localize.availableLanguages {
let displayName = Localize.displayNameForLanguage(language)
let languageAction = UIAlertAction(
title: displayName,
style: .default,
handler: { (_: UIAlertAction!) -> Void in
Localize.update(language: language)
})
actionSheet.addAction(languageAction)
}
let cancelAction = UIAlertAction(
title: "general.alerts.cancel".localize(),
style: UIAlertAction.Style.cancel,
handler: nil
)
actionSheet.addAction(cancelAction)
self.present(actionSheet, animated: true, completion: nil)
}
I expect to see the right localization
The rows present the default text
Hi!
I am trying to localize a key like this:
"This_is_key" = "You have % apples";
But it always fallback to base language. (in my case, I localize for Vietnamese). But the key without parameters still working in Vietnamese.
I tried so many ways but it still not working.
Please check
Base on your example, what if I want to keep the string "app.label" without remove its key. For example, I want to localize "app.label" in the first viewcontroller but I don't want to localize "app.label" in the second viewcontroller. Any solutions ?
The library does not handle properly languages that are coded by iOS in three components. I have tried to include translation for both simplified and traditional Chinese. The language variable is set as follow:
However, I cannot create json resource with the name zh-Hans-US / zh-Hant-US as last component encode the regional settings. In my case it is US. I have tried to name files: lang-zh-Hant.json and lang-zh-Hans.json but it results in selecting default language as the library always fall back to the single component file name (lang-zh.json) which also cannot be found.
/// Please put here your config
ℹ Please replace this with what you did.
ℹ Please replace this with what you expected to happen.
ℹ Please replace this with of what happened instead.
First of all let me thank you for the awesome library. It just saved me lots of code and helped keep things neat.
One feature I think would add value is option to add an external datasource other than App's Bundle. So, we could read from JSON files that is in application's documents directory. This helps to make the localisation files dynamic.
A simple use case would be the ability to push any changes to the strings to the app from server. The app could just replace contents in the documents directory. I am assuming we cannot alter contents in the Bundle. Correct me if I am wrong.
A new feature I'd love to have in this library (which would take huge efforts, I believe, though) would be to integrate the feature that Laurine has.
Ported over, it would look something like this:
lang-en.json
{
"login": {
"welcome": "Hey %, what's up?"
"username": "Username",
"password": "Password"
}
}
ViewController.swift
welcomeLabel.text = Localize.login.welcome(value: "Andres") // Hey Andres, what's up?
usernameLabel.text = Localize.login.username
That'd be super awesome!
How would I go about localizing the following UIElements...
UISegmentedControl
's titlesUITextField
's placeholders?...all in storyboard?
I used Localizable.strings
according to Apple guideline, and it works with
NSLocalizedString("key", comment: "")
Now I try to use Localize and "key".localized
and it display key
, not the proper value
ℹ Please provide a quick description for this issue.
Xcode - 11.5
MacOS - 10.15.3
Swift - 5
Pod::Spec.new do |s|
s.platform = :ios
s.ios.deployment_target = '11.1'
s.name = "*"
s.summary = " for customer."
s.requires_arc = true
s.version = "0.1.2"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Swapnil G" => "**" }
s.homepage = "***"
s.source = { :git => "***",
:tag => "#{s.version}" }
s.ios.framework = "UIKit"
s.framework = "WebKit"
s.ios.framework = "HealthKit"
s.dependency 'SQLite.swift','> 0.12.2'> 4.9.1'
s.dependency 'Alamofire', '
s.dependency 'Charts', '> 3.4.0'> 1.3.0'
s.dependency 'CryptoSwift', '
s.dependency 'NVActivityIndicatorView', '> 4.8.0'> 2.3.0'
s.dependency 'SwiftSoup', '
s.dependency 'SwiftyJSON', '> 5.0.0'> 2.3.13'
s.dependency 'DropDown', '
s.dependency 'HGCircularSlider', '> 2.2.0'> 4.3.2'
s.dependency 'RealmSwift', '
s.dependency 'HGCircularSlider', '> 2.2.0'> 2.3.13'
s.dependency 'DropDown', '
s.source_files = ".framework/Headers/*.h"
s.public_header_files = ".framework/Headers/*.h"
s.vendored_frameworks = "**.framework"
s.resource_bundle = { "**" => ["Localize/.lproj/.strings"] }
s.swift_version = "5.0"
end
Added strings file for hindi and english language checked with normal framework (not binary framework), it works as expected but not with the binaryframework
Localization should work for binaryframework also.
Validation failed with error - [iOS] file patterns: The resource_bundles
pattern for Framework_Bundle
did not match any file.
My project is mixing between swift and objective-c.
I can't find the instruction to use with objective-c view controller.
Greetings, dear Localize developer!
Your work is immaculate and useful beyond belief. Your guides are simple and easy to follow and It helped me save tons of time. The only thing I was wondering about is why not display languages in their original, i.e. action sheet now displays list of languages in the language currently chosen.
PS. For some reason, O'zbek is written as O'Zbek (capital Z), could you please fix that too. I'd do it myself, I just don't know how.
When trying to use SPM we got errors like:
NextLevel/NextLevel#185
dropbox/SwiftyDropbox#252
Please add localization for attribute strings i.e UILabel.
Hi,
This is not an issue according to this pod. I already used this pod in another iOS application. It is nice and very easy to understand and easy to implement.
I have a situation here. I am working a project which already contains localization file for the two languages. The code is written is like this for the text.
NSLocalizedString("Use_Thumb", comment: "")
Is there any possibility to use this pod to handle the Localizable.strings file. If possible. How to use it.
The project is updated to swift 4.2 version.
please give suggestions.
For error messages, I try to generically access the localized strings.
Example json:
"errors": {
"some_error": "Something went wrong!",
"some_other_error": "We're not sure what's happening..."
}
From the API, I receive an error code and would like to do:
let errorCode = ... // from API response
let errorMessage = "errors.\(errorCode)".localized
Now, in case I forgot to add localization for an error code, or the backend added a new error code, before I was able to update the app, I'd like to do a fallback, when there's no localized string for this key.
Currently, I compare the returned string to the key:
let key = "errors.\(errorCode)"
if key.localized != key {
errorMessage = "There's an unknown error."
}
However, I'd much rather do something like this:
let key = "errors.\(errorCode)"
if Localize.shared.localizationExists(forKey: key) {
See a possible implementation for this below:
/// Determines whether a localized string exists for given key
///
/// - parameter key: localization key
/// - returns: boolean value determining whether a localized string exists for give key
public func localizationExists(forKey key: String) -> Bool {
return key.localized != key
}
System button with default title/change in runtime not localized correctly
Localize.update(fileName: "Localization")
Localize.update(defaultLanguage: "en-US")
Localize.update(language: "en-VN")
I had a button (from storyboard) with default title is "Start Reading". In the runtime, that button can be change to "Resume chap.1" for example. It work perfect until I hit that button, the title fade to "Start Reading" instead of "Resume chap.1". The title only change when click and hold the finger on it, after releasing, it fade back to correct title "Resume..." The button I use is system type
Button type: Default system type
Default title: "Start Reading"
Runtime title: "Resume chap.1"
Click & hold button: Fade to "Start Reading"
Release button: Fade to "Resume chap.1"
CocoaPods still shows 2.1.0 as latest:
https://cocoapods.org/pods/Localize
Please release 2.2.0 there! Thanks!
ℹ Please provide a quick description for this issue.
/// Please put here your config
lang-en, lang-fr, lang-sv
It shows only fr and sv 2 options, not showing en options.
ℹ It does not add english option
.for language in Localize.availableLanguages {
let displayName = Localize.displayNameForLanguage(language)
let languageAction = UIAlertAction(
title: displayName,
style: .default,
handler: { (_: UIAlertAction!) -> Void in
Localize.update(language: language)
})
actionSheet.addAction(languageAction)
}
Hi, I have a problem using this localization framework.
Here is the step I have done
let localize = Localize.shared
localize.update(provider: .json)
localize.update(fileName: "lang")
localize.update(defaultLanguage: "en")
print(localize.language())
print(localize.availableLanguages())
print("hello.world".localize())
somewhere in the viewControlleren
[]
hello.world
Am I missing something? or they cannot find my "lang-en.json" file?
Thank you
Hi, thanks for your work.
I am trying to translate an app that is built with a tableviewcontroller and each row has a separate tableviewcell file, and each tableviewcell has a collectionviewcell. This is to present a view like NetFlix or the AppStore: collectionviews embedded in tableviews.
The problem with the translation comes when I want to translate in the HomeTableViewController file. Because the strings to translate are in the TableViewCell file, that has no viewdidload or viewwillappear.
Any help would be very much appreciated.
localize storyboard on the fly using Storyboard String file Not working
Is there a way to set the fallback languages if I want something different from what apple does?
Hi, Does this library support NSLocalizedStringFromTable ?
crash with localize codes like "fr-LU", "en-DE" ...
First off, thanks for this awesome library. I love how simple, yet flexible, it is!
Keep up the good work!
I've setup localization for English and German: lang-en.json
, lang-de.json
.
It works perfectly when manually setting the language in code by doing localize.update(language: "de")
.
However, it does not automatically set the language when changing my device's language.
Am I missing something?
I figured I could manually set the language to the device's language by passing...
Locale.preferredLanguages.first?.components(separatedBy: "-").first.lowercased ?? "en"
...as language code.
Is this the way it's supposed to be?
This is not proper pluralization
It does not work for basically any other language than English, see
https://unicode-org.github.io/cldr-staging/charts/37/supplemental/language_plural_rules.html
For example "Czech" language has 4 categories:
"few" = 2-4, but your code always assigns 3 to "many"
Doesn't grab both strings when put into one.
"kingsen.game.mode.hearts".localize() + " kingsen.game.mode.jack".localize(), in an array
For Chinese language "zh-Hans" and "zh-Hant" and Indonesia in Xcode localization actually leads to "id-ID", not "id". So the best would be while allowing a fallback or some other way.
ℹ Please provide a quick description for this issue.
Tabbar string not refreshed until app restart
/// Please put here your config
UITabBarItem(title: "More_title".localize(), image: UIImage(named: "icon-more-menu"), tag: 4)
ℹ I try to change the language from setting page.
ℹ The tabbar text should change to respective language immediately.
ℹ It didnot changed to latest updated language.
On iOS 8 app crashes when You change the language after some ViewControllers was destroyed.
It crashes on line:
NotificationCenter.default.post(name: Notification.Name(rawValue: LocalizeChangeNotification), object: nil)
In method:
public func update(language:Languages) -> Void
Of class:
LocalizeCommonProtocol
It most likely because of not removing the observer from NotificationCenter that are added in awakeFromNib
methods in extensions of UIControlls in file LocalizeExtensions
.
This is not needed for iOS9 and above.
http://useyourloaf.com/blog/unregistering-nsnotificationcenter-observers-in-ios-9/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.