summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_l10n_util.cc
blob: 2c66ff9beb72bb92642a5fe33b46b9cf44f9f2e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_l10n_util.h"

#include <set>
#include <string>
#include <vector>

#include "app/l10n_util.h"
#include "base/file_util.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_message_bundle.h"
#include "chrome/common/json_value_serializer.h"

namespace errors = extension_manifest_errors;
namespace keys = extension_manifest_keys;

namespace extension_l10n_util {

std::string GetDefaultLocaleFromManifest(const DictionaryValue& manifest,
                                         std::string* error) {
  std::string default_locale;
  if (!manifest.GetString(keys::kDefaultLocale, &default_locale)) {
    *error = errors::kInvalidDefaultLocale;
    return "";
  }
  // Normalize underscores to hyphens.
  std::replace(default_locale.begin(), default_locale.end(), '_', '-');
  return default_locale;
}

bool AddLocale(const std::set<std::string>& chrome_locales,
               const FilePath& locale_folder,
               std::set<std::string>* valid_locales,
               std::string* locale_name,
               std::string* error) {
  // Normalize underscores to hyphens because that's what our locale files use.
  std::replace(locale_name->begin(), locale_name->end(), '_', '-');
  // Accept name that starts with a . but don't add it to the list of supported
  // locales.
  if (locale_name->find(".") == 0)
    return true;
  if (chrome_locales.find(*locale_name) == chrome_locales.end()) {
    // Fail if there is an extension locale that's not in the Chrome list.
    *error = StringPrintf("Supplied locale %s is not supported.",
                          locale_name->c_str());
    return false;
  }
  // Check if messages file is actually present (but don't check content).
  if (file_util::PathExists(
         locale_folder.AppendASCII(Extension::kMessagesFilename))) {
    valid_locales->insert(*locale_name);
  } else {
    *error = StringPrintf("Catalog file is missing for locale %s.",
                          locale_name->c_str());
    return false;
  }

  return true;
}

bool GetValidLocales(const FilePath& locale_path,
                     std::set<std::string>* valid_locales,
                     std::string* error) {
  // Get available chrome locales as a set.
  const std::vector<std::string>& available_locales =
      l10n_util::GetAvailableLocales();
  static std::set<std::string> chrome_locales(available_locales.begin(),
                                              available_locales.end());
  // Enumerate all supplied locales in the extension.
  file_util::FileEnumerator locales(locale_path,
                                    false,
                                    file_util::FileEnumerator::DIRECTORIES);
  FilePath locale_folder;
  while (!(locale_folder = locales.Next()).empty()) {
    std::string locale_name =
        WideToASCII(locale_folder.BaseName().ToWStringHack());
    if (!AddLocale(chrome_locales,
                   locale_folder,
                   valid_locales,
                   &locale_name,
                   error)) {
      return false;
    }
  }

  if (valid_locales->empty()) {
    *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed;
    return false;
  }

  return true;
}

// Loads contents of the messages file for given locale. If file is not found,
// or there was parsing error we return NULL and set |error|.
// Caller owns the returned object.
static DictionaryValue* LoadMessageFile(const FilePath& locale_path,
                                        const std::string& locale,
                                        std::string* error) {
  std::string extension_locale = locale;
  // Normalize hyphens to underscores because that's what our locale files use.
  std::replace(extension_locale.begin(), extension_locale.end(), '-', '_');
  FilePath file = locale_path.AppendASCII(extension_locale)
      .AppendASCII(Extension::kMessagesFilename);
  JSONFileValueSerializer messages_serializer(file);
  Value *dictionary = messages_serializer.Deserialize(error);
  if (!dictionary && error->empty()) {
    // JSONFileValueSerializer just returns NULL if file cannot be found. It
    // doesn't set the error, so we have to do it.
    *error = StringPrintf("Catalog file is missing for locale %s.",
                          extension_locale.c_str());
  }

  return static_cast<DictionaryValue*>(dictionary);
}

ExtensionMessageBundle* LoadMessageCatalogs(
    const FilePath& locale_path,
    const std::string& default_locale,
    const std::string& application_locale,
    std::string* error) {
  scoped_ptr<DictionaryValue> default_catalog(
      LoadMessageFile(locale_path, default_locale, error));
  if (!default_catalog.get()) {
    return false;
  }

  scoped_ptr<DictionaryValue> app_catalog(
      LoadMessageFile(locale_path, application_locale, error));
  if (!app_catalog.get()) {
    // Only default catalog has to be present. This is not an error.
    app_catalog.reset(new DictionaryValue);
    error->clear();
  }

  return ExtensionMessageBundle::Create(*default_catalog,
                                        *app_catalog,
                                        error);
}

}  // namespace extension_l10n_util