← Back to team overview

ubiquity-slideshow team mailing list archive

lp:~dylanmccall/ubiquity-slideshow-ubuntu/defaults-builder-support into lp:ubiquity-slideshow-ubuntu

 

Dylan McCall has proposed merging lp:~dylanmccall/ubiquity-slideshow-ubuntu/defaults-builder-support into lp:ubiquity-slideshow-ubuntu.

Requested reviews:
  Ubiquity Slideshow (ubiquity-slideshow)

For more details, see:
https://code.launchpad.net/~dylanmccall/ubiquity-slideshow-ubuntu/defaults-builder-support/+merge/116035

This change is surprisingly non-invasive, but a sanity check might be in order. Effectively, it changes the build format to look like this:

/ubiquity-slideshow/slides/index.html
/ubiquity-slideshow/slides/[html for each slide]
/ubiquity-slideshow/slides/directory.js
/ubiquity-slideshow/slides/l10n/[locale]/[html for each slide]

Mostly the same as before. Localized slides are all inside l10n instead of right under /ubiquity-slideshow/slides, which is kind of nice. Internally, we've added a C locale but it isn't represented in the build directory. (This makes it a little easier to test things, since I didn't want to change all the project folders to mirror that structure).

Where it gets interesting is the "extra" directory, which can be added by a defaults package generated with ubuntu-defaults-builder.  Here's what this might look like in the future after installing a defaults package by a particular loco team:

/ubiquity-slideshow/slides/extra/directory.js
/ubiquity-slideshow/slides/extra/[locale]/[html for each slide]
/ubiquity-slideshow/slides/extra/[locale]/screenshots

Yes, screenshots! :)

The extra directory format is a tad different from the slides directory. You can modify a screenshot for the C locale by adding a file like this:
/ubiquity-slideshow/slides/extra/C/screenshots/welcome.jpg
-- 
https://code.launchpad.net/~dylanmccall/ubiquity-slideshow-ubuntu/defaults-builder-support/+merge/116035
Your team Ubiquity Slideshow is requested to review the proposed merge of lp:~dylanmccall/ubiquity-slideshow-ubuntu/defaults-builder-support into lp:ubiquity-slideshow-ubuntu.
=== modified file 'Makefile'
--- Makefile	2012-04-10 16:47:18 +0000
+++ Makefile	2012-07-20 17:19:18 +0000
@@ -31,13 +31,13 @@
 	cp -rL $(SOURCESLIDES)/oem-config-ubuntu $(BUILD)
 
 translations:
-	./generate-local-slides.sh ubuntu
-	./generate-local-slides.sh kubuntu
-	./generate-local-slides.sh xubuntu
-	./generate-local-slides.sh lubuntu
-	./generate-local-slides.sh edubuntu
-	./generate-local-slides.sh ubuntustudio
-	./generate-local-slides.sh oem-config-ubuntu
+	python generate-local-slides.py ubuntu
+	python generate-local-slides.py kubuntu
+	python generate-local-slides.py xubuntu
+	python generate-local-slides.py lubuntu
+	python generate-local-slides.py edubuntu
+	python generate-local-slides.py ubuntustudio
+	python generate-local-slides.py oem-config-ubuntu
 
 .PHONY : clean
 

=== added file 'generate-local-slides.py'
--- generate-local-slides.py	1970-01-01 00:00:00 +0000
+++ generate-local-slides.py	2012-07-20 17:19:18 +0000
@@ -0,0 +1,76 @@
+#!/usr/bin/python
+
+import os, sys, glob, subprocess
+import json
+
+if len(sys.argv) > 1:
+	distro = sys.argv[1]
+else:
+	print("Usage: %s <distro>" % sys.argv[0]);
+	sys.exit(-1)
+
+if not subprocess.call(["which", "po4a-translate"],
+                       stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0:
+	print("Error: po4a is not available.")
+	sys.exit(1)
+
+source_dir = '.'
+po_dir = os.path.join(source_dir, 'po', distro)
+
+build_dir = 'build'
+build_slides = os.path.join(build_dir, distro, 'slides')
+
+template_slides = glob.glob(os.path.join(build_slides, '*.html'))
+template_slides.remove(os.path.join(build_slides, 'index.html'))
+
+directory = {}
+
+for locale_file in glob.glob( os.path.join(po_dir, '*.po') ):
+	locale_name = os.path.basename(locale_file).rstrip('.po')
+	locale_slides = os.path.join(build_slides, 'l10n', locale_name)
+	
+	print("Working on locale %s" % locale_name)
+	
+	directory[locale_name] = {
+		'slides' : [],
+		'media' : []
+	}
+	
+	for template_slide in template_slides:
+		slide_name = os.path.basename(template_slide)
+		output_slide = os.path.join(locale_slides, slide_name)
+		
+		try:
+			os.makedirs(locale_slides)
+		except OSError:
+			# Directory already exists
+			pass
+		
+		if os.path.exists(output_slide):
+			os.remove(output_slide)
+		
+		# -k 1 -> if there are any translations at all, keep it.
+		subprocess.call(['po4a-translate',
+			             '-M', 'UTF-8',
+			             '-f', 'xhtml',
+			             '-m',  template_slide,
+			             '-p', locale_file,
+			             '-l', output_slide,
+			             '-k', '1',
+			             '-o', 'attributes="data-translate"'])
+		
+		if os.path.exists(output_slide):
+			directory[locale_name]['slides'].append(slide_name)
+		else:
+			#print("\t%s was not translated for locale %s" \
+			#	  % (slide_name, locale_name))
+			try:
+				os.rmdir(locale_slides)
+			except OSError:
+				# Directory is not empty
+				pass
+	
+	directory_file = open(os.path.join(build_slides, 'directory.json'), 'w')
+	json.dump(directory, directory_file)
+	directory_file.close()
+

=== removed file 'generate-local-slides.sh'
--- generate-local-slides.sh	2012-01-24 03:41:41 +0000
+++ generate-local-slides.sh	1970-01-01 00:00:00 +0000
@@ -1,49 +0,0 @@
-#!/bin/sh
-
-distro=$1
-if [ -z $distro ]; then
-	echo "Usage: $0 <distro>"
-	exit -1
-fi
-
-SOURCE=.
-BUILD=build
-
-PODIR=$SOURCE/po/$distro
-BUILDSLIDES=$BUILD/$distro/slides
-
-if ! which po4a-translate >/dev/null; then
-	echo; echo "Error: po4a is not available."
-	exit 1
-fi
-
-echo "directory = new Object()" > $BUILDSLIDES/directory.js
-
-for locale in $PODIR/*.po; do
-	if [ -e $locale ]; then
-		localename=$(basename $locale .po)
-		localeslides=$BUILDSLIDES/loc.$localename
-		
-		echo "directory['$localename'] = new Object()" >> $BUILDSLIDES/directory.js
-		echo "Found locale: $locale"
-		
-		for slide in $BUILDSLIDES/*.html; do
-			slidename=$(basename $slide)
-			[ $slidename = "index.html" ] && continue
-			
-			outputslide="$localeslides/$slidename"
-			[ -e $outputslide ] && rm -f $outputslide
-			[ ! -e $localeslides ] && mkdir -p $localeslides
-			
-			# -k 1 -> if there are any translations at all, keep it.
-			po4a-translate -M UTF-8 -f xhtml -m $slide -p $locale -l $outputslide -k 1 -o attributes="data-translate"
-			if ! [ -e "$outputslide" ]; then
-				rmdir $localeslides 2>/dev/null || true
-				echo "              $slidename was not translated for locale $localename"
-			else
-				echo "directory['$localename']['$slidename'] = true" >> $BUILDSLIDES/directory.js
-				#echo "              translated $slide for $locale locale"
-			fi
-		done
-	fi
-done

=== modified file 'slideshows/edubuntu/slides/index.html'
--- slideshows/edubuntu/slides/index.html	2012-02-22 15:45:39 +0000
+++ slideshows/edubuntu/slides/index.html	2012-07-20 17:19:18 +0000
@@ -14,7 +14,6 @@
 	
 	<script type="text/javascript" src="link-core/base.js"></script>
 	
-	<script type="text/javascript" src="directory.js"></script>
 	<script type="text/javascript">
 		SLIDESHOW_OPTIONS = {
 			fx:'fade',

=== modified file 'slideshows/kubuntu/slides/index.html'
--- slideshows/kubuntu/slides/index.html	2012-01-21 23:33:25 +0000
+++ slideshows/kubuntu/slides/index.html	2012-07-20 17:19:18 +0000
@@ -14,8 +14,6 @@
 	
 	<script type="text/javascript" src="link-core/base.js"></script>
 	
-	<script type="text/javascript" src="directory.js"></script>
-	
 	<script type="text/javascript" src="link-core/slideshow.js"></script>
 </head>
 

=== modified file 'slideshows/link-core/base.js'
--- slideshows/link-core/base.js	2012-07-06 00:25:07 +0000
+++ slideshows/link-core/base.js	2012-07-20 17:19:18 +0000
@@ -7,7 +7,8 @@
 	'locale' : 'en'
 };
 (function() {
-	var parameters = window.location.hash.slice(window.location.hash.indexOf('#') + 1).split('?');
+	var hash = window.location.hash.split('#')[1] || '';
+	var parameters = hash.split(/\?|&/);
 	$.each(parameters, function(i, parameter) {
 		var hash = parameter.split('=');
 		var key = hash[0];

=== modified file 'slideshows/link-core/slideshow.js'
--- slideshows/link-core/slideshow.js	2012-07-06 00:25:07 +0000
+++ slideshows/link-core/slideshow.js	2012-07-20 17:19:18 +0000
@@ -16,7 +16,6 @@
 link-core/jquery.js
 link-core/jquery.cycle.all.js
 link-core/signals.js
-directory.js (note that this file does not exist yet, but will when the build script runs)
 */
 
 /* Pass parameters by creating a global SLIDESHOW_OPTIONS object, containing
@@ -35,10 +34,38 @@
 
 var slideshow;
 
-var SLIDESHOW_TRANSLATED;
-var SLIDESHOW_LOCALE;
+var directory = {};
+var extra_directory = {};
 
 $(document).ready(function() {
+	function loadExtraSlides() {
+		$.ajax({
+			type: 'GET',
+			url: 'extra/directory.json',
+			cache: false,
+			success: function(data, status, xhr) {
+				extra_directory = $.extend(extra_directory, data);
+			},
+			complete: function(xhr, status) {
+				slideshowReady();
+			}
+		});
+	}
+	
+	$.ajax({
+		type: 'GET',
+		url: 'directory.json',
+		cache: false,
+		success: function(data, status, xhr) {
+			directory = $.extend(directory, data);
+		},
+		complete: function(xhr, status) {
+			loadExtraSlides();
+		}
+	});
+});
+
+function slideshowReady() {
 	slideshow = $('#slideshow');
 	
 	var slideshow_options = {
@@ -52,13 +79,12 @@
 	
 	$.extend(slideshow_options, window.SLIDESHOW_OPTIONS);
 	
-	if ( 'locale' in INSTANCE_OPTIONS )
-		setLocale(INSTANCE_OPTIONS['locale']);
-	
 	if ( 'rtl' in INSTANCE_OPTIONS )
 		$(document.body).addClass('rtl');
 	
-	loadSlides();
+	var locale = INSTANCE_OPTIONS['locale'] || 'C';
+	loadSlides(locale);
+	
 	Signals.fire('slideshow-loaded');
 	
 	slideshow_options.before = function(curr, next, opts) {
@@ -105,57 +131,38 @@
 	
 	slideshow.cycle(slideshow_options);
 	Signals.fire('slideshow-started');
-});
-
-
-function setLocale(locale) {
-	SLIDESHOW_TRANSLATED = true;
-	SLIDESHOW_LOCALE = locale;
-	
-	slideshow.find('div>a').each(function() {
-		var new_url = get_translated_url($(this).attr('href'), locale);
-		
-		if ( new_url != undefined ) {
-			$(this).attr('href', new_url);
-		} else {
-			SLIDESHOW_TRANSLATED = false;
-		}
-	})
-	
-	function get_translated_url(slide_name, locale) {
-		var translated_url = undefined;
-		
-		if ( translation_exists(slide_name, locale) ) {
-			translated_url = "./loc."+locale+"/"+slide_name;
-		} else {
-			var before_dot = locale.split(".",1)[0];
-			var before_underscore = before_dot.split("_",1)[0];
-			if ( before_underscore != null && translation_exists(slide_name, before_underscore) )
-				translated_url = "./loc."+before_underscore+"/"+slide_name;
-			else if ( before_dot != null && translation_exists(slide_name, before_dot) )
-				translated_url = "./loc."+before_dot+"/"+slide_name;
-		}
-		
-		return translated_url;
+};
+
+function getTranslatedFile(locale, file_name, file_category) {
+	var territory = locale.split(".",1)[0];
+	var language = territory.split("_",1)[0];
+	
+	return tryFileLocale(locale, file_name, file_category) ||
+	       tryFileLocale(territory, file_name, file_category) ||
+	       tryFileLocale(language, file_name, file_category) ||
+	       tryFileLocale('C', file_name, file_category);
+	
+	function tryFileLocale(locale, file_name, file_category) {
+		if (translationFileExists(extra_directory, locale, file_name, file_category)) {
+			// extra_directory can override slides from any locale, including C
+			return './extra/'+locale+'/'+file_name;
+		} else if (locale == 'C') {
+			return './'+file_name;
+		} else if (translationFileExists(directory, locale, file_name, file_category)) {
+			return './l10n/'+locale+'/'+file_name;
+		} else {
+			return undefined;
+		}
 	}
 	
-	function translation_exists(slide_name, locale) {
-		result = false;
-		try {
-			result = ( directory[locale][slide_name] == true );
-		} catch(err) {
-			/*
-			This usually happens if the directory object
-			(auto-generated at build time, placed in ./directory.js)
-			does not exist. That object is needed to know whether
-			a translation exists for the given locale.
-			*/
-		}
-		return result;
+	function translationFileExists(directory, locale, file_name, file_category) {
+		return locale in directory &&
+		       file_category in directory[locale] &&
+		       directory[locale][file_category].indexOf(file_name) >= 0;
 	}
 }
 
-function loadSlides() {
+function loadSlides(locale) {
 	var selected_slidesets = []
 	if ( 'slidesets' in INSTANCE_OPTIONS )
 		selected_slidesets = INSTANCE_OPTIONS['slidesets'].split(' ');
@@ -163,7 +170,6 @@
 	function slideIsVisible(slide) {
 		var slidesets = $(slide).data('slidesets');
 		var is_visible = true;
-		
 		if ( slidesets !== undefined ) {
 			is_visible = false;
 			$.each(slidesets.split(' '), function(index, slideset) {
@@ -172,24 +178,40 @@
 				}
 			});
 		}
-		
 		return is_visible;
 	}
 	
+	function translateSlideContents(locale, contents) {
+		// Warning: this assumes all images are local.
+		// TODO: Only translate images selected by $(img.translatable)
+		var images = $('img', contents);
+		$.each(images, function(index, image) {
+			var image_name = $(image).attr('src');
+			var translated_image = getTranslatedFile(locale, image_name, 'media');
+			$(image).attr('src', translated_image);
+		});
+	}
+	
 	slideshow.children('div').each(function(index, slide) {
 		if ( slideIsVisible($(slide)) ) {
-			var url = $(slide).children('a').attr('href');
+			var slide_name = $(slide).children('a').attr('href');
+			var translated_slide = getTranslatedFile(locale, slide_name, 'slides');
+			
 			$.ajax({
-				url: url,
+				type: 'GET',
+				url: translated_slide,
+				cache: false,
 				async: false,
 				success: function(data, status, xhr) {
-					$(slide).append(data);	
+					$(data).appendTo(slide);
+					translateSlideContents(locale, slide);
 				}
 			});
 		} else {
 			$(slide).detach();
 		}
 	});
+	
 	slideshow.children('div').first().data('first', true);
 	slideshow.children('div').last().data('last', true);
 }

=== modified file 'slideshows/lubuntu/slides/index.html'
--- slideshows/lubuntu/slides/index.html	2012-07-01 09:56:05 +0000
+++ slideshows/lubuntu/slides/index.html	2012-07-20 17:19:18 +0000
@@ -14,7 +14,6 @@
 	
 	<script type="text/javascript" src="link-core/base.js"></script>
 	
-	<script type="text/javascript" src="directory.js"></script>
 	<script type="text/javascript">
 		SLIDESHOW_OPTIONS = {
 			timeout:50000,

=== modified file 'slideshows/ubuntu/slides/index.html'
--- slideshows/ubuntu/slides/index.html	2012-01-23 07:19:54 +0000
+++ slideshows/ubuntu/slides/index.html	2012-07-20 17:19:18 +0000
@@ -14,7 +14,6 @@
 	
 	<script type="text/javascript" src="link-core/base.js"></script>
 	
-	<script type="text/javascript" src="directory.js"></script>
 	<script type="text/javascript">
 		SLIDESHOW_OPTIONS = {
 			timeout:50000,

=== modified file 'slideshows/ubuntustudio/slides/index.html'
--- slideshows/ubuntustudio/slides/index.html	2012-03-19 22:01:22 +0000
+++ slideshows/ubuntustudio/slides/index.html	2012-07-20 17:19:18 +0000
@@ -10,7 +10,6 @@
 	<script type="text/javascript" src="link-core/jquery.js"></script>
 	<script type="text/javascript" src="link-core/jquery.cycle.all.js"></script>
 	<script type="text/javascript" src="link-core/base.js"></script>
-	<script type="text/javascript" src="directory.js"></script>
 	<script type="text/javascript">
 		SLIDESHOW_OPTIONS = {
 			timeout:50000,

=== modified file 'slideshows/xubuntu/slides/index.html'
--- slideshows/xubuntu/slides/index.html	2012-01-31 15:40:39 +0000
+++ slideshows/xubuntu/slides/index.html	2012-07-20 17:19:18 +0000
@@ -14,7 +14,6 @@
 	
 	<script type="text/javascript" src="link-core/base.js"></script>
 	
-	<script type="text/javascript" src="directory.js"></script>
 	<script type="text/javascript">
 		SLIDESHOW_OPTIONS = {
 			timeout:50000,


Follow ups