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

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: 19.07.2025 - 12:56