/** * Creates a locale * * @param string|int $country country of the locale * @param string|int $language language of the locale (optional) * @param string|int $currency currency of the locale (optional) */ public function __construct($country, $language = null, $currency = null) { $this->_country = self::parseCountry($country); // Set language from user input or from country default if (is_numeric($language)) { $language = KlarnaLanguage::getCode($language); } $this->_language = $language; // Set currency from user input or from country default if (is_numeric($currency)) { $currency = KlarnaCurrency::getCode($currency); } $this->_currency = $currency; }
/** * Get a translated text from the language pack * * @param string $text the string to be translated * @param string|int $language target language, iso code or KlarnaLanguage * * @return string the translated text */ public function fetch($text, $language) { if (is_numeric($language)) { $language = KlarnaLanguage::getCode($language); } else { $language = strtolower($language); } // XPath query to get translation $xpath = "//string[@id='{$text}']/{$language}"; $aResult = (array) @$this->_xml->xpath($xpath); if (count($aResult) > 0) { return strval($aResult[0]); } // Fallback to the english text if ($language != 'en') { return $this->fetch($text, 'en'); } // Or failing that, the placeholder return $text; }
/** * Returns the language code for the set language constant. * * @param int {@link KlarnaLanguage Language} constant. * @return string Two letter code, e.g. "da", "de", etc. */ public function getLanguageCode($language = null) { $language = $language === null ? $this->language : $language; $code = KlarnaLanguage::getCode($language); return $code === null ? '' : $code; }
/** * Returns the language code for the set language constant. * * @param int $language {@link KlarnaLanguage Language} constant. * * @return string Two letter code, e.g. "da", "de", etc. */ public function getLanguageCode($language = null) { if ($language === null) { $language = $this->_language; } $code = KlarnaLanguage::getCode($language); return (string) $code; }