Code View

mstring / mstring-1.3.1.2 / src / Message.cpp
// SPDX-License-Identifier: MIT
#include "Message.hh"

#include <algorithm>
#include <utility>

//
// Parameter
//
Parameter::Parameter( std::string name, std::string type )
  : Name( std::move( name ) ),
    Type( std::move( type ) )
{
  // blank
}

//
// TextElement
//
TextElement::TextElement( bool m )
  : Macro( m )
{
  // blank
}

TextElement::TextElement( std::string text, bool m )
  : Macro( m ),
    Text( std::move( text ) )
{
  // blank
}

//
// LocaleMessage
//
LocaleMessage::LocaleMessage( std::string language, std::string encode )
  : Language( std::move( language ) ),
    Encode( std::move( encode ) )
{
  // blank
}

bool LocaleMessage::operator== ( const LocaleMessage &locale ) const
{
  return Language == locale.Language && Encode == locale.Encode;
}

void LocaleMessage::AppendText( const std::string &text )
{
  if ( text.empty( ) )
  {
    return;
  }

  if ( ! TextElements.empty( ) && ! TextElements.back( ).Macro )
  {
    TextElements.back( ).Text.append( text );
  }
  else
  {
    TextElements.emplace_back( text, false );
  }
}

//
// Message
//
Message::Message( std::string name, std::string defaultLanguage, const MessageSettings &settings )
  : Name( std::move( name ) ),
    DefaultLanguage( std::move( defaultLanguage ) ),
    Settings( std::make_shared<const MessageSettings>( settings ) )
{
  // blank
}

const LocaleMessage *Message::DefaultLocale( ) const
{
  if ( Locales.empty( ) )
  {
    return nullptr;
  }

  auto const found = std::find_if( Locales.begin( ), Locales.end( ),
                                   [this]( const LocaleMessage &locale )
                                   { return locale.Language == DefaultLanguage; } );
  return found != Locales.end( ) ? &*found : &Locales.front( );
}

LocaleMessage &Message::Locale( const std::string &language, const std::string &encode )
{
  LocaleMessage const key( language, encode );
  auto const found = std::find( Locales.begin( ), Locales.end( ), key );

  if ( found != Locales.end( ) )
  {
    return *found;
  }

  Locales.push_back( key );
  return Locales.back( );
}

const Parameter *Message::FindParameter( const std::string &name ) const
{
  auto const found = std::find_if( Parameters.begin( ), Parameters.end( ),
                                   [&name]( const Parameter &parameter )
                                   { return parameter.Name == name; } );
  return found != Parameters.end( ) ? &*found : nullptr;
}