Chromium Code Reviews| Index: ios/chrome/tools/strings/generate_localizable_strings.mm |
| diff --git a/ios/chrome/tools/strings/generate_localizable_strings.mm b/ios/chrome/tools/strings/generate_localizable_strings.mm |
| index 95e733f5875e12a7c1531ae1b8aa1aabc454912a..348782fc6e8b0d47c34dd9f3be4a8889ab20d62a 100644 |
| --- a/ios/chrome/tools/strings/generate_localizable_strings.mm |
| +++ b/ios/chrome/tools/strings/generate_localizable_strings.mm |
| @@ -88,22 +88,15 @@ NSString* GetStringFromDataPack(const ui::DataPack& data_pack, |
| return nil; |
| } |
| -NSString* EscapeStringForLocalizableStrings(NSString* string) { |
| - NSString* slashEscapedString = |
| - [string stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]; |
| - return [slashEscapedString stringByReplacingOccurrencesOfString:@"\"" |
| - withString:@"\\\""]; |
| -} |
| - |
| -// Generate the content of the Localizable.string file for |locale| from the |
| -// resource data pack |data_pack|. The content should have the format of: |
| -// "IDS_PRINT_TO_PHONE" = "Print to phone jobs are available."; |
| -// "IDS_SNAPSHOTS" = "Snapshots are available."; |
| -NSString* GenerateLocalizableStringsFileContent(const ui::DataPack& data_pack, |
| - const char* locale, |
| - NSArray* resources, |
| - NSDictionary* resources_ids) { |
| - NSMutableString* localizable_strings = [NSMutableString string]; |
| +// Generates a NSDictionary mapping string IDs to localized strings. The |
| +// dictionary can be written as a Property List (only contains types that |
| +// are valid in Propery Lists). |
| +NSDictionary* GenerateLocalizableStringsDictionary( |
| + const ui::DataPack& data_pack, |
| + const char* locale, |
| + NSArray* resources, |
| + NSDictionary* resources_ids) { |
| + NSMutableDictionary* dictionary = [NSMutableDictionary dictionary]; |
| for (id resource : resources) { |
| NSString* resource_name = nil; |
| NSString* resource_output_name = nil; |
| @@ -128,10 +121,7 @@ NSString* GenerateLocalizableStringsFileContent(const ui::DataPack& data_pack, |
| [[resources_ids objectForKey:resource_name] integerValue]; |
| NSString* string = GetStringFromDataPack(data_pack, resource_id); |
| if (string) { |
| - const char* output_string_name = [resource_output_name UTF8String]; |
| - [localizable_strings |
| - appendFormat:@" \"%s\" = \"%@\";\n", output_string_name, |
| - EscapeStringForLocalizableStrings(string)]; |
| + [dictionary setObject:string forKey:resource_output_name]; |
| } else { |
| fprintf(stderr, "ERROR: fail to load string '%s' for locale '%s'\n", |
| [resource_name UTF8String], locale); |
| @@ -139,7 +129,7 @@ NSString* GenerateLocalizableStringsFileContent(const ui::DataPack& data_pack, |
| } |
| } |
| - return localizable_strings; |
| + return dictionary; |
| } |
| NSDictionary* LoadResourcesListFromHeaders(NSArray* header_list, |
| @@ -199,12 +189,12 @@ NSDictionary* LoadResourcesListFromHeaders(NSArray* header_list, |
| return resources_ids; |
| } |
| -// Save |localizable_strings| with |locale| to |
| -// |output_dir|/|locale|.lproj/|output_filename|. |
| -bool SaveLocalizableFile(NSString* localizable_strings, |
| - NSString* locale, |
| - NSString* output_dir, |
| - NSString* output_filename) { |
| +// Save |dictionary| as a Property List file (in binary1 encoding) |
| +// with |locale| to |output_dir|/|locale|.lproj/|output_filename|. |
| +bool SavePropertyList(NSDictionary* localizable_strings, |
| + NSString* locale, |
| + NSString* output_dir, |
| + NSString* output_filename) { |
| // Compute the path to the output directory with locale. |
| NSString* output_path = [output_dir |
| stringByAppendingPathComponent:[NSString |
| @@ -222,12 +212,20 @@ bool SaveLocalizableFile(NSString* localizable_strings, |
| return false; |
| } |
| + // Convert to property list in binary format. |
| + NSData* data = [NSPropertyListSerialization |
| + dataWithPropertyList:localizable_strings |
| + format:NSPropertyListBinaryFormat_v1_0 |
| + options:0 |
| + error:nil]; |
|
stkhapugin
2016/07/04 13:28:40
Please add error handling. For example, print the
sdefresne
2016/07/04 13:34:12
Done.
|
| + if (!data) { |
| + fprintf(stderr, "ERROR: conversion to property list failed\n"); |
| + return false; |
| + } |
| + |
| // Save the strings to the disk. |
| output_path = [output_path stringByAppendingPathComponent:output_filename]; |
| - if (![localizable_strings writeToFile:output_path |
| - atomically:YES |
| - encoding:NSUTF16StringEncoding |
| - error:nil]) { |
| + if (![data writeToFile:output_path atomically:YES]) { |
| fprintf(stderr, "ERROR: Failed to write out '%s'\n", |
| [output_filename UTF8String]); |
| return false; |
| @@ -358,11 +356,10 @@ int main(int argc, char* const argv[]) { |
| exit(1); |
| } |
| - NSString* localizable_strings = GenerateLocalizableStringsFileContent( |
| + NSDictionary* dictionary = GenerateLocalizableStringsDictionary( |
| *data_pack, [locale UTF8String], output_strings, resources_ids); |
| - if (localizable_strings) { |
| - SaveLocalizableFile(localizable_strings, locale, output_dir, |
| - output_name); |
| + if (dictionary) { |
| + SavePropertyList(dictionary, locale, output_dir, output_name); |
| } else { |
| fprintf(stderr, "ERROR: Unable to create %s.\n", |
| [output_name UTF8String]); |