<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Roger Molas</title>
    <description>The latest articles on DEV Community by Roger Molas (@rogermolas).</description>
    <link>https://dev.to/rogermolas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F92419%2Fd910d821-7cf0-484d-87dd-c45785c5da1c.jpeg</url>
      <title>DEV Community: Roger Molas</title>
      <link>https://dev.to/rogermolas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rogermolas"/>
    <language>en</language>
    <item>
      <title>Access class name using @objc attribute</title>
      <dc:creator>Roger Molas</dc:creator>
      <pubDate>Thu, 19 Mar 2020 12:41:55 +0000</pubDate>
      <link>https://dev.to/rogermolas/access-class-name-using-objc-attribute-df0</link>
      <guid>https://dev.to/rogermolas/access-class-name-using-objc-attribute-df0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a Simple Table View app&lt;/li&gt;
&lt;li&gt;Load customs cells each row&lt;/li&gt;
&lt;li&gt;Cell identifier is the class name if the object is TableCell cell identifier is "TableCell"&lt;/li&gt;
&lt;li&gt;Use common cell loading techniques&lt;/li&gt;
&lt;li&gt;Use helper method to load cell using @objc attribute&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Create UITableViewCell subclass&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class HeaderCell: UITableViewCell {
    ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ContentsCell: UITableViewCell {
    ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AboutCell: UITableViewCell {
    ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Load cell in Tableview in traditional way&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&amp;gt; UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as? HeaderCell
        return cell
    }
    if indexPath.row == 2 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContentsCell", for: indexPath) as? ContentsCell
        return cell
    }
    if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AboutCell", for: indexPath) as? AboutCell
        return cell
    }
    return UITableViewCell()
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So whats &lt;strong&gt;@objc&lt;/strong&gt; attribute?&lt;br&gt;
When you apply it to a class or method it instructs Swift to make those things available to Objective-C as well as Swift code.&lt;/p&gt;

&lt;p&gt;Most of the time you see &lt;code&gt;@objc&lt;/code&gt; when you call a method from a UIBarButtonItem or UIButton, you’ll need to mark that method using &lt;code&gt;@objc&lt;/code&gt; so it’s exposed – both of those, and many others, are Objective-C code.&lt;/p&gt;

&lt;p&gt;In this case, we will use &lt;code&gt;@objc&lt;/code&gt; to name our subclasses &lt;code&gt;HeaderCell&lt;/code&gt;, &lt;code&gt;ContentsCell&lt;/code&gt;, and &lt;code&gt;AboutCell&lt;/code&gt; in Swift&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update our UITableViewCell subclass&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@objc(HeaderCell)
class HeaderCell: UITableViewCell {
    ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@objc(ContentsCell)
class ContentsCell: UITableViewCell {
    ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@objc(AboutCell)
class AboutCell: UITableViewCell {
    ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a Helper method to take the cell identifier using the AnyClass type from the UITableViewCell subclass.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: Cell identifier should be the class name&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func getCell(_ cls: AnyClass, _ index: IndexPath) -&amp;gt; AnyObject {
    let identifier = NSStringFromClass(cls)
    let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: index)
    return cell
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Load cell in Tableview using new way&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&amp;gt; UITableViewCell {
    if indexPath.row == 0 {
        let cell = getCell(HeaderCell.self, indexPath) as! HeaderCell
        return cell
    }
    if indexPath.row == 2 {
        let cell = getCell(ContentsCell.self, indexPath) as! ContentsCell
        return cell
    }
    if indexPath.row == 1 {
        let cell = getCell(AboutCell.self, indexPath) as! AboutCell
        return cell
    }
    return UITableViewCell()
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Looks clean right?, so &lt;code&gt;@objc&lt;/code&gt; attribute is not just for methods, we use it to name our class as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Addition Tricks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's make it shorter, create an enum of rows called &lt;code&gt;Row&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum Row: Int {
    case HeaderCell = 0
    case ContentsCell = 1
    case AboutCell  = 2

    var className: AnyClass {
        switch self {
            case .Header:
                return HeaderCell.self
            case .Contents:
                return ContentsCell.self
            case .About:
                return AboutCell.self
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&amp;gt; UITableViewCell {
    let row = Row(rawValue: indexPath.row)
    let cell = getCell(row.className, indexPath) // dynamic binding
    return cell
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it!&lt;br&gt;
Thanks for your time&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>objectivec</category>
    </item>
    <item>
      <title>iOS and Android Localization Tool</title>
      <dc:creator>Roger Molas</dc:creator>
      <pubDate>Mon, 13 Aug 2018 12:45:44 +0000</pubDate>
      <link>https://dev.to/rogermolas/ios-localization-tool-2pgf</link>
      <guid>https://dev.to/rogermolas/ios-localization-tool-2pgf</guid>
      <description>&lt;p&gt;Localization is simply the process of translating your app into multiple languages.&lt;/p&gt;

&lt;p&gt;In situation like you need support multiple language, including API response messages and dynamic strings you need a list of localizable .strings file, and you need to localized it based on the Language you want  ( e.g &lt;strong&gt;&lt;em&gt;English&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;Chinese&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;Japanese&lt;/em&gt;&lt;/strong&gt; ). &lt;/p&gt;

&lt;p&gt;Xcode has a &lt;strong&gt;built-in&lt;/strong&gt; localizable file generator that generate your localizable .strings for each language you supported.&lt;/p&gt;

&lt;p&gt;In this format&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;en.lproj  // directory
   |- localizable.strings

zh.lproj  // directory
   |- localizable.strings

ja.lproj  // directory
   |- localizable.strings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thats Cool!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;But how about the contents?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You need to copy and paste it base on it corresponding laguage. For Chinese stings you put it on &lt;strong&gt;zh.lproj/ localizable.strings&lt;/strong&gt; and  for Japanese stings put it on &lt;strong&gt;ja.lproj/ localizable.strings&lt;/strong&gt; and your good to go.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Holy Shiiit, i have 400 stings that needs to be localized in 3 or more languages&lt;/em&gt;&lt;/strong&gt; 400 x  3 = 1,200&lt;/p&gt;

&lt;p&gt;Just copy and paste your 1,200 lines of string, Good Luck!&lt;/p&gt;

&lt;h3&gt;
  
  
  To solve the pain of copy and pasting and save time.
&lt;/h3&gt;

&lt;p&gt;you can use the tool called &lt;a href="https://github.com/rogermolas/csv-localizer"&gt;&lt;strong&gt;csv-localizer&lt;/strong&gt;&lt;/a&gt; &lt;a href="https://github.com/rogermolas/csv-localizer"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MqqI7exj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.shields.io/github/stars/badges/shields.svg%3Fstyle%3Dsocial%26label%3DStars" alt="GitHub stars"&gt;&lt;/a&gt; available on Github.&lt;/p&gt;

&lt;p&gt;Just instruct you translator or someone working on translations to put all translated strings into CSV file based on the format provided.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;csv-localizer&lt;/strong&gt; can be installed from homebrew via&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew tap rogermolas/csv-localizer
&lt;span class="nv"&gt;$ &lt;/span&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;csv-localizer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Convert using the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;csv-localizer &lt;span class="nt"&gt;-p&lt;/span&gt; ios &lt;span class="nt"&gt;-i&lt;/span&gt; path/csv_files/ &lt;span class="nt"&gt;-o&lt;/span&gt; path/output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Input directory, CSV files directory path, should not include the .csv file&lt;/em&gt;&lt;br&gt;
&lt;em&gt;"path/csv_files/&lt;/em&gt;" &lt;strong&gt;and not&lt;/strong&gt; "&lt;em&gt;&lt;del&gt;path/csv_files/strings.csv&lt;/del&gt;&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The reason for that is, csv-localizer can process multiple csv files, thats why its only accept  a directory path.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Generated files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;en.lproj  // directory
   |- localizable.strings

zh.lproj  // directory
   |- localizable.strings

ja.lproj  // directory
   |- localizable.strings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thats it,  your ready to go. Easy Right?&lt;/p&gt;

&lt;p&gt;It will generate the same format just like what Xcode did, but this time it has its localized contents in each corresponding laguage. :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Also worked on Android it will generate &lt;code&gt;string.xml&lt;/code&gt;&lt;/strong&gt; with contents&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;csv-localizer &lt;span class="nt"&gt;-p&lt;/span&gt; android &lt;span class="nt"&gt;-i&lt;/span&gt; path/csv_files/ &lt;span class="nt"&gt;-o&lt;/span&gt; path/output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget give a  &lt;a href="https://github.com/rogermolas/csv-localizer"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MqqI7exj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.shields.io/github/stars/badges/shields.svg%3Fstyle%3Dsocial%26label%3DStars" alt="GitHub stars"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>android</category>
      <category>localization</category>
      <category>tools</category>
    </item>
  </channel>
</rss>
