TranslationUtility.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Utility;
  3. use App\Models\Translation;
  4. class TranslationUtility
  5. {
  6. // Hold the class instance.
  7. private static $instance = null;
  8. private $translations;
  9. // The db connection is established in the private constructor.
  10. private function __construct()
  11. {
  12. $data = Translation::all();
  13. $this->translations = collect($data->toArray())->all();
  14. //$this->translations = collect($data->toArray());
  15. }
  16. public static function getInstance()
  17. {
  18. if (!self::$instance) {
  19. self::$instance = new TranslationUtility();
  20. }
  21. return self::$instance;
  22. }
  23. public static function reInstantiate()
  24. {
  25. self::$instance = new TranslationUtility();
  26. }
  27. public function cached_translation_row($lang_key, $lang)
  28. {
  29. $row = [];
  30. if (empty($this->translations)) {
  31. return $row;
  32. }
  33. foreach ($this->translations as $item) {
  34. if ($item['lang_key'] == $lang_key && $item['lang'] == $lang) {
  35. $row = $item;
  36. break;
  37. }
  38. }
  39. return $row;
  40. }
  41. //The code below also works but it takes more time than the function written above
  42. //$this->translations = collect($data->toArray());
  43. /*public function cached_translation_row($lang_key, $lang)
  44. {
  45. $row = $this->translations->where('lang_key', $lang_key)->where('lang', $lang)->first();
  46. return $row != null ? $row : [];
  47. }*/
  48. public function getAllTranslations()
  49. {
  50. return $this->translations;
  51. }
  52. }