Skip to content

Instantly share code, notes, and snippets.

@lokothodida
Last active March 25, 2016 19:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lokothodida/706afee923f08f18fc51 to your computer and use it in GitHub Desktop.
Save lokothodida/706afee923f08f18fc51 to your computer and use it in GitHub Desktop.
GetSimple Theme Developer Language Helper Functions

GetSimple Theme Developer Language Helper Functions

Allows GetSimple theme designers to have theme-dependent language strings in a lang folder of their theme.

Usage

  1. Add language_functions.php (from this gist) to your current theme's folder.

  2. Create a lang folder in your current theme

  3. Create language files for your theme with file names corresponding to language codes:

    // theme/yourtheme/lang/en_US.php
    $i18n = array(
      'LATEST_UPDATE' => 'Latest update',
      // ...
    );

    The variable $i18n must be defined as an associative array.

  4. In your theme, include the language_functions.php file.

  5. Call set_theme_lang($lang) with the base name of the desired language file (e.g. en_US).

  6. Print out strings using get_theme_i18n($hash). Return them using return_theme_i18n($hash).

    // theme/yourtheme/template.php
    // ...
    include('language_functions.php');
    
    // Set the language
    set_theme_lang('en_US');
    
    // Use your language hashes
    get_theme_i18n('LATEST_UPDATE'); // prints 'Latest update'

API

bool set_theme_lang(string $lang)

Sets the language code to the theme language file $lang.php.

void get_theme_i18n(string $hash)

Echos the i18n theme $hash if it exists, {$hash} otherwise.

string return_theme_i18n(string $hash)

Returns the i18n theme $hash if it exists, {$hash} otherwise.

<?php
// Language functions for theme
// Sets the current theme language
function set_theme_lang($lang = null) {
static $currentLang = null;
if ($lang) {
$currentLang = $lang;
}
return $currentLang;
}
// Returns the current theme language
function get_theme_lang() {
return set_theme_lang();
}
// Echos the language hash value
function get_theme_i18n($hash) {
echo return_theme_i18n($hash);
}
// Return the language hash value
function return_theme_i18n($hash) {
// Languages (state is maintained throughout calls to return_theme_i18n)
static $langs = array();
// Current Language
$lang = get_theme_lang();
// Merge the current language file if it hasn't been loaded yet
if (!isset($langs[$lang])) {
$langs[$lang] = load_theme_lang_file($lang);
}
// Return the hash if it exists in the current language
if (isset($langs[$lang][$hash])) {
$string = $langs[$lang][$hash];
} else {
$string = '{' . $hash . '}';
}
return $string;
}
// Loads a theme language file and returns the $i18n array
function load_theme_lang_file($lang) {
$file = GSTHEMESPATH . $GLOBALS['TEMPLATE'] . '/lang/' . $lang . '.php';
$i18n = array();
if (file_exists($file)) {
include($file); // $i18n should be defined in the file
}
return $i18n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment