Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import Foundation
  4.  
  5. // This is a fake implementation mimicking the behavior of having a Localizable.string
  6. // This is used here to be able to test the code easily in a Playground
  7. func localize(key: String) -> String {
  8. return [
  9. "alert.title": "Titre d'Alerte",
  10. "alert.message": "Message d'Alerte",
  11. "greetings.text": "My name is %@ and I'm %d."
  12. ][key] ?? key
  13. }
  14.  
  15. // The real implementation would instead use Localizable.strings:
  16. //func localize(key: String) -> String {
  17. // return NSLocalizedString(key, comment: "")
  18. //}
  19.  
  20.  
  21. // MARK: Version 1
  22.  
  23. enum L10n_v1 : String {
  24. // >>> GENERATE THIS USING A SCRIPT
  25. case AlertTitle = "alert.title"
  26. case AlertMessage = "alert.message"
  27. case Presentation = "greetings.text"
  28. // <<< ---
  29.  
  30. func format(args: CVarArgType...) -> String {
  31. let format = localize(self.rawValue)
  32. return String(format: format, arguments: args)
  33. }
  34. var string : String {
  35. return localize(self.rawValue)
  36. }
  37. }
  38.  
  39. L10n_v1.AlertTitle.string
  40. L10n_v1.Presentation.format("David", 29)
  41.  
  42.  
  43.  
  44. // MARK: Version 2
  45.  
  46.  
  47. enum L10n_v2 : CustomStringConvertible {
  48. // >>> GENERATE THIS USING A SCRIPT
  49. case AlertTitle
  50. case AlertMessage
  51. case Presentation(String, Int)
  52.  
  53. var description : String {
  54. switch self {
  55. case .AlertTitle:
  56. return tr("alert.title")
  57. case .AlertMessage:
  58. return tr("alert.message")
  59. case .Presentation(let first, let last):
  60. return tr("greetings.text", first, last)
  61. }
  62. }
  63. // <<< ---
  64.  
  65. private func tr(key: String, _ args: CVarArgType...) -> String {
  66. let format = localize(key)
  67. return String(format: format, arguments: args)
  68. }
  69. }
  70.  
  71. func tr(key: L10n_v2) -> String { return key.description }
  72.  
  73. tr(.AlertTitle)
  74. tr(.Presentation("David", 29))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement