Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@brocheafoin
Last active December 22, 2015 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brocheafoin/6503685 to your computer and use it in GitHub Desktop.
Save brocheafoin/6503685 to your computer and use it in GitHub Desktop.
Making wp-content/languages/themes/ translation files accessible to Codestyling Localization via symlinks. Symlinks are made on the fly, should be update-proof. In our case, this is in a site-specific Must-Use plugin.
/* TODO
Using the 'current_screen' hook means symlinks will be recreated *every time* a Codestyling Localization page is called.
A better solution would be to store themes' versions in a site_option and run this only when installed versions are higher than what we have in our site_option (i.e. an update has been made). See here:
http://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
*/
add_action( 'current_screen' , 'uneq_cs_translations' );
function uneq_cs_translations( $current_screen ) {
/* Re/Create symlinks when visiting Codestyling page */
if ( 'tools_page_codestyling-localization/codestyling-localization' == $current_screen->id ) {
uneq_translation_symlinks();
}
}
function uneq_translation_symlinks() {
# TODO : user WP_filesystem instead of PHP filesystem functions
$installed_themes = wp_get_themes();
# NOTE : for completeness, should actually loop into all installed locales, not just the one currently used. We don't care because our install is fr_FR only.
$locale = get_locale();
foreach ( $installed_themes as $theme_id => $theme_object ) {
# Theme Foundry themes all load_theme_textdomain from a languages/ directory
if ( 'The Theme Foundry' == $theme_object->get('Author') ) {
# Paths to the real translation files, in wp-content/languages/themes
$mo_file_path = trailingslashit( WP_LANG_DIR ) . 'themes/' . $theme_id . '-' . $locale . '.mo';
$po_file_path = trailingslashit( WP_LANG_DIR ) . 'themes/' . $theme_id . '-' . $locale . '.po';
# Path to theme translations directory (wp-content/themes/theme/languages)
$theme_lang_dir_path = trailingslashit(WP_CONTENT_DIR) . 'themes/' . trailingslashit($theme_id) . 'languages/';
# Create 'languages' directory if it's not already there
if (! file_exists($theme_lang_dir_path)) {
mkdir($theme_lang_dir_path,0755);
}
# Creating the symbolic links
if (file_exists($mo_file_path)) {
$mo_link = $theme_lang_dir_path . $locale . '.mo';
symlink( $mo_file_path, $mo_link );
}
if (file_exists($po_file_path)) {
$po_link = $theme_lang_dir_path . $locale . '.po';
symlink( $po_file_path, $po_link );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment