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
- class \Drupal\language\LanguageNegotiationMethodBase implements LanguageNegotiationMethodInterface
- class \Drupal\ip_language_negotiation\Plugin\LanguageNegotiation\IpLanguageNegotiationLanguageNegotiationIp
Expanded class hierarchy of IpLanguageNegotiationLanguageNegotiationIp
File
- src/
Plugin/ LanguageNegotiation/ IpLanguageNegotiationLanguageNegotiationIp.php, line 19
Namespace
Drupal\ip_language_negotiation\Plugin\LanguageNegotiationView 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;
}
}