| @@ -177,8 +177,13 @@ | ||
| 177 | 177 | * @return array |
| 178 | 178 | */ |
| 179 | 179 | public function getLanguagesList() |
| 180 | 180 | { |
| 181 | + if ($this->all_langs) { | |
| 182 | + // return the cached languages | |
| 183 | + return $this->all_langs; | |
| 184 | + } | |
| 185 | + | |
| 181 | 186 | $langs = []; |
| 182 | 187 | $known_langs = VikBooking::getVboApplication()->getKnownLanguages(); |
| 183 | 188 | |
| 184 | 189 | foreach ($known_langs as $ltag => $ldet) { |
| @@ -188,8 +193,9 @@ | ||
| 188 | 193 | $langs[$ltag] = $ldet; |
| 189 | 194 | } |
| 190 | 195 | } |
| 191 | 196 | |
| 197 | + // cache and return languages | |
| 192 | 198 | $this->all_langs = $langs; |
| 193 | 199 | |
| 194 | 200 | return $this->all_langs; |
| 195 | 201 | } |
| @@ -200,8 +206,13 @@ | ||
| 200 | 206 | * @return array |
| 201 | 207 | */ |
| 202 | 208 | public function getLanguagesTags() |
| 203 | 209 | { |
| 210 | + if (!$this->all_langs) { | |
| 211 | + // set known languages | |
| 212 | + $this->getLanguagesList(); | |
| 213 | + } | |
| 214 | + | |
| 204 | 215 | return array_keys($this->all_langs); |
| 205 | 216 | } |
| 206 | 217 | |
| 207 | 218 | /** |
| @@ -521,9 +532,9 @@ | ||
| 521 | 532 | * @param string $lang Force the translation to a specific language tag like it-IT. |
| 522 | 533 | * |
| 523 | 534 | * @return array The initial array with translated values (if applicable). |
| 524 | 535 | */ |
| 525 | - public function translateContents(&$content, $table, $alias_keys = array(), $ids = array(), $lang = null) | |
| 536 | + public function translateContents(&$content, $table, array $alias_keys = [], array $ids = [], $lang = null) | |
| 526 | 537 | { |
| 527 | 538 | $to_lang = is_null($lang) ? $this->current_lang : $lang; |
| 528 | 539 | $to_lang = !is_null(self::$force_tolang) ? self::$force_tolang : $to_lang; |
| 529 | 540 | |
| @@ -560,9 +571,9 @@ | ||
| 560 | 571 | } |
| 561 | 572 | } |
| 562 | 573 | } |
| 563 | 574 | |
| 564 | - if (count($translated)) { | |
| 575 | + if ($translated) { | |
| 565 | 576 | // set translations buffer |
| 566 | 577 | $this->translations_buffer[$table][$to_lang] = $translated; |
| 567 | 578 | |
| 568 | 579 | // fetch reference_id to be translated and replace default lang values |
| @@ -585,8 +596,73 @@ | ||
| 585 | 596 | } |
| 586 | 597 | } |
| 587 | 598 | |
| 588 | 599 | return $content; |
| 600 | + } | |
| 601 | + | |
| 602 | + /** | |
| 603 | + * Translates a single database record with no reference as a proxy for the records list translations. | |
| 604 | + * | |
| 605 | + * @param array|object $record The record (associative or object) to translate. | |
| 606 | + * @param string $table The database table to which the record belongs. | |
| 607 | + * @param string $lang Force the translation to a specific language tag like it-IT. | |
| 608 | + * @param array $alias_keys Key-Value pairs where Key is the ALIAS used and Value is the original field name. | |
| 609 | + * @param array $ids The reference_IDs to be translated, the IDs of the records. | |
| 610 | + * | |
| 611 | + * @return array|object The original record translated or identical. | |
| 612 | + * | |
| 613 | + * @uses translateContents() | |
| 614 | + * | |
| 615 | + * @since 1.17.2 (J) - 1.7.2 (WP) | |
| 616 | + */ | |
| 617 | + public function translateRecord($record, $table, $lang = null, array $alias_keys = [], array $ids = []) | |
| 618 | + { | |
| 619 | + $is_assoc = is_array($record); | |
| 620 | + $is_object = is_object($record); | |
| 621 | + | |
| 622 | + if ((!$is_assoc && !$is_object) || !$record) { | |
| 623 | + // abort | |
| 624 | + return $record; | |
| 625 | + } | |
| 626 | + | |
| 627 | + if ($lang) { | |
| 628 | + // ensure the given language tag exists | |
| 629 | + $known_tags = $this->getLanguagesTags(); | |
| 630 | + | |
| 631 | + if (!in_array($lang, $known_tags)) { | |
| 632 | + // try to normalize the given language tag | |
| 633 | + $main_lang = substr($lang, 0, 2); | |
| 634 | + $main_langs = []; | |
| 635 | + foreach ($known_tags as $tag) { | |
| 636 | + $main_langs[$tag] = substr($tag, 0, 2); | |
| 637 | + } | |
| 638 | + | |
| 639 | + // try to find the first matching 2-char language tag | |
| 640 | + $match_tag = array_search($main_lang, $main_langs); | |
| 641 | + | |
| 642 | + if ($match_tag) { | |
| 643 | + // swap given lang | |
| 644 | + $lang = $match_tag; | |
| 645 | + } | |
| 646 | + } | |
| 647 | + } | |
| 648 | + | |
| 649 | + // always cast the record to array and treat it as a list | |
| 650 | + $records = [(array) $record]; | |
| 651 | + | |
| 652 | + // translate record as a list | |
| 653 | + $this->translateContents($records, $table, $alias_keys, $ids, $lang); | |
| 654 | + | |
| 655 | + // get the translated (or identical) record | |
| 656 | + $record = $records[0]; | |
| 657 | + | |
| 658 | + if ($is_object) { | |
| 659 | + // cast back to object | |
| 660 | + $record = (object) $record; | |
| 661 | + } | |
| 662 | + | |
| 663 | + // return the evnetually translated original record | |
| 664 | + return $record; | |
| 589 | 665 | } |
| 590 | 666 | |
| 591 | 667 | /** |
| 592 | 668 | * Compares the array to be translated with the translation and replaces the array values if not empty. |