Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa,php5] Różne wersje językowe strony (w plikach), moja gotowa klasa (prosze o opinie)
rafalp
post 22.06.2008, 11:35:13
Post #1





Grupa: Zarejestrowani
Postów: 224
Pomógł: 18
Dołączył: 4.02.2003
Skąd: Częstochowa

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


Zamieszczam prostą bardzo uniwersalną klasę (z zastosowaniem do małych projektów) i może komuś się przyda a za dodatkowe sugestie, pomysły lub wytkanie jakiś błędów będę wdzięczny.

Wszystko opiera się na plikach i zczytaniu pliku do tablicy, zapisaniu tłumaczeń wiersz po wierszu a następnie pobraniu wybranego wiersza i wypisaniu go na stronie.

Klasa nadaje się najbardziej na tłumaczenie już gotowego projektu gdyż użycie polega na tłumaczeniu jakiegoś fragmentu tekstu, zapisanie do pliku(ów) przetłumaczenia a następnie wywołanie metody.


  1. <?php
  2. class Language
  3. {
  4. /*******************************
  5. * Author: Rafal @ Pawlukiewicz . com
  6. ********************************/
  7. /**
  8.  * License
  9.  *
  10.  * This library is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU Lesser General Public
  12.  * License as published by the Free Software Foundation.
  13.  *
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17.  * Lesser General Public License for more details.
  18.  *
  19.  * If you modify this class, or have any ideas to improve it, please contact me!
  20.  * You are allowed to redistribute this class, if you keep my name and my contac
  21.  on it.
  22.  *
  23.  *
  24.  **/
  25.  
  26.  
  27. public $language; //actual language set
  28. public $folder;  // catalog where we storage file
  29. public $myfile; // file with translation
  30. public $default;  // file with translation
  31. public $extension;  // language file extention
  32. public $error;  // if some error appear
  33. public $show_error; // if we want to show error message on website
  34.  
  35. function __construct($set_default = "en") // default language
  36. {
  37.  $this->folder = "include/language/";
  38.  $this->extension = ".lang";
  39.  $this->default = $set_default;
  40.  $this->error = false;
  41.  $this->show_error = true; // change it if you do not want show error message
  42. }
  43.  
  44. function e($text, $n)
  45. {
  46.  if($this->language == '')
  47. $this->language = $this->default;
  48.  
  49.  $source = $this->folder . $this->language . $this->extension;
  50.  if(file_exists($source))
  51. $this->myfile = file($source);
  52.  else
  53. $this->error = true;
  54.  
  55.  if(($n > 0) AND ($n <= count($this->myfile)) AND !($this->error))
  56. {
  57. $text = $this->myfile[intval($n)-1];
  58. return($text);
  59. }
  60.  
  61. if($this->error AND $this->show_error)
  62. $this->ShowError();
  63. }
  64.  
  65. function ShowError()
  66. {
  67.  echo "[We have a problem with (" . $this->language . ") translation file]";
  68.  $this->show_error = false; // show only one error message per website
  69. }
  70. }
  71.  
  72. /*
  73.  
  74. ===========
  75. == USING: ==
  76. ===========
  77. $lang = new Language("pl");  // inicjacja z domyślnym językiem
  78.  
  79. $lang->language = $some_session->lang; // ustawienie aktualnego języka pobranego np. z sesji
  80. $lang->language = "pl";  // ustawienie ręczne aktualnego języka
  81. echo $lang->e("arbitrary_text", 1); // wstawienie tekstu gdzie "arbitrary_text" oznacza naszą interpterację/nazwę pola który chcemy wyświetlić a drugi parametr (1) znacza numer wiersza z pliku tłumaczeń który chcemy wstawić.
  82.  
  83. */
  84. ?>


Ten post edytował rafalp 17.07.2008, 12:44:24


--------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
vokiel
post 22.07.2008, 11:57:16
Post #2





Grupa: Zarejestrowani
Postów: 2 592
Pomógł: 445
Dołączył: 12.03.2007

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


Nie będę się wdawał w przydatność tej klasy, bo to zależy od tego jakie kto ma zapotrzebowanie. Ale czepię się aaevil.gif jednego:
  1. <?php
  2. function __construct($set_default = "en")
  3. {
  4.  $this->folder = "include/language/";
  5.  $this->extension = ".lang";
  6.  $this->default = $set_default;
  7.  $this->error = false; // jeśli ustawiasz to w konstruktorze to daj możliwość zmiany z zewnątrz na nie be
    zpośrednio z klasy
  8.  $this->show_error = true; // j.w.
  9. }
  10. // czyli albo dajesz to w deklaracji przed konstruktorem
  11. public $error=false;
  12. public $show_error=true;
  13.  
  14. albo przerób kostrukor, tak, żeby można było to ustawić w zależności od danej potrzeby, a nie globalnie w kodzie klasy, czyli np:
  15. function __construct($set_default = "en", $error=false, $show_error=true)
  16. {
  17.  $this->folder = "include/language/";
  18.  $this->extension = ".lang";
  19.  $this->default = $set_default;
  20.  $this->error = $error;
  21.  $this->show_error = $show_error;
  22. }
  23. ?>


Co do podawania nr wiersza tłumaczenia to troche zamotane, bo kto przy 245liniach tłumaczenia będzie pamiętał, że tutaj to akurat była linia 156 a nie np 157. Poza tym ile można mieć tłumaczeń jednego elementu, w jednym języku? Raczej jedno.
Moim zdaniem lepiej chyba zrobić na zasadzie fraza_do_przetłumacznia=tłumaczenie.


--------------------
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 20:14