Home | History | Annotate | Download | only in src
      1 // Copyright (C) 2013 Google Inc.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 // http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #include <libaddressinput/localization.h>
     16 
     17 #include <cassert>
     18 #include <cstddef>
     19 #include <string>
     20 
     21 namespace i18n {
     22 namespace addressinput {
     23 
     24 namespace {
     25 
     26 // For each language code XX with translations:
     27 //    (1) Add a namespace XX here with an include of "XX_messages.cc".
     28 //    (2) Add a wrapper that converts the char pointer to std::string. (GRIT
     29 //        generated functions return char pointers.)
     30 //    (2) Use the XX::GetStdString in the SetLanguage() method below.
     31 namespace en {
     32 
     33 #include "en_messages.cc"
     34 
     35 std::string GetStdString(int message_id) {
     36   const char* str = GetString(message_id);
     37   return str != NULL ? std::string(str) : std::string();
     38 }
     39 
     40 }  // namespace en
     41 
     42 }  // namespace
     43 
     44 Localization::Localization() : get_string_(&en::GetStdString) {}
     45 
     46 Localization::~Localization() {}
     47 
     48 std::string Localization::GetString(int message_id) const {
     49   return get_string_(message_id);
     50 }
     51 
     52 void Localization::SetLanguage(const std::string& language_code) {
     53   if (language_code == "en") {
     54     get_string_ = &en::GetStdString;
     55   } else {
     56     assert(false);
     57   }
     58 }
     59 
     60 void Localization::SetGetter(std::string (*getter)(int)) {
     61   assert(getter != NULL);
     62   get_string_ = getter;
     63 }
     64 
     65 }  // namespace addressinput
     66 }  // namespace i18n
     67