text.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Get the text for the correct translation
  4. *
  5. * @method array get
  6. */
  7. class text {
  8. /**
  9. * Called when the object is created
  10. */
  11. public function __construct() {
  12. //place holder
  13. }
  14. /**
  15. * Called when there are no references to a particular object
  16. * unset the variables used in the class
  17. */
  18. public function __destruct() {
  19. foreach ($this as $key => $value) {
  20. unset($this->$key);
  21. }
  22. }
  23. /**
  24. * Get a specific item from the cache
  25. * @var string $language_code examples: en-us, es-cl, fr-fr, pt-pt
  26. * @var string $app_path examples: app/exec or core/domains
  27. */
  28. public function get($language_code = null, $app_path = null, $exclude_global = false) {
  29. //get the global app_languages.php
  30. if (!$exclude_global){
  31. include $_SERVER["PROJECT_ROOT"]."/resources/app_languages.php";
  32. }
  33. //get the app_languages.php
  34. if ($app_path != null) {
  35. $lang_path = $_SERVER["PROJECT_ROOT"]."/".$app_path."/app_languages.php";
  36. }
  37. else {
  38. $lang_path = getcwd().'/app_languages.php';
  39. }
  40. if(file_exists($lang_path)){
  41. require $lang_path;
  42. }
  43. //get the available languages
  44. krsort($text);
  45. foreach ($text as $lang_label => $lang_codes) {
  46. foreach ($lang_codes as $lang_code => $lang_text) {
  47. if ($lang_text != '') {
  48. $app_languages[] = $lang_code;
  49. }
  50. }
  51. }
  52. $_SESSION['app']['languages'] = array_unique($app_languages);
  53. //check the session language
  54. if (isset($_SESSION['domain']) and $language_code == null){
  55. $language_code = $_SESSION['domain']['language']['code'];
  56. } elseif ($language_code == null){
  57. $language_code = 'en-us';
  58. }
  59. //reduce to specific language
  60. if ($language_code != 'all') {
  61. foreach ($text as $key => $value) {
  62. if (strlen($value[$language_code]) > 0) {
  63. $text[$key] = $value[$language_code];
  64. } else {
  65. //fallback to en-us
  66. $text[$key] = $value['en-us'];
  67. }
  68. }
  69. }
  70. //return the array of translations
  71. return $text;
  72. }
  73. }
  74. ?>