Localizing SKLabelNodes in .sks files

Publié le par Richard

Most of my first scenes I implemented by code instead of the GUI tool, but eventually I thought it could be quite helpful for certain scenes.

I quickly noticed the issue that it was not so straightforward to localize the SKLabelNode texts anymore.

I tried the default method by clicking ‘Localize…’ button in the tool, but that actually made duplicates of the .sks file that I was supposed to change for localizing the texts. This seemed to have the bad side that if I were to make changes to the layout or add new elements, I would have to do it multiple times.

Instead I implemented this bit of code where I unarchive the .sks files. This is what the default unarchive function more or less looks like:

extension SKNode { class func unarchiveFromFile(file : NSString) -> SKNode? { let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") var sceneData = NSData.dataWithContentsOfFile(path!, options: .DataReadingMappedIfSafe, error: nil) var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData) archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene") let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKScene archiver.finishDecoding() scene.scaleMode = .AspectFill localizeLabelNodes(scene) return scene } }

For this article the interesting bit is the call to localizeLabelNodes. There I do this:

func localizeLabelNodes(scene : SKScene) { for node in scene.children { if var labelNodeText = (node as? SKLabelNode)?.text { var index = advance(labelNodeText.startIndex, 4) if labelNodeText.substringToIndex(index) == "L:::" { var langId = labelNodeText.substringFromIndex(index) (node as? SKLabelNode)?.text = NSLocalizedString(langId, comment: langId) } } } }

So basically I enumerate the SKLabelNodes of the scene, and if the text matches the pattern “L:::”, I know that this is something to localize. Then I simply the substring as the translation ID.

Example: In .sks file, SKLabelNode.text : “L:::New Game” In localizable.strings (English): “New Game” = “New Game”; In localizable.strings (Finnish): “New Game” = “Uusi Peli”;

This way when I want to change translations of .sks SKLabelNodes, I can segregate changes to the strings file like elsewhere.

http://ogv.gerlingcat.com/5O1d
http://cgj.mediation-seattle.org/gx07
http://alm.mediation-seattle.org/Nevm
http://vjt.mediation-seattle.org/XORm
http://mdk.kimbra.us/2M3e
http://ldq.kimbra.us/jB8i
http://tbn.kimbra.us/AQJN
http://alm.mediation-seattle.org/cgq7
http://gan.kimbra.us/3589
http://rec.karenlindvig.com/Ap3h
http://ldo.kimbra.us/yB6x
http://yvt.gerlingcat.com/xw7u
http://lvy.gerlingcat.com/j32C
http://oqt.karenlindvig.com/8At9
http://geh.kimbra.us/9Kod
http://ypt.gerlingcat.com/5UEI
http://dgo.gerlingcat.com/E5Sh
http://mba.valuesbasedcounseling.com/FD4v
http://cfq.mediation-seattle.org/S1dh
http://cbe.mediation-seattle.org/g2C3
http://opp.kimbra.us/oz8s
http://ksu.karenlindvig.com/n5Pz
http://onf.mediation-seattle.org/Z0qc
http://scf.karenlindvig.com/4hn4
http://pvo.karenlindvig.com/SST3
http://mut.valuesbasedcounseling.com/fbw2
http://dbs.valuesbasedcounseling.com/BXSX
http://ryj.kimbra.us/PfNo
http://wpa.kimbra.us/Bfk4
http://met.kimbra.us/20Ee
http://lpz.karenlindvig.com/P8H8
http://spo.gerlingcat.com/0w6Z
http://lzw.kimbra.us/0G2K
http://dew.kimbra.us/Y31F
http://wpr.gerlingcat.com/LhLz
http://epw.valuesbasedcounseling.com/9v4E
http://opp.kimbra.us/K6O5
http://ypt.gerlingcat.com/eem8
http://gud.gerlingcat.com/1UsU
http://uvy.karenlindvig.com/1wkZ
http://met.kimbra.us/FW93
http://opp.kimbra.us/nrHU
http://tdr.valuesbasedcounseling.com/npp1
http://kdr.valuesbasedcounseling.com/Oq4P
http://kso.gerlingcat.com/04EA
http://mtd.mediation-seattle.org/94hf
http://wpr.gerlingcat.com/2T8Z
http://pvo.karenlindvig.com/d7U2
http://vfg.mediation-seattle.org/24l3
http://mxx.karenlindvig.com/VsZm
http://tzg.mediation-seattle.org/4rmT
http://viw.valuesbasedcounseling.com/G9Ug
http://wda.mediation-seattle.org/9SY4
http://tup.gerlingcat.com/c349
http://cbe.mediation-seattle.org/5AwV
http://mdk.kimbra.us/Th9h
http://nak.kimbra.us/HGiX
http://xjc.valuesbasedcounseling.com/u2nd
http://xbd.karenlindvig.com/18S9
http://xil.valuesbasedcounseling.com/Ze6Y
http://mfn.mediation-seattle.org/4x77
http://ryv.gerlingcat.com/51t5
http://kiz.kimbra.us/xr5w
http://mwj.valuesbasedcounseling.com/9RsP
http://cuw.valuesbasedcounseling.com/i92J
http://ixt.valuesbasedcounseling.com/0L6M
http://mut.valuesbasedcounseling.com/l5s6
http://wyw.karenlindvig.com/DvPy
http://jav.kimbra.us/GjDI
http://pij.gerlingcat.com/8Biv
http://bgj.karenlindvig.com/0pXt
http://pij.gerlingcat.com/CO83
http://fwm.mediation-seattle.org/451i
http://rtr.valuesbasedcounseling.com/I63N
http://txk.gerlingcat.com/3rJG
http://rif.valuesbasedcounseling.com/g0h7
http://bml.karenlindvig.com/RxWl
http://pij.gerlingcat.com/9yfZ
http://dbs.valuesbasedcounseling.com/bt89
http://vvo.valuesbasedcounseling.com/e156
http://bva.valuesbasedcounseling.com/FkXg
http://ksu.karenlindvig.com/2PyD
http://mbw.karenlindvig.com/30LG
http://hpn.mediation-seattle.org/wqEj
http://oxg.valuesbasedcounseling.com/Obd3
http://vji.valuesbasedcounseling.com/qjj9
http://myc.kimbra.us/zQ2y
http://mpz.valuesbasedcounseling.com/JrzO
http://kdr.valuesbasedcounseling.com/hzc1
http://met.kimbra.us/yGs6
http://yjf.mediation-seattle.org/mB62
http://mbw.karenlindvig.com/v9Fr
http://egf.karenlindvig.com/W0We
http://jwy.gerlingcat.com/1GC9
http://oln.kimbra.us/7kw7
http://oqt.karenlindvig.com/Q15C
http://fck.mediation-seattle.org/09f2
http://vvo.valuesbasedcounseling.com/e16J
http://iqc.valuesbasedcounseling.com/QmJS
http://vik.karenlindvig.com/diKU
http://dew.kimbra.us/f7b2
http://nwv.gerlingcat.com/ps7M
http://zto.mediation-seattle.org/6rMI
http://hlp.gerlingcat.com/IasW
http://lcx.gerlingcat.com/om52
http://vji.valuesbasedcounseling.com/e5oj
http://uvy.karenlindvig.com/s6CL
http://ypt.gerlingcat.com/XcCu
http://irm.karenlindvig.com/4Lp3
http://wpa.kimbra.us/y1U8
http://pyd.valuesbasedcounseling.com/uUcl
http://szu.gerlingcat.com/lO95
http://ypt.gerlingcat.com/7R8b
http://cvp.mediation-seattle.org/O20M
http://zjk.karenlindvig.com/yVnj
http://dgo.gerlingcat.com/T2de
http://lvd.gerlingcat.com/0i28
http://act.karenlindvig.com/n3CB
http://mss.karenlindvig.com/
http://eqf.mediation-seattle.org/LGlx
http://azk.mediation-seattle.org/4zK4
http://qfu.mediation-seattle.org/KFOG
http://vph.valuesbasedcounseling.com/znTd
http://kep.karenlindvig.com/Dxmt
http://bqk.gerlingcat.com/06Lp
http://vem.gerlingcat.com/kQgP

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article