1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// This file declares a helper class for selecting a supported language from a
6// set of candidates.
7
8#ifndef CHROME_INSTALLER_UTIL_LANGUAGE_SELECTOR_H_
9#define CHROME_INSTALLER_UTIL_LANGUAGE_SELECTOR_H_
10
11#include <string>
12#include <vector>
13
14#include "base/basictypes.h"
15
16namespace installer {
17
18// A helper class for selecting a supported language from a set of candidates.
19// By default, the candidates are retrieved from the operating system.
20class LanguageSelector {
21 public:
22  // Default constructor will select from the set of languages supported by the
23  // operating system.
24  LanguageSelector();
25
26  // Constructor for testing purposes.
27  explicit LanguageSelector(const std::vector<std::wstring>& candidates);
28
29  ~LanguageSelector();
30
31  // The offset of the matched language (i.e., IDS_L10N_OFFSET_*).
32  int offset() const { return offset_; }
33
34  // The full name of the candidate language for which a match was found.
35  const std::wstring& matched_candidate() const { return matched_candidate_; }
36
37  // The name of the selected translation.
38  std::wstring selected_translation() const { return GetLanguageName(offset_); }
39
40  // Returns the name of a translation given its offset.
41  static std::wstring GetLanguageName(int offset);
42
43 private:
44  typedef bool (*SelectPred_Fn)(const std::wstring&, int*);
45
46  static bool SelectIf(const std::vector<std::wstring>& candidates,
47                       SelectPred_Fn select_predicate,
48                       std::wstring* matched_name, int* matched_offset);
49  void DoSelect(const std::vector<std::wstring>& candidates);
50
51  std::wstring matched_candidate_;
52  int offset_;
53
54  DISALLOW_COPY_AND_ASSIGN(LanguageSelector);
55};
56
57}  // namespace installer.
58
59#endif  // CHROME_INSTALLER_UTIL_LANGUAGE_SELECTOR_H_
60