{"id":108,"date":"2009-08-14T11:42:27","date_gmt":"2009-08-14T11:42:27","guid":{"rendered":"http:\/\/www.blogs.abeazam.com\/dev\/?p=108"},"modified":"2009-08-14T11:42:27","modified_gmt":"2009-08-14T11:42:27","slug":"code-based-localisation","status":"publish","type":"post","link":"http:\/\/www.blogs.abeazam.com\/dev\/2009\/08\/code-based-localisation\/","title":{"rendered":"Code Based Localisation"},"content":{"rendered":"<p><strong>Code Based Localisation in xCode 3.0+ SDK<\/strong><\/p>\n<p>Code based localisation focuses on any strings you may have in your code. Finding these strings and making them dynamic. This is a contrast to how localisation is done in say Flash where the localisation methods are normally set up by the developer. Kind of like Flex.<\/p>\n<p>If you are after NIB Localisation checkout this post <a href=\"http:\/\/www.blogs.abeazam.com\/dev\/?p=98\" target=\"_self\">here<\/a><\/p>\n<p><strong>Abstract:<br \/>\n<\/strong>A localised project needs a local specific folder that follows a name convention made up of 2 parts. First is the language like &#8220;English&#8221;, &#8220;French&#8221; or &#8220;Japanese&#8221;. The second is the local project abbreviation &#8220;.lproj&#8221; putting them together you get folders by the name &#8220;English.lproj&#8221;, &#8220;French.lproj&#8221; or &#8220;Japanese.lproj&#8221;. All the local based copy and assets will end up in the respective local folder. You can create these folders manually but I like to get xCode to create them using the localisation functionality built into the IDE.<\/p>\n<p>Basically any text that is set via code needs to be set using NSLocalizedString this takes 2 strings. The first is the the copy that needs localisation. The second string it takes is the comment that appears above the name value pair.\u00a0 see example:<\/p>\n<p>[c]\/\/use this to set the default copy to &quot;Please enter user name:&quot; in English<br \/>\nself.userNameFeild.text = NSLocalizedString(@&quot;Please Enter your user nsme;&quot;, @&quot;user name subbmision copy&quot;);<br \/>\n\/\/use this to set the default copy to &quot;Please enter user name:&quot; in french<br \/>\nself.userNameFeild.text = NSLocalizedString(@&quot;Veuillez \u00e9crire le nom d&#8217;utilisateur:&quot;,@&quot;Veuillez \u00e9crire le nom d&#8217;utilisateur&quot;);[\/c]<\/p>\n<p>Once you have written your code using NSLocalizedString with all the necessary default copy you will run a terminal script that will pull out the default strings strings to create a .strings file which you would then localise. &#8220;genstrings&#8221; is the command line tool used to interrogate your .m files to find what needs to be localised.<\/p>\n<p>Once you have your localised .strings file you simply copy it to the different local folders updated the copy to the respective local and bobs\u00a0 your uncle, its done.<\/p>\n<p><strong>Implementation:<\/strong><\/p>\n<ol style=\"margin-top: 0px; margin-bottom: 0px;\">\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">create a new project in xcode it should not matter what type but I will use an iPhone View-Based Application.<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">add the bellow code to the viewControler of the newly created project to quickly add some uilabels<br \/>\n[c]- (void)viewDidLoad {<br \/>\nUILabel *uiLableFirstName =\u00a0 [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 50)];<br \/>\nuiLableFirstName.text = NSLocalizedString(@&quot;First name:&quot;, @&quot;first Name&quot;);<br \/>\n[self.view addSubview:uiLableFirstName];<\/p>\n<p>UILabel *uiLableSurName =\u00a0 [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 150, 50)];<br \/>\nuiLableSurName.text = NSLocalizedString(@&quot;Last name:&quot;, @&quot;sure Name to use&quot;);<br \/>\n[self.view addSubview:uiLableSurName];<\/p>\n<p>[super viewDidLoad];<br \/>\n}[\/c]<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Open the terminal window and navigate too the root of you project folder. This is the folder that contains your Classes folder.<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Run &#8220;genstrings .\/Classes\/*.m&#8221; in the terminal which will scan every .m file in your Classes folder for any references to NSLocalizedString and added them to a Localizable.strings file which should appear in the root of your project folder.<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">In the finder navigate to the root of the project folder and then drag and drop Localizable.strings from the Finder to the resource folder in XCode remembering to insure that the &#8220;Copy items into&#8230;&#8221; check box is unchecked. Also make sure that Text Encoding is set to UTF-16 to insure all languages are covered.<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Finally press add.<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Right-Click the newly added Localizable.strings then select &#8220;Get Info&#8221;.<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Select the General tab from the &#8220;Get Info&#8221; panel<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Click &#8220;Make file Localizable&#8221;<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Click back on the general tab<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Click &#8220;Add Localization&#8221;<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Enter your new Locals &#8220;French&#8221;(from the English name section of the link) and or see here for full list: http:\/\/www.loc.gov\/standards\/iso639-2\/php\/English_list.php<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Once you have added the Locals for Localizable.strings close the &#8220;Get Info&#8221; window<\/li>\n<li style=\"margin-top: 0px; margin-bottom: 0px;\">Run your app go to the settings panel&gt;&gt;Genral&gt;&gt;International change your local and it should display your localised text.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Code Based Localisation in xCode 3.0+ SDK Code based localisation focuses on any strings you may have in your code. Finding these strings and making them dynamic. This is a contrast to how localisation is done in say Flash where the localisation methods are normally set up by the developer. Kind of like Flex. If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[26,28,36,37,53],"tags":[66,98,69,71,100,101,73,105],"_links":{"self":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/108"}],"collection":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":0,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}