Skip to content

Commit

Permalink
Merge branch 'release/0.9.32'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Jul 14, 2015
2 parents 0ba1af2 + 3b3ac68 commit 7029edf
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 42 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# v0.9.32
## 07/14/2015

1. [](#new)
* Detect users preferred language via `http_accept_language` setting
* Added new `translateArray()` language method
2. [](#improved)
* Support `en` translations by default for plugins & themes
* Improved default generator tag
* Minor language tweaks and fixes
3. [](#bugfix)
* Fix for session active language and homepage redirects
* Ignore root-level page rather than throwing error

# v0.9.31
## 07/09/2015

Expand Down
4 changes: 3 additions & 1 deletion system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ absolute_urls: false # Absolute or relative URLs for `base_url
timezone: '' # Valid values: http://php.net/manual/en/timezones.php
param_sep: ':' # Parameter separator, use ';' for Apache on windows

languages:
translations: true # Enable translations by default

home:
alias: '/home' # Default path for home, ie /

Expand Down Expand Up @@ -45,7 +48,6 @@ cache:
lifetime: 604800 # Lifetime of cached data in seconds (0 = infinite)
gzip: false # GZip compress the page output


twig:
cache: true # Set to true to enable twig caching
debug: false # Enable Twig debug
Expand Down
2 changes: 1 addition & 1 deletion system/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Some standard defines
define('GRAV', true);
define('GRAV_VERSION', '0.9.31');
define('GRAV_VERSION', '0.9.32');
define('DS', '/');

// Directories and Paths
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function init()
$this->loadCompiledConfig($this->configLookup, $this->pluginLookup, 'master');

// process languages if supported
if ($this->get('system.languages', false)) {
if ($this->get('system.languages.translations', true)) {
$this->languagesLookup = $locator->findResources('languages://');
$this->loadCompiledLanguages($this->languagesLookup, $this->pluginLookup, 'master');
}
Expand Down
136 changes: 107 additions & 29 deletions system/src/Grav/Common/Language/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class Language
protected $page_extensions = [];
protected $fallback_languages = [];
protected $default;
protected $active;
protected $active = null;
protected $config;
protected $http_accept_language;

/**
* Constructor
Expand Down Expand Up @@ -114,8 +115,10 @@ public function setDefault($lang)
{
if ($this->validate($lang)) {
$this->default = $lang;

return $lang;
}

return false;
}

Expand All @@ -140,8 +143,10 @@ public function setActive($lang)
{
if ($this->validate($lang)) {
$this->active = $lang;

return $lang;
}

return false;
}

Expand All @@ -154,7 +159,7 @@ public function setActive($lang)
*/
public function setActiveFromUri($uri)
{
$regex = '/(\/('.$this->getAvailable().')).*/';
$regex = '/(\/(' . $this->getAvailable() . ')).*/';

// if languages set
if ($this->enabled()) {
Expand All @@ -165,19 +170,31 @@ public function setActiveFromUri($uri)

// store in session if different
if ($this->config->get('system.session.enabled', false)
&& $this->config->get('system.language.session_store_active', true)
&& $this->grav['session']->active_language != $this->active) {
$this->grav['session']->active_language = $this->active;
&& $this->config->get('system.languages.session_store_active', true)
&& $this->grav['session']->active_language != $this->active
) {
$this->grav['session']->active_language = $this->active;
}
} else {
// try getting from session, else no active
if ($this->config->get('system.session.enabled', false) && $this->config->get('system.language.session_store_active', true)) {
if ($this->config->get('system.session.enabled', false) &&
$this->config->get('system.languages.session_store_active', true)) {
$this->active = $this->grav['session']->active_language ?: null;
} else {
$this->active = null;
}
// if still null, try from http_accept_language header
if ($this->active === null && $this->config->get('system.languages.http_accept_language')) {
$preferred = $this->getBrowserLanguages();
foreach ($preferred as $lang) {
if ($this->validate($lang)) {
$this->active = $lang;
break;
}
}

}
}
}

return $uri;
}

Expand All @@ -197,21 +214,22 @@ public function getFallbackPageExtensions($file_ext = null)
if ($this->enabled()) {
$valid_lang_extensions = [];
foreach ($this->languages as $lang) {
$valid_lang_extensions[] = '.'.$lang.$file_ext;
$valid_lang_extensions[] = '.' . $lang . $file_ext;
}

if ($this->active) {
$active_extension = '.'.$this->active.$file_ext;
$active_extension = '.' . $this->active . $file_ext;
$key = array_search($active_extension, $valid_lang_extensions);
unset($valid_lang_extensions[$key]);
array_unshift($valid_lang_extensions, $active_extension);
}

$this->page_extensions = array_merge($valid_lang_extensions, (array) $file_ext);
$this->page_extensions = array_merge($valid_lang_extensions, (array)$file_ext);
} else {
$this->page_extensions = (array) $file_ext;
$this->page_extensions = (array)$file_ext;
}
}

return $this->page_extensions;
}

Expand All @@ -235,6 +253,7 @@ public function getFallbackLanguages()
$this->fallback_languages = $fallback_languages;
}
}

return $this->fallback_languages;
}

Expand All @@ -250,31 +269,37 @@ public function validate($lang)
if (in_array($lang, $this->languages)) {
return true;
}

return false;
}

/**
* Translate a key and possibly arguments into a string using current lang and fallbacks
*
* @param Array $args first argument is the lookup key value
* other arguments can be passed and replaced in the translation with sprintf syntax
* @param Array $args first argument is the lookup key value
* other arguments can be passed and replaced in the translation with sprintf syntax
* @param Array $languages
*
* @return string
*/
public function translate(Array $args, $languages = null)
public function translate(Array $args, Array $languages = null)
{
$lookup = array_shift($args);

if ($this->enabled() && $lookup) {

if (empty($languages)) {
if ($this->config->get('system.languages.translations.fallback', true)) {
$languages = $this->getFallbackLanguages();
} else {
$languages = (array) $this->getDefault();
if ($this->config->get('system.languages.translations', true)) {
if ($this->enabled() && $lookup) {
if (empty($languages)) {
if ($this->config->get('system.languages.translations.fallback', true)) {
$languages = $this->getFallbackLanguages();
} else {
$languages = (array)$this->getDefault();
}
}
} else {
$languages = ['en'];
}

foreach ($this->getFallbackLanguages() as $lang) {
foreach ((array)$languages as $lang) {
$translation = $this->getTranslation($lang, $lookup);

if ($translation) {
Expand All @@ -286,7 +311,34 @@ public function translate(Array $args, $languages = null)
}
}
}
return '<span class="untranslated">'.$lookup.'</span>';

return '<span class="untranslated">' . $lookup . '</span>';
}

public function translateArray($key, $index, $languages = null)
{
if ($this->config->get('system.languages.translations', true)) {
if ($this->enabled() && $key) {
if (empty($languages)) {
if ($this->config->get('system.languages.translations.fallback', true)) {
$languages = $this->getFallbackLanguages();
} else {
$languages = (array)$this->getDefault();
}
}
} else {
$languages = ['en'];
}

foreach ((array)$languages as $lang) {
$translation_array = (array)$this->config->getLanguages()->get($lang . '.' . $key, null);
if ($translation_array && array_key_exists($index, $translation_array)) {
return $translation_array[$index];
}
}
}

return '<span class="untranslated">' . $key . '[' . $index . ']</span>';
}

/**
Expand All @@ -297,14 +349,40 @@ public function translate(Array $args, $languages = null)
*
* @return string
*/
public function getTranslation($lang, $key) {
$languages = $this->config->getLanguages();

$translation = $languages->get($lang.'.'.$key, null);
public function getTranslation($lang, $key)
{
$translation = $this->config->getLanguages()->get($lang . '.' . $key, null);
if (is_array($translation)) {
return (string) array_shift($translation);
return (string)array_shift($translation);
}

return $translation;
}

function getBrowserLanguages($accept_langs = [])
{
if (empty($this->http_accept_language)) {
if (empty($accept_langs) && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$accept_langs = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
} else {
return $accept_langs;
}

foreach (explode(',', $accept_langs) as $k => $pref) {
// split $pref again by ';q='
// and decorate the language entries by inverted position
if (false !== ($i = strpos($pref, ';q='))) {
$langs[substr($pref, 0, $i)] = array((float)substr($pref, $i + 3), -$k);
} else {
$langs[$pref] = array(1, -$k);
}
}
arsort($langs);

// no need to undecorate, because we're only interested in the keys
$this->http_accept_language = array_keys($langs);
}
return $this->http_accept_language;
}

}
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ public function metadata($var = null)
$page_header = $this->header;

// Set the Generator tag
$this->metadata['generator'] = array('name'=>'generator', 'content'=>'Grav ' . GRAV_VERSION);
$this->metadata['generator'] = array('name'=>'generator', 'content'=>'GravCMS ' . GRAV_VERSION);

// Safety check to ensure we have a header
if ($page_header) {
Expand Down
3 changes: 1 addition & 2 deletions system/src/Grav/Common/Page/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ protected function recurse($directory, Page &$parent = null)
}
}

if (!empty($page_found)) {
if ($parent && !empty($page_found)) {
$file = new \SplFileInfo($page_found);
$page->init($file);
$page->extension($page_extension);
Expand All @@ -639,7 +639,6 @@ protected function recurse($directory, Page &$parent = null)

/** @var \DirectoryIterator $file */
foreach (new \FilesystemIterator($directory) as $file) {

$name = $file->getFilename();

if ($file->isFile()) {
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected function loadConfiguration($name, Config $config)
$themeConfig = CompiledYamlFile::instance("themes://{$name}/{$name}" . YAML_EXT)->content();
$config->joinDefaults("themes.{$name}", $themeConfig);

if ($this->grav['language']->enabled()) {
if ($this->config->get('system.languages.translations', true)) {
$languages = CompiledYamlFile::instance("themes://{$name}/languages". YAML_EXT)->content();
if ($languages) {
$config->getLanguages()->mergeRecursive($languages);
Expand Down
14 changes: 11 additions & 3 deletions system/src/Grav/Common/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function getFilters()
new \Twig_SimpleFilter('markdown', [$this, 'markdownFilter']),
new \Twig_SimpleFilter('starts_with', [$this, 'startsWithFilter']),
new \Twig_SimpleFilter('ends_with', [$this, 'endsWithFilter']),
new \Twig_SimpleFilter('t', [$this, 'translateFilter'])
new \Twig_SimpleFilter('t', [$this, 'translate']),
new \Twig_SimpleFilter('ta', [$this, 'translateArray'])
];
}

Expand All @@ -75,7 +76,8 @@ public function getFunctions()
new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
new \Twig_SimpleFunction('gist', [$this, 'gistFunc']),
new \Twig_simpleFunction('random_string', [$this, 'randomStringFunc']),
new \Twig_simpleFunction('t', [$this, 'translateFunc'])
new \Twig_simpleFunction('t', [$this, 'translate']),
new \Twig_simpleFunction('ta', [$this, 'translateArray'])
];
}

Expand Down Expand Up @@ -363,11 +365,17 @@ public function endsWithFilter($haystack, $needle)
return Utils::endsWith($haystack, $needle);
}

public function translateFilter()
public function translate()
{
return $this->grav['language']->translate(func_get_args());
}

public function translateArray($key, $index, $lang = null)
{
return $this->grav['language']->translateArray($key, $index, $lang);
}


/**
* Repeat given string x times.
*
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public function init()
$uri = $language->setActiveFromUri($uri);

// redirect to language specific homepage if configured to do so
if ($uri == '/' && $language->enabled() && !$language->getActive()) {
if ($uri == '/' && $language->enabled()) {
if ($config->get('system.languages.home_redirect.include_route', true)) {
$prefix = $config->get('system.languages.home_redirect.include_lang', true) ? $language->getDefault() . '/' : '';
$prefix = $config->get('system.languages.home_redirect.include_lang', true) ? $language->getLanguage() . '/' : '';
$grav->redirect($prefix . Pages::getHomeRoute());
} elseif ($config->get('system.languages.home_redirect.include_lang', true)) {
$grav->redirect($language->getDefault() . '/');
$grav->redirect($language->getLanguage() . '/');
}
}

Expand Down

0 comments on commit 7029edf

Please sign in to comment.