Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] tworzenie strony wielojęzycznej
--free--
post
Post #1





Goście







Witam,


Potrzebuje zrobić stronkę wielojęzyczną.
Znalazłem przykład na necie i stworzyłem

w pliku index.php

  1. <?php
  2. ini_set('display_errors','1');
  3. require('langs/lang.class.php');
  4. __('main_page');
  5. ?>


w folderze /langs są pliki

lang.class.php

  1. <?php
  2.  
  3. class Lang
  4. {
  5. private $languages = array('pl' => 'Polski', 'en' => 'English');
  6. private $current_language = FALSE;
  7. private $lines = array();
  8.  
  9. private static $instance = FALSE;
  10.  
  11. public function __construct()
  12. {
  13. $this->set_language();
  14. }
  15. public function instance()
  16. {
  17. if(self::$instance == FALSE)
  18. {
  19. self::$instance = new Lang;
  20. }
  21.  
  22. return self::$instance;
  23. }
  24. private function set_language()
  25. {
  26. if(isset($_GET['lang']) AND array_key_exists(($lang = strtolower($_GET['lang'])), $this->languages))
  27. {
  28. $this->current_language = $lang;
  29. $_SESSION['site_lang'] = $lang;
  30. }
  31. elseif(isset($_SESSION['site_lang']) AND array_key_exists(($lang = strtolower($_SESSION['site_lang'])), $this->languages))
  32. {
  33. $this->current_language = $lang;
  34. }
  35. else
  36. {
  37. $this->current_language = $this->default_language();
  38. }
  39.  
  40. $this->load_lang_file();
  41. }
  42. private function default_language()
  43. {
  44. return current(array_keys($this->languages));
  45. }
  46. private function load_lang_file()
  47. {
  48. if(file_exists('langs/'.$this->current_language.'.php'))
  49. {
  50. include 'langs/'.$this->current_language.'.php';
  51.  
  52. $this->lines = $lang;
  53. }
  54. }
  55. public static function line($name = FALSE, $params = array())
  56. {
  57. if(isset(self::instance()->lines[$name]))
  58. return vsprintf(self::instance()->lines[$name], $params);
  59.  
  60. return FALSE;
  61. }
  62. public static function get_language()
  63. {
  64. return self::instance()->current_language;
  65. }
  66.  
  67. public static function get_languages()
  68. {
  69. return self::instance()->languages;
  70. }
  71. function __($name = FALSE, $params = array())
  72. {
  73. return Lang::line($name, $params);
  74. }
  75. }


oraz pliki pl.php oraz en.php

gdzie są wpisy :
<?php
$lang['main_page'] = 'Strona główna';
?>


po wywołaniu strony index.php jest błąd :


Fatal error: Call to undefined function __() in C:\Program Files\WebServ\httpd-users\tg\index.php on line 12




Serwer HTTP: Apache/2.2.6 (Win32) PHP/5.2.5
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 11)
mat-bi
post
Post #2





Grupa: Zarejestrowani
Postów: 690
Pomógł: 92
Dołączył: 6.02.2011

Ostrzeżenie: (0%)
-----


Ty naprawdę nie widzisz? Przyjrzyj się zasięgowi klamerek
Go to the top of the page
+Quote Post
-test-
post
Post #3





Goście







chyba jestem ślepy albo juz na oczy nie widze
Go to the top of the page
+Quote Post
mat-bi
post
Post #4





Grupa: Zarejestrowani
Postów: 690
Pomógł: 92
Dołączył: 6.02.2011

Ostrzeżenie: (0%)
-----


Porób sobie wcięcia, to powinieneś widzieć
Go to the top of the page
+Quote Post
-test-
post
Post #5





Goście







zrobilem ... i nie widze (IMG:style_emoticons/default/co_jest.gif)
Go to the top of the page
+Quote Post
-test-
post
Post #6





Goście







zmieniłem na :


  1. <?php
  2.  
  3. class Lang
  4. {
  5. private $languages = array('pl' => 'Polski', 'en' => 'English');
  6. private $current_language = FALSE;
  7. private $lines = array();
  8.  
  9. private static $instance = FALSE;
  10.  
  11. public function __construct()
  12. {
  13. $this->set_language();
  14. }
  15.  
  16. public function instance()
  17. {
  18. if(self::$instance == FALSE)
  19. {
  20. self::$instance = new Lang;
  21. }
  22.  
  23. return self::$instance;
  24. }
  25. private function set_language()
  26. {
  27. if(isset($_GET['lang']) AND array_key_exists(($lang = strtolower($_GET['lang'])), $this->languages))
  28. {
  29. $this->current_language = $lang;
  30. $_SESSION['site_lang'] = $lang;
  31. }
  32. elseif(isset($_SESSION['site_lang']) AND array_key_exists(($lang = strtolower($_SESSION['site_lang'])), $this->languages))
  33. {
  34. $this->current_language = $lang;
  35. }
  36. else
  37. {
  38. $this->current_language = $this->default_language();
  39. }
  40.  
  41. $this->load_lang_file();
  42. }
  43. private function default_language()
  44. {
  45. return current(array_keys($this->languages));
  46. }
  47. private function load_lang_file()
  48. {
  49. if(file_exists('langs/'.$this->current_language.'.php'))
  50. {
  51. include 'langs/'.$this->current_language.'.php';
  52.  
  53. $this->lines = $lang;
  54. }
  55. }
  56. public static function line($name = FALSE, $params = array())
  57. {
  58. if(isset(self::instance()->lines[$name]))
  59. return vsprintf(self::instance()->lines[$name], $params);
  60.  
  61. return FALSE;
  62. }
  63. public static function get_language()
  64. {
  65. return self::instance()->current_language;
  66. }
  67.  
  68. public static function get_languages()
  69. {
  70. return self::instance()->languages;
  71. }
  72. }
  73. function __($name = FALSE, $params = array())
  74. {
  75. return Lang::line($name, $params);
  76. }


błądu nie ma ale jest cyzsta strona tekst sie nie wyświetił
Go to the top of the page
+Quote Post
Rid
post
Post #7





Grupa: Zarejestrowani
Postów: 715
Pomógł: 47
Dołączył: 5.12.2010

Ostrzeżenie: (0%)
-----


Nie wiem czy aby ta konstrukcja jest dobra:
Cytat
elseif(isset($_SESSION['site_lang']) AND array_key_exists(($lang = strtolower($_SESSION['site_lang'])), $this->languages))

czy nie powinno być:
Cytat
else { if(warunek){
.........
działanie
.............
}
}
Go to the top of the page
+Quote Post
mortus
post
Post #8





Grupa: Zarejestrowani
Postów: 2 178
Pomógł: 596
Dołączył: 25.09.2009
Skąd: Piwniczna-Zdrój

Ostrzeżenie: (0%)
-----


@Rid Obie konstrukcje są równoważne, tzn. że działają tak samo.
@mat-bi Kolega nie widział problemu z klamerkami, bo go nie było.

@do autora
Klasę Lang masz napisaną prawidłowo. Jednak problemem jest użycie funkcji __(). Otóż funkcja ta jest metodą klasy Lang, zatem wywołujemy ją w taki oto sposób:
  1. Lang::__('main_page');
Dodatkowo należy zauważyć, że powyższa funkcja zwraca łańcuch znaków, który potrzebujemy wyświetlić. Zatem kod powinien wyglądać następująco:
  1. echo Lang::__('main_page');
Wróć zatem do pierwszej wersji kodu i zastąp odpowiednią linię w pliku index.php podanym przeze mnie wyrażeniem.

Ten post edytował mortus 20.05.2011, 08:21:17
Go to the top of the page
+Quote Post
mat-bi
post
Post #9





Grupa: Zarejestrowani
Postów: 690
Pomógł: 92
Dołączył: 6.02.2011

Ostrzeżenie: (0%)
-----


Radziłbym czasem pomyśleć. Nie, nie ma w sensie błedu, ale ma, gdyż źle dał zasięg klasy.
Go to the top of the page
+Quote Post
-test-
post
Post #10





Goście







mortus dzięki zapomniałem o echo lol (IMG:style_emoticons/default/biggrin.gif)

a teraz mam jeszcze parę pytań

taki kod działa :

  1. <div id="menu_gora">
  2. <a href="index.php"> <?php echo __('menu_index'); ?></a>
  3. <a href="firma.php"> <?php echo __('menu_firma'); ?> </a>
  4. </div>


a chciałbym taki :

<div id="menu_gora">
<?php
$txt='';
$txt.="<a href=\"index.php\"> __('menu_index'); </a>";
echo $txt;
</div>

ii wtedy wynik jest __('menu_index');

da się to przerobic?

2 pytanie :
jak rozwiązać problem tabel wielojęzycznych

np o strukturze
id int primary
oferta varchar

i jak to zrobić zeby w polskim było np
1 książka
2 długopis
a w wejsji angielskiej
1 book
2 pen

(IMG:style_emoticons/default/smile.gif) dzięki za pomoc
Go to the top of the page
+Quote Post
1010
post
Post #11





Grupa: Zarejestrowani
Postów: 749
Pomógł: 37
Dołączył: 3.10.2006

Ostrzeżenie: (0%)
-----


  1. <div id="menu_gora">
  2. <?php
  3. $txt='';
  4. $txt.="<a href=\"index.php\"> ". __('menu_index') ." </a>";
  5. echo $txt;
  6. </div>



Jeśli chodzi o tabele to dodajesz pole lang, bądź też lang_id (bardziej wydajne) i przy zapytaniach where lang="aktualny_jezyk". O to chodziło?

Ten post edytował 1010 21.05.2011, 12:21:46
Go to the top of the page
+Quote Post
-test-
post
Post #12





Goście







tylko mysle jak strukture ulozyc zeby to w jednej tabeli zmiescic np

id opis_pl opis_en ilosc_pl ilosc_en
1 ksiazka book jeden one itd

bo tabela zamowienie posiada 22 pola
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 19:56