Skip to content

Commit

Permalink
Added pluralize
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilvagomez committed Apr 30, 2019
1 parent fe1ffb9 commit 203b315
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 11 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
All notable changes to this project will be documented in this file.
`Localize` adheres to [Semantic Versioning](http://semver.org/).

## [2.3.0](https://github.com/andresilvagomez/Localize/releases/tag/2.3.0)

Released on 2019-04-30.

- Pluralizations
- By [Andres Silva](https://github.com/andresilvagomez).

---

## [2.2.0](https://github.com/andresilvagomez/Localize/releases/tag/2.2.0)

Released on 2019-03-28.
Expand Down
4 changes: 2 additions & 2 deletions Localize.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|

s.name = "Localize"
s.version = "2.2.0"
s.version = "2.3.0"
s.license = 'MIT'
s.summary = "Localize is a framework writed in swift to localize your projects easier improves i18n, including storyboards and strings."
s.homepage = "https://github.com/andresilvagomez/Localize"
s.author = { "Andres Silva" => "[email protected]" }
s.source = { :git => "https://github.com/andresilvagomez/Localize.git", :tag => "2.2.0" }
s.source = { :git => "https://github.com/andresilvagomez/Localize.git", :tag => "2.3.0" }

s.ios.deployment_target = '9.0'
s.source_files = "Source/*.swift"
Expand Down
12 changes: 12 additions & 0 deletions Localize.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
9F9345581E291D4E00C400F7 /* Localize.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 69F1B5741D955990000A2B87 /* Localize.framework */; };
9F989F4C1E2B3B8200A19F67 /* BaseTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F989F411E2B34E700A19F67 /* BaseTest.swift */; };
9F989F4D1E2B3B8300A19F67 /* OtherLangTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F989F441E2B34E700A19F67 /* OtherLangTest.swift */; };
9F9CC4BE22788BBF00B66CEE /* Pluralize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F9CC4BD22788BBF00B66CEE /* Pluralize.swift */; };
9F9CC4C0227898AA00B66CEE /* PluralizeJsonTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F9CC4BF227898AA00B66CEE /* PluralizeJsonTest.swift */; };
9F9CC4C422789DEB00B66CEE /* PluralizeStringsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F9CC4C322789DEB00B66CEE /* PluralizeStringsTest.swift */; };
9FA3BC491E3174980054CA92 /* BaseTestInSpanish.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FA3BC481E3174980054CA92 /* BaseTestInSpanish.swift */; };
9FA3BC511E318C080054CA92 /* ReadingOtherFiles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FA3BC501E318C080054CA92 /* ReadingOtherFiles.swift */; };
9FCD8EA51E52AD3E00B5909C /* StringsChanginDefaultFileName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCD8EA41E52AD3E00B5909C /* StringsChanginDefaultFileName.swift */; };
Expand Down Expand Up @@ -98,6 +101,9 @@
9F989F411E2B34E700A19F67 /* BaseTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTest.swift; sourceTree = "<group>"; };
9F989F421E2B34E700A19F67 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9F989F441E2B34E700A19F67 /* OtherLangTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OtherLangTest.swift; sourceTree = "<group>"; };
9F9CC4BD22788BBF00B66CEE /* Pluralize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pluralize.swift; sourceTree = "<group>"; };
9F9CC4BF227898AA00B66CEE /* PluralizeJsonTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PluralizeJsonTest.swift; sourceTree = "<group>"; };
9F9CC4C322789DEB00B66CEE /* PluralizeStringsTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PluralizeStringsTest.swift; sourceTree = "<group>"; };
9FA3BC481E3174980054CA92 /* BaseTestInSpanish.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestInSpanish.swift; sourceTree = "<group>"; };
9FA3BC4A1E318BCB0054CA92 /* other-en.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "other-en.json"; sourceTree = "<group>"; };
9FA3BC4C1E318BDC0054CA92 /* some-it.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "some-it.json"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -164,6 +170,7 @@
9F2FC5681E427AFD00C15610 /* LocalizeStrings.swift */,
2B717464207D9AFE0070DC5F /* LocalizeUI.swift */,
9FE9F0C821FACAA800034A34 /* LocalizeConfig.swift */,
9F9CC4BD22788BBF00B66CEE /* Pluralize.swift */,
9F9345461E291A1800C400F7 /* Suporting Files */,
);
path = Source;
Expand Down Expand Up @@ -197,6 +204,8 @@
2BAA7AA3207FC9F6008E4E3A /* UIViewComponentsES.swift */,
2BAA7AA5207FCA8B008E4E3A /* UIViewComponentsWithString.swift */,
2B8E83BA21810F6500DB21FB /* UIViewComponentsWithDisabledAutoLocalize.swift */,
9F9CC4BF227898AA00B66CEE /* PluralizeJsonTest.swift */,
9F9CC4C322789DEB00B66CEE /* PluralizeStringsTest.swift */,
9F989F4A1E2B3B5600A19F67 /* Supporting Files */,
);
path = Tests;
Expand Down Expand Up @@ -379,6 +388,7 @@
9FE9F0C921FACAA800034A34 /* LocalizeConfig.swift in Sources */,
9F93453F1E29192300C400F7 /* LocalizeExtensions.swift in Sources */,
9F2FC5671E42793B00C15610 /* LocalizeJson.swift in Sources */,
9F9CC4BE22788BBF00B66CEE /* Pluralize.swift in Sources */,
9F9345411E29192300C400F7 /* Localize.swift in Sources */,
9F9345421E29192300C400F7 /* LocalizeString.swift in Sources */,
9F761AD71E305B9B00904B4B /* LocalizeStatic.swift in Sources */,
Expand All @@ -400,9 +410,11 @@
9F989F4D1E2B3B8300A19F67 /* OtherLangTest.swift in Sources */,
9FA3BC511E318C080054CA92 /* ReadingOtherFiles.swift in Sources */,
2B8E83BC2181100A00DB21FB /* UIViewComponentsWithDisabledAutoLocalize.swift in Sources */,
9F9CC4C422789DEB00B66CEE /* PluralizeStringsTest.swift in Sources */,
2BAA7AA6207FCA8B008E4E3A /* UIViewComponentsWithString.swift in Sources */,
2BAA7AA4207FC9F6008E4E3A /* UIViewComponentsES.swift in Sources */,
9F19DFC71E4D2B1D00F953E6 /* StringBaseTest.swift in Sources */,
9F9CC4C0227898AA00B66CEE /* PluralizeJsonTest.swift in Sources */,
9FA3BC491E3174980054CA92 /* BaseTestInSpanish.swift in Sources */,
9FCD8EA91E53327D00B5909C /* JsonChanginFileName.swift in Sources */,
B6EECE2A215AD8CE007D8A59 /* StringFallbackTest.swift in Sources */,
Expand Down
86 changes: 83 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/andresilvagomez/Localize/master/LICENSE)
[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/vsouza/awesome-ios)

Localize is a framework written in swift to help you localize your projects. It supports both storyboards and strings.
Localize is a framework written in swift to help you localize and pluralize your projects. It supports both storyboards and strings.

![Localize Storyboard](https://www.dropbox.com/s/t5uij0bg0tgignu/localize.gif?raw=1)
___
Expand All @@ -24,6 +24,7 @@ ___
## Features

- [x] Storyboard with IBInspectable
- [x] Pluralize and localize your keys
- [x] Keep the File.strings files your app already uses
- [x] Support Apple strings and JSON Files
- [x] Change your app language without changing device language
Expand Down Expand Up @@ -55,7 +56,7 @@ platform :ios, '9.0'
use_frameworks!

target '<Your Target Name>' do
pod 'Localize' , '~> 2.2.0'
pod 'Localize' , '~> 2.3.0'
end

# If you are using Swift 4.x
Expand Down Expand Up @@ -363,14 +364,93 @@ func application(_ application: UIApplication, didFinishLaunchingWithOptions lau

```

## Pluralize

```swift
print( "people".pluralize(value: 0) )
// there are no people

print( "people".pluralize(value: 1) )
// only one person

print( "people".pluralize(value: 2) )
// two people

print( "people".pluralize(value: 27) )
// many people

print( "people".pluralize(value: 103) )
// hundreds of people

print( "people".pluralize(value: 1010) )
// thousand of people

print( "people".pluralize(value: 1000000) )
// millions of people
```

how you need compose your file.

```json
// Json file

{
"people": {
"zero": "there are no people",
"one": "only one person",
"two": "two people",
"many": "many people",
"hundreds": "hundreds of people",
"thousand": "thousand of people",
"millions": "millions of people",
"other": "not defined people"
}
}
```

```strings
# string file
"people.zero" = "there are no people";
"people.one" = "only one person";
"people.two" = "two people";
"people.many" = "many people";
"people.hundreds" = "hundreds of people";
"people.thousand" = "thousand of people";
"people.millions" = "millions of people";
"people.other" = "not defined people";
```

but also you can show your value

```swift
print( "people".pluralize(value: 1) )
/// 1 Person
```

in your file

```json
// JSON
{
"people": {
"one": "% Person",
...
}
}

// Strings
"people.one" = "% Person";
```

___

## Notes for your AppStore release

To make all languages you have localized your app for visible on the AppStore, you must add a language in the project's settings.

1. For that, click on your project name in the left side bar.
2. Then, choose project, instead of a target.
2. Then, choose project, instead of a target.
3. At the bottom, under *Localizations*, press the + button & select a language you want to add
4. On prompt, uncheck all files Xcode wants to add localization for, but keep a single one, that you won't actually localize, such as your launch screen for instance.
- if you need to localize all your files, I suggest adding a placeholder storyboard file that you'll then add to localization
Expand Down
2 changes: 1 addition & 1 deletion Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.0.1</string>
<string>2.3.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
6 changes: 3 additions & 3 deletions Source/LocalizeExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,13 @@ extension UIViewController {
get { return autoLocalizeValue() }
set { setAutoLocalizeValue(value: newValue) }
}

/// Localizable tag storeged property
@IBInspectable public var localizeTitle: String? {
get { return localizedValueFor(key: &localizeKey1) }
set { setLocalized(value: newValue, key: &localizeKey1) }
}

/// Override awakeFromNib when is going visible, try search a key in JSON File
/// If key match replace text, if can't match return the key (original text)
/// Set title and placeholder for UITextField
Expand All @@ -392,7 +392,7 @@ extension UIViewController {
NotificationCenter.localize(observer: self, selector: #selector(localize))
}
}

/// Here we change text with key replacement
@objc public func localize() {
LocalizeUI.localize(key: &localizeTitle, value: &title)
Expand Down
11 changes: 11 additions & 0 deletions Source/LocalizeString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,15 @@ public extension String {

return Localize.localize(key: self, dictionary: values, tableName: tableName)
}

/// Pluralize strings with numeric value
/// get localized and pluralized key acording with the rules
/// and value.
///
/// - parameter String: The value could you pluralize
///
/// - returns: Localized and Pluralized key according with the value.
func pluralize(value: Int, tableName: String? = nil) -> String {
return Pluralize.pluralize(key: self, value: value, tableName: tableName)
}
}
50 changes: 50 additions & 0 deletions Source/Pluralize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Pluralize.swift
// Localize
//
// Copyright © 2019 @andresilvagomez.
//

import Foundation

fileprivate extension Int {
/// Get plural string value for Int value.
var plural: String {
switch self {
case 0:
return "zero"
case 1:
return "one"
case 2:
return "two"
case 3...99:
return "many"
case 100...999:
return "hundreds"
case 1_000...999_999:
return "thousand"
case 1_000_000...999_999_999:
return "millions"
default:
return "other"
}
}
}

/// Pluralize is a class focused in reduce your app logic for localized values
/// you can pluralize
public class Pluralize {

/// Pluralize strings with numeric value
/// get localized and pluralized key acording with the rules
/// and value.
///
/// - parameter String: The value could you pluralize
///
/// - returns: Localized and Pluralized key according with the value.
static func pluralize(key: String, value: Int, tableName: String? = nil) -> String {
let key = "\(key).\(value.plural)"

return key.localize(value: "\(value)", tableName: tableName)
}
}
40 changes: 40 additions & 0 deletions Tests/PluralizeJsonTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// PluralizeJsonTest.swift
// LocalizeTests
//
// Copyright © 2019 @andresilvagomez.
//

import XCTest
import Localize

class PluralizeJsonTest: XCTestCase {
override func setUp() {
super.setUp()
Localize.update(provider: .json)
Localize.update(bundle: Bundle(for: type(of: self)))
Localize.update(language: "en")
}

func testPluralize() {
XCTAssertEqual("people".pluralize(value: 0), "there are no people")
XCTAssertEqual("people".pluralize(value: 1), "only one person")
XCTAssertEqual("people".pluralize(value: 2), "two people")
XCTAssertEqual("people".pluralize(value: 27), "many people")
XCTAssertEqual("people".pluralize(value: 103), "hundreds of people")
XCTAssertEqual("people".pluralize(value: 1010), "thousand of people")
XCTAssertEqual("people".pluralize(value: 1000000), "millions of people")
XCTAssertEqual("people".pluralize(value: -1), "not defined people")
}

func testPluralizeTable() {
XCTAssertEqual("other.people".pluralize(value: 0, tableName: "other"), "there are no people")
XCTAssertEqual("other.people".pluralize(value: 1, tableName: "other"), "only one person")
XCTAssertEqual("other.people".pluralize(value: 2, tableName: "other"), "two people")
XCTAssertEqual("other.people".pluralize(value: 27, tableName: "other"), "many people")
XCTAssertEqual("other.people".pluralize(value: 103, tableName: "other"), "hundreds of people")
XCTAssertEqual("other.people".pluralize(value: 1010, tableName: "other"), "thousand of people")
XCTAssertEqual("other.people".pluralize(value: 1000000, tableName: "other"), "millions of people")
XCTAssertEqual("other.people".pluralize(value: -1, tableName: "other"), "not defined people")
}
}
42 changes: 42 additions & 0 deletions Tests/PluralizeStringsTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// PluralizeStringsTest.swift
// LocalizeTests
//
// Created by Andres Silva Gomez on 4/30/19.
// Copyright © 2019 Kekiiwaa. All rights reserved.
//

import XCTest
import Localize

class PluralizeStringsTest: XCTestCase {
override func setUp() {
super.setUp()
Localize.update(provider: .strings)
Localize.update(bundle: Bundle(for: type(of: self)))
Localize.update(language: "en")
Localize.update(fileName: "Strings")
}

func testPluralize() {
XCTAssertEqual("people".pluralize(value: 0), "there are no people")
XCTAssertEqual("people".pluralize(value: 1), "only one person")
XCTAssertEqual("people".pluralize(value: 2), "two people")
XCTAssertEqual("people".pluralize(value: 27), "many people")
XCTAssertEqual("people".pluralize(value: 103), "hundreds of people")
XCTAssertEqual("people".pluralize(value: 1010), "thousand of people")
XCTAssertEqual("people".pluralize(value: 1000000), "millions of people")
XCTAssertEqual("people".pluralize(value: -1), "not defined people")
}

func testPluralizeTable() {
XCTAssertEqual("other.people".pluralize(value: 0, tableName: "Other"), "there are no people")
XCTAssertEqual("other.people".pluralize(value: 1, tableName: "Other"), "only one person")
XCTAssertEqual("other.people".pluralize(value: 2, tableName: "Other"), "two people")
XCTAssertEqual("other.people".pluralize(value: 27, tableName: "Other"), "many people")
XCTAssertEqual("other.people".pluralize(value: 103, tableName: "Other"), "hundreds of people")
XCTAssertEqual("other.people".pluralize(value: 1010, tableName: "Other"), "thousand of people")
XCTAssertEqual("other.people".pluralize(value: 1000000, tableName: "Other"), "millions of people")
XCTAssertEqual("other.people".pluralize(value: -1, tableName: "Other"), "not defined people")
}
}
Loading

0 comments on commit 203b315

Please sign in to comment.