title: Swift 3: Set Finder label color
body: <p>I'm trying to set the colored labels shown by the finder. The only function I know is setResourceValue. But this needs localized names!</p>
  
  <p>I could image my mother language and english as well, but all others I don't know. I can't believe, that this should be the way. </p>
  
  <p>Is the are translation function, which takes a standard parameter like an enum or int and delivers the localized color name?</p>
  
  <p>I have an running part, but only for two languages (German and English):</p>
  
  <pre><code>let colorNamesEN = [ "None", "Gray", "Green", "Purple", "Blue", "Yellow", "Red", "Orange" ]
  let colorNamesDE = [ "",     "Grau", "Grün",  "Lila",   "Blau", "Gelb",   "Rot", "Orange" ]
  
  public enum TagColors : Int8 {
      case None = -1, Gray, Green, Purple, Blue, Yellow, Red, Orange, Max
  }
  
  //let theURL : NSURL = NSURL.fileURLWithPath("/Users/dirk/Documents/MyLOG.txt")
  
  extension NSURL {
      // e.g.  theURL.setColors(0b01010101)
      func tagColorValue(tagcolor : TagColors) -&gt; UInt16 {
          return 1 &lt;&lt; UInt16(tagcolor.rawValue)
      }
  
      func addTagColor(tagcolor : TagColors) -&gt; Bool {
          let bits : UInt16 = tagColorValue(tagcolor) | self.getTagColors()
          return setTagColors(bits)
      }
  
      func remTagColor(tagcolor : TagColors) -&gt; Bool {
          let bits : UInt16 = ~tagColorValue(tagcolor) &amp; self.getTagColors()
          return setTagColors(bits)
      }
  
      func setColors(tagcolor : TagColors) -&gt; Bool {
          let bits : UInt16 = tagColorValue(tagcolor)
          return setTagColors(bits)
      }
  
      func setTagColors(colorMask : UInt16) -&gt; Bool {
          // get string for all available and requested bits
          let arr = colorBitsToStrings(colorMask &amp; (tagColorValue(TagColors.Max)-1))
  
          do {
              try self.setResourceValue(arr, forKey: NSURLTagNamesKey)
              return true
          }
          catch {
              print("Could not write to file \(self.absoluteURL)")
              return false
          }
      }
  
      func getTagColors() -&gt; UInt16 {
          return getAllTagColors(self.absoluteURL)
      }
  }
  
  
  // let initialBits: UInt8 = 0b00001111
  func colorBitsToStrings(colorMask : UInt16) -&gt; NSArray {
      // translate bits to (localized!) color names
      let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode)!
  
      // I don't know how to automate it for all languages possible!!!!
      let colorNames = countryCode as! String == "de" ? colorNamesDE : colorNamesEN
  
      var tagArray = [String]()
      var bitNumber : Int = -1   // ignore first loop
      for colorName in colorNames {
          if bitNumber &gt;= 0 {
              if colorMask &amp; UInt16(1&lt;&lt;bitNumber) &gt; 0 {
                  tagArray.append(colorName)
              }
          }
          bitNumber += 1
      }
      return tagArray
  }
  
  
  func getAllTagColors(file : NSURL) -&gt; UInt16 {
      var colorMask : UInt16 = 0
  
      // translate (localized!) color names to bits
      let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode)!
      // I don't know how to automate it for all languages possible!!!!
      let colorNames = countryCode as! String == "de" ? colorNamesDE : colorNamesEN
      var bitNumber : Int = -1   // ignore first loop
  
      var tags : AnyObject?
  
      do {
          try file.getResourceValue(&amp;tags, forKey: NSURLTagNamesKey)
          if tags != nil {
              let tagArray = tags as! [String]
  
              for colorName in colorNames {
                  if bitNumber &gt;= 0 {
                      // color name listed?
                      if tagArray.filter( { $0 == colorName } ).count &gt; 0 {
                          colorMask |= UInt16(1&lt;&lt;bitNumber)
                      }
                  }
                  bitNumber += 1
              }
          }
      } catch {
          // process the error here
      }
  
      return colorMask
  }
  </code></pre>