You are here

class IpLanguageNegotiationLanguageNegotiationIp in IP Language Negotiation 8

Class for identifying language from the IP address.

Plugin annotation


@LanguageNegotiation(
  id = Drupal\ip_language_negotiation\Plugin\LanguageNegotiation\IpLanguageNegotiationLanguageNegotiationIp::METHOD_ID,
  weight = -1,
  name = @Translation("IP address"),
  description = @Translation("Language based on visitor's IP address."),
  config_route_name = "ip_language_negotiation.form"
)

Hierarchy

Expanded class hierarchy of IpLanguageNegotiationLanguageNegotiationIp

File

src/Plugin/LanguageNegotiation/IpLanguageNegotiationLanguageNegotiationIp.php, line 19

Namespace

Drupal\ip_language_negotiation\Plugin\LanguageNegotiation
View source
class IpLanguageNegotiationLanguageNegotiationIp extends LanguageNegotiationMethodBase {

  /**
   * The language negotiation method id.
   */
  const METHOD_ID = 'ip-language-negotiation-ip';

  /**
   * {@inheritdoc}
   */
  public function getLangcode(Request $request = NULL) {
    $langcode = NULL;
    if ($request && $this->languageManager) {

      // Disable caching for this page. This only happens when negotiating
      // based on IP. Once the redirect took place to the correct domain
      // or language prefix, this function is not reached anymore and
      // caching works as expected.
      \Drupal::service('page_cache_kill_switch')
        ->trigger();
      $countries = $this->config
        ->get('ip_language_negotiation.settings')
        ->get('ip_language_negotiation_countries') ?: [];
      $current_ip = $request
        ->getClientIp();

      // Check for debug settings. If enabled, use it.
      if (\Drupal::config('ip2country.settings')
        ->get('debug')) {

        // Debug Country entered.
        if (\Drupal::config('ip2country.settings')
          ->get('test_type') == 0) {
          $country_code = \Drupal::config('ip2country.settings')
            ->get('test_country') ?: 'US';
        }
        else {
          $ip = \Drupal::config('ip2country.settings')
            ->get('test_ip_address') ?: $current_ip;
          $country_code = ip2country_get_country($ip);
        }
      }
      else {
        $country_code = ip2country_get_country($current_ip);
      }
      if (!empty($country_code)) {

        // Check if a language is set for the determined country.
        if (!empty($countries[$country_code])) {
          $langcode = $countries[$country_code];
        }
      }
    }
    return $langcode;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IpLanguageNegotiationLanguageNegotiationIp::getLangcode public function Performs language negotiation. Overrides LanguageNegotiationMethodInterface::getLangcode
IpLanguageNegotiationLanguageNegotiationIp::METHOD_ID constant The language negotiation method id.
LanguageNegotiationMethodBase::$config protected property The configuration factory.
LanguageNegotiationMethodBase::$currentUser protected property The current active user.
LanguageNegotiationMethodBase::$languageManager protected property The language manager.
LanguageNegotiationMethodBase::persist public function Notifies the plugin that the language code it returned has been accepted. Overrides LanguageNegotiationMethodInterface::persist 1
LanguageNegotiationMethodBase::setConfig public function Injects the configuration factory. Overrides LanguageNegotiationMethodInterface::setConfig
LanguageNegotiationMethodBase::setCurrentUser public function Injects the current user. Overrides LanguageNegotiationMethodInterface::setCurrentUser
LanguageNegotiationMethodBase::setLanguageManager public function Injects the language manager. Overrides LanguageNegotiationMethodInterface::setLanguageManager