Slide 1

Slide 1 text

SWIFT Scripting LOCALIZATION AltConf 2016

Slide 2

Slide 2 text

Hi! I’m Ayaka. @AYANONAGON

Slide 3

Slide 3 text

WORKFLOW

Slide 4

Slide 4 text

GERMANY CHINA JAPAN

Slide 5

Slide 5 text

ENGLISH = > 300 MILLION NATIVE SPEAKERS

Slide 6

Slide 6 text

SPANISH = > 400 MILLION NATIVE SPEAKERS

Slide 7

Slide 7 text

CHINESE = > 1.3 BILLION NATIVE SPEAKERS

Slide 8

Slide 8 text

MORE POTENTIAL USERS !

Slide 9

Slide 9 text

MORE USERS !

Slide 10

Slide 10 text

MORE PROFIT !

Slide 11

Slide 11 text

MORE MONEY TO PUT BACK INTO YOUR PRODUCT !"

Slide 12

Slide 12 text

HAPPY LONG-TERM USERS !

Slide 13

Slide 13 text

NSLocalizedString

Slide 14

Slide 14 text

MYCLASS.SWIFT let okButtonTitle = NSLocalizedString("OK", comment: "Alert button confirmation title") let cancelButtonTitle = NSLocalizedstring("Cancel", comment: "Alert button cancel title")

Slide 15

Slide 15 text

$ genstrings *.swift

Slide 16

Slide 16 text

BASE.LPROJ/LOCALIZABLE.STRINGS /* Alert button confirmation title */ "OK" = "OK"; /* Alert button cancel title */ "Cancel" = "Cancel";

Slide 17

Slide 17 text

NL.LPROJ/LOCALIZABLE.STRINGS /* Alert button confirmation title */ "OK" = "OK"; /* Alert button cancel title */ "Cancel" = "Annuleer";

Slide 18

Slide 18 text

JP.LPROJ/LOCALIZABLE.STRINGS /* Alert button confirmation title */ "OK" = "OK"; /* Alert button cancel title */ "Cancel" = "Ωϟϯηϧ";

Slide 19

Slide 19 text

“Run”

Slide 20

Slide 20 text

“Run a Workflow”

Slide 21

Slide 21 text

“Run a marathon”

Slide 22

Slide 22 text

“Run a Workflow” “Run a marathon”

Slide 23

Slide 23 text

ʮWorkflowΛ࣮ߦ͢Δʯ ʮϚϥιϯΛ૸Δʯ

Slide 24

Slide 24 text

࣮ߦ VS ૸Δ

Slide 25

Slide 25 text

let runWorkflowTitle = NSLocalizedString("Run", comment: "Run workflow title") /* Run workflow title */ "Run" = "Run";

Slide 26

Slide 26 text

let goOnARunTitle = NSLocalizedString("Run", comment: "Go on a run title") /* Go on a run title */ "Run" = "Run";

Slide 27

Slide 27 text

let runWorkflowTitle = NSLocalizedString("Run", comment: "Run workflow title") let goOnARunTitle = NSLocalizedString("Run", comment: "Go on a run title") /* Run workflow title */ "Run" = "Run"; /* Go on a run title */ "Run" = "Run";

Slide 28

Slide 28 text

/* Run workflow title */ "run-workflow.button.title" = "Run"; /* Go on a run title */ "go-on-a-run.button.title" = "Run";

Slide 29

Slide 29 text

/* Run workflow title */ "RUN_WORKFLOW_BUTTON_TITLE" = "Run"; /* Go on a run title */ "GO_ON_A_RUN_BUTTON_TITLE" = "Run";

Slide 30

Slide 30 text

/* Run workflow title */ "RUN_WORKFLOW_BUTTON_TITLE" = "࣮ߦ"; /* Go on a run title */ "GO_ON_A_RUN_BUTTON_TITLE" = "૸Δ";

Slide 31

Slide 31 text

NSLocalizedString("RUN_WORKFLOW_BUTTON_TITLE", comment: "Run workflow title") NSLocalizedString("GO_ON_A_RUN_BUTTON_TITLE", comment: "Go on a run title")

Slide 32

Slide 32 text

NSLocalizedString("Run", comment: "Run workflow title") NSLocalizedString("Run", comment: "Go on a run title")

Slide 33

Slide 33 text

/* Run workflow title */ "Run" = "Run"; /* Go on a run title */ "Run" = "Run";

Slide 34

Slide 34 text

/* Run workflow title */ "run-run_workflow_title" = "Run"; /* Go on a run title */ "run-go_on_a_run_title" = "Run";

Slide 35

Slide 35 text

NSLocalizedString("Run", comment: "Run workflow title") NSLocalizedString("Run", comment: "Go on a run title") /* Run workflow title */ "run-run_workflow_title" = "Run"; /* Go on a run title */ "run-go_on_a_run_title" = "Run";

Slide 36

Slide 36 text

GOAL #1: Effortless NSLOCALIZEDSTRING-ING

Slide 37

Slide 37 text

GOAL #2: CONTINUOUS LOCALIZATION

Slide 38

Slide 38 text

FETCH LOCALIZABLE.STRINGS FROM THE ‘

Slide 39

Slide 39 text

GOALS 1. Effortless NSLocalizedString-ing via autogenerating localization keys from comments 2. Continuous localization via loading strings from the ‘

Slide 40

Slide 40 text

NSLocalizedString WFLocalizedString

Slide 41

Slide 41 text

$ genstrings -o en.lproj -s WFLocalizedString

Slide 42

Slide 42 text

$ genstrings -o en.lproj -s WFLocalizedString

Slide 43

Slide 43 text

Script IT

Slide 44

Slide 44 text

IN SWIFT —

Slide 45

Slide 45 text

WHY SWIFT? 1. Familiar 2. Or new! 3. Fewer language dependencies 4. Type-safe 5. But still feels light-weight & script friendly 6. Fun!

Slide 46

Slide 46 text

THE PLAN 1. Get all the .swift, .m, .mm files 2. Parse out all of the WFLocalizedStrings from each file 3. Write them all out to Localizable.strings

Slide 47

Slide 47 text

STRING PARSING

Slide 48

Slide 48 text

[self setTitle:WFLocalizedString(@"Create Workflow") forState:UIControlStateNormal]; [self setTitle:WFLocalizedStringWithDescription(@"Create Workflow", @"Button") forState:UIControlStateNormal];

Slide 49

Slide 49 text

REGULAR EXPRESSIONS !

Slide 50

Slide 50 text

NSREGULAR EXPRESSIONS ! !

Slide 51

Slide 51 text

"WFLocalizedString\\(@\"([^\"]*)\"\\)"

Slide 52

Slide 52 text

"WFLocalizedStringWithDescription\\(@\"([^ \"]*)\", @\"([^\"]*)\"\\)"

Slide 53

Slide 53 text

func getMatches(in line: String) throws -> [TextCheckingResult] { let pattern = "WFLocalizedString\\(@\"([^\"]*)\"\\)" let regex = try RegularExpression(pattern: pattern, options: []) let matches = regex.matches(in: line, options: [], range: NSRange(location: 0, length: line.utf16.count)) if matches.count > 0 { return matches } let descriptivePattern = "WFLocalizedStringWithDescription\\(@\"([^\"]*)\", @\"([^\"]*)\"\\)" let descriptiveRegex = try RegularExpression(pattern: descriptivePattern, options: []) return descriptiveRegex.matches(in: line, options: [], range: NSRange(location: 0, length: line.utf16.count)) }

Slide 54

Slide 54 text

struct LocalizedString { let string: String let description: String? } func getLocalizedStrings(in line: String) throws -> [LocalizedString] { let matches = try getMatches(in: line) return matches.map { let line = line as NSString let string = line.substring(with: $0.range(at: 1)) var description: String? = nil if $0.numberOfRanges > 2 { description = line.substring(with: $0.range(at: 2)) } return LocalizedString(string: string, description: description) } }

Slide 55

Slide 55 text

PLAYGROUNDS TO THE RESCUE!

Slide 56

Slide 56 text

DEMO

Slide 57

Slide 57 text

Script?

Slide 58

Slide 58 text

HELLO-WORLD.SWIFT print("Hello AltConf 2016!")

Slide 59

Slide 59 text

XCRUN SWIFT HELLO-WORLD.SWIFT

Slide 60

Slide 60 text

CHMOD +X HELLO-WORLD.SWIFT

Slide 61

Slide 61 text

#!/USR/BIN/ENV XCRUN SWIFT

Slide 62

Slide 62 text

HELLO-WORLD.SWIFT #!/usr/bin/env xcrun swift print("Hello AltConf 2016!")

Slide 63

Slide 63 text

./HELLO-WORLD.SWIFT

Slide 64

Slide 64 text

DEMO

Slide 65

Slide 65 text

WHAT NOW?

Slide 66

Slide 66 text

Translation

Slide 67

Slide 67 text

LESS Scriptable

Slide 68

Slide 68 text

But lots of fun!

Slide 69

Slide 69 text

PEOPLE ARE GREAT WITH NUANCES

Slide 70

Slide 70 text

͜ͷϔΞελΠϧɺͲ (WHAT DO YOU THINK OF THIS HAIRSTYLE?)

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

͜ͷϔΞελΠϧɺͲ (WHAT DO YOU THINK OF THIS HAIRSTYLE?)

Slide 73

Slide 73 text

͏ΜɺͪΐͬͱͶ… (YEAH, A LITTLE…)

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

͏ΜɺͪΐͬͱͶ… ((THAT’S REALLY BAD. YOU SHOULD GO BACK TO THE HAIR SALON AND HAVE IT FIXED.))

Slide 76

Slide 76 text

Who are the best localizers?

Slide 77

Slide 77 text

Who understands your product the most?

Slide 78

Slide 78 text

USERS!

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Script THE TEDIOUS PARTS. Focus ON THE NUANCED PARTS.

Slide 83

Slide 83 text

TIPS ▸ Make all user facing strings an NSLocalizedString even if you don’t have plans to localize the app in the nearest future. ▸ Avoid using spaces for layout padding like @" OK" ▸ Avoid concatenating strings to make sentences.

Slide 84

Slide 84 text

MORE READING ▸ Apple’s iOS Developer Library: Localizing Your App ▸ objc.io Issue 9: String Localization ▸ My Swift Summit London talk

Slide 85

Slide 85 text

COOL PROJECTS & INSPIRATION ▸ SwiftGen by AliSoftware ▸ twine by mobiata

Slide 86

Slide 86 text

GITHUB.COM/AYANONAGON/TALKS

Slide 87

Slide 87 text

Thank you! ??? @AYANONAGON

Slide 88

Slide 88 text

No content