Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

#367: First attempt at a class for the new translation system.

  • Loading branch information...
commit 6b74c31d91118f915c212d3166e688b9f2393fbf 1 parent bf7116c
Franz Liedke authored September 15, 2011

Showing 1 changed file with 135 additions and 0 deletions. Show diff stats Hide diff stats

  1. 135  include/classes/lang.php
135  include/classes/lang.php
... ...
@@ -0,0 +1,135 @@
  1
+<?php
  2
+
  3
+require_once PUN_ROOT.'modules/gettext/gettext.php';
  4
+require_once PUN_ROOT.'modules/cache/cache.php';
  5
+
  6
+class Flux_Lang
  7
+{
  8
+	/**
  9
+	 * An array of translated strings
  10
+	 *
  11
+	 * @var array
  12
+	 */
  13
+	protected $translationTable = array();
  14
+
  15
+	/**
  16
+	 * The directory where language packs are located
  17
+	 *
  18
+	 * @var string
  19
+	 */
  20
+	protected $langDir = 'lang/';
  21
+
  22
+	/**
  23
+	 * The default language
  24
+	 *
  25
+	 * @var string
  26
+	 */
  27
+	protected $defaultLang = 'en';
  28
+
  29
+	/**
  30
+	 * The language to use
  31
+	 *
  32
+	 * @var string
  33
+	 */
  34
+	protected $lang = 'en';
  35
+
  36
+	/**
  37
+	 * Set the directory where language packs are located
  38
+	 *
  39
+	 * @param string $dir
  40
+	 * @return void
  41
+	 */
  42
+	public function setLanguageDirectory($dir)
  43
+	{
  44
+		// Remove any trailing slashes
  45
+		$this->langDir = rtrim($dir, '/');
  46
+	}
  47
+
  48
+	/**
  49
+	 * Set the default language
  50
+	 *
  51
+	 * @param array $lang
  52
+	 * @return void
  53
+	 */
  54
+	public function setDefaultLanguage($lang)
  55
+	{
  56
+		if (file_exists($this->langDir.'/'.$lang))
  57
+			$this->defaultLang = $lang;
  58
+		else
  59
+			throw new Exception('Default language pack "'.$lang.'" does not exist.');
  60
+	}
  61
+
  62
+	/**
  63
+	 * Set the language to use
  64
+	 *
  65
+	 * @param array $lang
  66
+	 * @return void
  67
+	 */
  68
+	public function setLanguage($lang)
  69
+	{
  70
+		if (file_exists($this->langDir.'/'.$lang))
  71
+			$this->lang = $lang;
  72
+		else
  73
+			throw new Exception('Language pack "'.$lang.'" does not exist.');
  74
+	}
  75
+
  76
+	/**
  77
+	 * Load a new file and add its translations to our elements
  78
+	 *
  79
+	 * Incomplete language packs will fall back to the default language
  80
+	 * for missing strings.
  81
+	 *
  82
+	 * This only loads Gettext files if cached files cannot be found.
  83
+	 *
  84
+	 * @param array $resource
  85
+	 * @return void
  86
+	 */
  87
+	public function load($resource)
  88
+	{
  89
+		$default_filename = $this->langDir.'/'.$this->defaultLang.'/'.$resource.'.mo';
  90
+		$filename = $this->langDir.'/'.$this->lang.'/'.$resource.'.mo';
  91
+
  92
+		$cache = Cache::load('file', array('dir' => FORUM_CACHE_DIR), 'varexport');
  93
+		// TODO: Handle Cache config globally. How?
  94
+
  95
+		// TODO: Slash allowed? - I'd rather use that than an underscore
  96
+		$trans_cache = $cache->get($this->lang.'_'.$resource);
  97
+		if ($trans_cache === Cache::NOT_FOUND)
  98
+		{
  99
+			$trans_cache = Gettext::parse($filename);
  100
+
  101
+			// If this is not the default language, load that, too
  102
+			if ($this->defaultLang != $this->lang)
  103
+			{
  104
+				$def_trans_cache = $cache->get($this->defaultLang.'_'.$resource);
  105
+				if ($def_trans_cache === Cache::NOT_FOUND)
  106
+				{
  107
+					$def_trans_cache = Gettext::parse($default_filename);
  108
+
  109
+					$cache->set($this->defaultLang.'_'.$resource, $def_trans_cache);
  110
+				}
  111
+
  112
+				// TODO: How could we automatically regenerate these cache files when necessary? (E.g. imagine the default language files being replaced during a new release, the custom translations haven't caught up yet. How to handle that? etc.)
  113
+
  114
+				// Now use default language values as fallback
  115
+				$trans_cache = array_merge($def_trans_cache, $trans_cache);
  116
+			}
  117
+
  118
+			$cache->set($this->lang.'_'.$resource, $trans_cache);
  119
+		}
  120
+
  121
+		// Store the loaded values for usage
  122
+		$this->translationTable = array_merge($this->translationTable, $trans_cache);
  123
+	}
  124
+
  125
+	/**
  126
+	 * Return the translation of the given string
  127
+	 *
  128
+	 * @param string $str
  129
+	 * @return string
  130
+	 */
  131
+	public function t($str)
  132
+	{
  133
+		return isset($this->translationTable[$str]) ? $this->translationTable[$str][0] : $str;
  134
+	}
  135
+}

0 notes on commit 6b74c31

Please sign in to comment.
Something went wrong with that request. Please try again.