Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(611)

Unified Diff: ios/chrome/tools/strings/generate_localizable_strings.mm

Issue 2121773002: Generate localizable strings in binary1 property list format. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@{0}
Patch Set: Display localized error. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..cec115448f108af0bc4cef9f624e36ecc2668f64 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* dictionary,
+ 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,22 @@ bool SaveLocalizableFile(NSString* localizable_strings,
return false;
}
+ // Convert to property list in binary format.
+ NSError* error = nil;
+ NSData* data = [NSPropertyListSerialization
+ dataWithPropertyList:dictionary
+ format:NSPropertyListBinaryFormat_v1_0
+ options:0
+ error:&error];
+ if (!data) {
+ fprintf(stderr, "ERROR: conversion to property list failed: %s\n",
+ [[error localizedDescription] UTF8String]);
+ 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 +358,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]);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698