2005-01-31 20:32:49 +00:00
< ? php
2008-08-05 22:43:14 +00:00
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
2005-01-31 20:32:49 +00:00
$lang_language_dir = " ../languages/ " ;
$lang_translations_dir = " translations/ " ;
2005-02-17 12:26:04 +00:00
$lang_prj_translations_dir = " project_specific_translations/ " ;
2005-01-31 20:32:49 +00:00
$lang_compiled_dir = " compiled/ " ;
2009-04-06 18:27:02 +00:00
$lang_log_level = 1 ;
2005-01-31 20:32:49 +00:00
2009-09-28 04:24:18 +00:00
// Fetches a list of compiled languages from the directory
// set to contain such files.
// @returns A list of languages that have been compiled
//
2005-04-02 15:49:30 +00:00
function getSupportedLanguages (){
global $lang_language_dir , $lang_compiled_dir ;
2009-03-24 21:24:57 +00:00
$list = array ();
2005-04-02 15:49:30 +00:00
if ( is_dir ( $lang_language_dir . $lang_compiled_dir )) {
2008-09-22 17:14:03 +00:00
if ( $dh = opendir ( $lang_language_dir . $lang_compiled_dir )) {
while ( $file = readdir ( $dh )) {
if ( substr ( $file , - 7 ) != " .po.inc " ) continue ;
2005-10-29 04:56:33 +00:00
if ( is_numeric ( substr ( $file , 0 , 5 ))) continue ;
$list [] = substr ( $file , 0 , - 7 );
2005-04-02 18:37:22 +00:00
}
}
2007-05-06 22:38:43 +00:00
} else {
2008-08-15 17:33:32 +00:00
echo " \" " . $lang_language_dir . $lang_compiled_dir . " \" is not a directory. Please consult the documentation for correctly setting up the translation system. " ;
exit ;
2007-05-06 22:38:43 +00:00
}
2005-04-02 15:49:30 +00:00
return $list ;
}
2009-09-28 04:24:18 +00:00
// Builds the lookup arrays from the
// language files found in the given directory tree.
// @param langdir The language base directory
// @param transdir The location of the .po files to compile relative to langdir
// @param compdir The output location relative to langdir
// @param append If true the function will only append to rather than replace the compiled files
//
2009-05-18 04:18:47 +00:00
function buildLanguages ( $langdir , $transdir , $compdir ){
2005-02-17 12:26:04 +00:00
global $lang_project_default ;
2005-03-02 13:33:07 +00:00
2008-10-12 15:22:16 +00:00
// Run through each language and compile their lookup arrays.
2005-01-31 20:32:49 +00:00
if ( is_dir ( $langdir . $transdir )) {
2005-04-02 18:37:22 +00:00
if ( $dh = opendir ( $langdir . $transdir )) {
while (( $file = readdir ( $dh )) !== false ) {
2005-01-31 20:32:49 +00:00
if ( $file == " .. " or $file == " . " ){
2008-10-14 21:40:14 +00:00
// Skip parent and current dir
2005-04-02 18:37:22 +00:00
} else if ( substr ( $file , - 3 ) == " .po " ){
2008-10-12 15:22:16 +00:00
// and only do files ending in .po
2009-04-06 18:27:02 +00:00
language_log (
" -------------Compiling $transdir $file ------------ " , 0
);
2008-11-28 19:03:05 +00:00
$language = parseLanguage ( $langdir . $transdir . $file );
2008-10-14 21:40:14 +00:00
if ( ! $language ){
language_log (
" WARNING: Could not parse language " . $file
);
continue ;
}
2009-05-18 04:18:47 +00:00
$path = $langdir . $compdir . $file . " .inc " ;
if ( file_exists ( $path )) {
$fh = fopen ( $path , " a " );
} else {
$fh = fopen ( $path , " w " );
fwrite ( $fh , " <?php \n " );
}
2009-03-24 23:27:12 +00:00
if ( ! $fh ) {
2006-05-09 18:25:15 +00:00
language_log (
2009-04-06 18:27:02 +00:00
" ERROR: could not access $langdir $compdir - please check permissions " , 2
2006-05-09 18:25:15 +00:00
);
2005-04-02 18:37:22 +00:00
exit ;
2005-10-29 05:30:57 +00:00
}
2008-10-12 15:22:16 +00:00
foreach ( $language as $key => $value ){
if ( $value !== " " ) {
//Skip if the msgstr is empty
fwrite ( $fh , " \$ language_lookup_array[ \" " . str_replace ( " \" " , " \\ \" " , substr ( $file , 0 , - 3 )) . " \" ][ \" " . $key . " \" ] = \" " . $value . " \" ; \n " );
2005-04-02 18:37:22 +00:00
}
}
2009-03-24 23:27:12 +00:00
// don't write \?\> - may append
2005-04-02 18:37:22 +00:00
fclose ( $fh );
2005-01-31 20:32:49 +00:00
} else {
2005-04-02 18:37:22 +00:00
//debug("File $file with unknown extension found in $info_dir");
}
2005-01-31 20:32:49 +00:00
}
closedir ( $dh );
} else {
2005-04-02 18:37:22 +00:00
//debug("$info_dir could not be opened - check permissions?");
}
2005-01-31 20:32:49 +00:00
} else {
2005-04-02 18:37:22 +00:00
//debug("$info_dir not found or is not a directory");
2005-01-31 20:32:49 +00:00
}
}
2009-09-28 04:24:18 +00:00
// Parses a gettext .po-file into an associative PHP array.
// @param file The file to parse
// checking for inconsistencies if needed.
//
2008-10-12 15:22:16 +00:00
function parseLanguage ( $file ){
2005-01-31 20:32:49 +00:00
$translation_file = file ( $file );
$first_entry = true ;
2007-04-23 16:14:47 +00:00
$current_token_text = " " ;
2009-05-06 09:49:46 +00:00
$current_token = " " ;
$parsing_token = false ;
$parsing_text = false ;
$output = array ();
2005-01-31 20:32:49 +00:00
for ( $i = 0 ; $i < sizeof ( $translation_file ); $i ++ ){
2009-05-06 09:49:46 +00:00
$entry = trim ( $translation_file [ $i ]);
//echo "line $i: $entry\n";
if ( substr ( $entry , 0 , 1 ) == " # " ) {
continue ;
} elseif ( strpos ( $entry , " msgid " ) !== false ) {
2005-04-02 18:37:22 +00:00
if ( ! $first_entry ){
//If this is not the first, save the previous entry
2008-12-08 19:39:19 +00:00
$output [ $current_token ] = $current_token_text ;
2005-04-02 18:37:22 +00:00
}
$current_token = getPOLineContent ( $entry );
$current_token_text = " " ;
2009-05-06 09:49:46 +00:00
$parsing_token = true ;
$parsing_text = false ;
2005-04-02 18:37:22 +00:00
$first_entry = false ;
2009-05-06 09:49:46 +00:00
} elseif ( strpos ( $entry , " msgstr " ) !== false ) {
$current_token_text = getPOLineContent ( $entry );
$parsing_token = false ;
$parsing_text = true ;
} elseif ( $parsing_token ) {
$current_token .= getPOLineContent ( $entry );
} elseif ( $parsing_text ) {
2005-04-02 18:37:22 +00:00
$current_token_text .= getPOLineContent ( $entry );
}
2005-01-31 20:32:49 +00:00
}
2008-10-12 15:22:16 +00:00
// Get the last token
if ( $current_token && $current_token_text ){
2008-10-14 21:40:14 +00:00
$output [ $current_token ] = $current_token_text ;
2005-01-31 20:32:49 +00:00
}
return $output ;
}
2009-09-28 04:24:18 +00:00
// Returns the contents of a line (ie removes "" from start and end)
//
2005-01-31 20:32:49 +00:00
function getPOLineContent ( $line ){
$start = strpos ( $line , '"' ) + 1 ;
$stop = strrpos ( $line , '"' );
2009-05-14 22:32:05 +00:00
$x = substr ( $line , $start , $stop - $start );
$n = ereg ( " [^ \\ ] \" " , $x );
if ( $n ) {
2009-05-19 21:00:03 +00:00
echo " ERROR - MISMATCHED QUOTES IN $line\n " ;
2009-05-14 22:32:05 +00:00
return " " ;
}
return $x ;
2005-01-31 20:32:49 +00:00
}
2009-09-28 04:24:18 +00:00
// Translate string
//
2008-10-12 15:22:16 +00:00
function tra ( $text /* ...arglist... */ ){
global $language_lookup_array , $languages_in_use ;
// Find the string in the user's language
2009-01-30 21:25:24 +00:00
//
2008-10-12 15:22:16 +00:00
foreach ( $languages_in_use as $language ){
2008-10-30 18:27:22 +00:00
if ( isset ( $language_lookup_array [ $language ][ $text ])) {
2008-10-14 21:40:14 +00:00
$text = $language_lookup_array [ $language ][ $text ];
break ;
2009-01-25 10:45:07 +00:00
} else if ( $language == " en " ){
2009-01-30 21:25:24 +00:00
// This language is defined in the code and is always available
break ;
}
2005-01-31 20:32:49 +00:00
}
2008-06-05 20:32:21 +00:00
2010-01-19 23:01:09 +00:00
// Replace relevant substrings with given arguments.
// Use strtr to avoid problems if an argument contains %n.
$replacements = array ();
2008-10-12 15:22:16 +00:00
for ( $i = 1 ; $i < func_num_args (); $i ++ ){
2010-01-19 23:01:09 +00:00
$replacements [ " % " . $i ] = func_get_arg ( $i );
2005-02-08 12:18:02 +00:00
}
2010-01-19 23:01:09 +00:00
$text = strtr ( $text , $replacements );
2008-10-12 15:22:16 +00:00
return $text ;
2005-01-31 20:32:49 +00:00
}
2008-10-12 15:22:16 +00:00
function tr_specific ( $text , $language ){
global $lang_language_dir , $lang_compiled_dir , $language_lookup_array ;
$file_name = $lang_language_dir . $lang_compiled_dir . $language . " .po.inc " ;
if ( file_exists ( $file_name )) {
2008-10-14 21:40:14 +00:00
require_once ( $file_name );
$text = $language_lookup_array [ $language ][ $text ];
2008-10-12 15:22:16 +00:00
}
return $text ;
2007-08-25 13:28:01 +00:00
}
2005-02-24 18:48:12 +00:00
function language_log ( $message , $loglevel = 0 ){
2009-03-24 21:24:57 +00:00
global $lang_log_level ;
2005-02-24 18:48:12 +00:00
if ( $loglevel == 0 ) $msg = " [ Debug ] " ;
if ( $loglevel == 1 ) $msg = " [ Warning ] " ;
if ( $loglevel == 2 ) $msg = " [ CRITICAL ] " ;
2008-06-05 20:32:21 +00:00
2009-04-06 18:27:02 +00:00
if ( $loglevel >= $lang_log_level ){
2009-03-24 21:24:57 +00:00
echo date ( " Y-m-d H:i:s " , time ()) . " " . $msg . " " . $message . " \n " ;
2005-02-24 18:48:12 +00:00
}
}
2006-08-21 01:44:03 +00:00
// Make a list of languages which the user prefers
// (by looking at cookies and browser settings)
// cookies have highest priority.
2005-04-02 15:49:30 +00:00
2006-08-21 01:44:03 +00:00
if ( isset ( $_COOKIE [ 'lang' ])){
$language_string = $_COOKIE [ 'lang' ] . " , " ;
} else {
$language_string = '' ;
}
2007-04-27 16:27:12 +00:00
if ( isset ( $_SERVER [ " HTTP_ACCEPT_LANGUAGE " ])) {
$language_string .= strtolower ( $_SERVER [ " HTTP_ACCEPT_LANGUAGE " ]);
2006-08-21 01:44:03 +00:00
}
2005-04-02 15:49:30 +00:00
2006-09-26 17:02:46 +00:00
// Find out which language to use by iterating through list
// The list is comma-separated, so split it into an array of the following type:
2008-06-05 20:32:21 +00:00
// Array (
// [0] => da
// [1] => en-us;q=0.7
// [2] => en;q=0.3
2006-08-21 01:44:03 +00:00
// )
$client_languages = explode ( " , " , $language_string );
2006-09-26 17:02:46 +00:00
// A language is either defined as primary-secondary or primary.
2006-08-21 01:44:03 +00:00
// It can also have a quality attribute set,
// which orders the languages in a user preferred ordering.
// Since this is usally the same order as the array indices
// we just ignore this attribute (TODO: don't ignore this attribute)
// A missing quality attribute means q=1
2008-10-12 15:22:16 +00:00
$languages_in_use = array ();
2006-08-21 01:44:03 +00:00
2008-10-12 15:22:16 +00:00
// Loop over languages that the client requests
for ( $i = 0 ; $i < sizeof ( $client_languages ); $i ++ ) {
2006-08-21 01:44:03 +00:00
if (( strlen ( $client_languages [ $i ]) > 2 )
&& ( substr ( $client_languages [ $i ], 2 , 1 ) == " _ " || substr ( $client_languages [ $i ], 2 , 1 ) == " - " ))
{
// If this is defined as primary-secondary, represent it as xx_YY
//
$language = substr (
$client_languages [ $i ], 0 , 2 ) . " _ " . strtoupper ( substr ( $client_languages [ $i ], 3 , 2 )
);
2006-09-26 17:02:46 +00:00
// And also check for the primary language
//
$language2 = substr ( $client_languages [ $i ], 0 , 2 );
2005-10-01 17:10:31 +00:00
} else {
2006-08-21 01:44:03 +00:00
// else just use xx
//
$language = substr ( $client_languages [ $i ], 0 , 2 );
2006-09-26 17:02:46 +00:00
$language2 = null ;
2005-10-01 17:50:15 +00:00
}
2006-09-26 17:02:46 +00:00
2009-04-16 09:01:41 +00:00
// if main language is english, look no further
//
if ( $i == 0 && $language == 'en' ) {
break ;
}
2006-08-21 01:44:03 +00:00
// If we have a translation for the language, include it
2006-09-26 17:02:46 +00:00
$file_name = $lang_language_dir . $lang_compiled_dir . $language . " .po.inc " ;
if ( file_exists ( $file_name )) {
2008-10-14 21:40:14 +00:00
if ( ! in_array ( $language , $languages_in_use )){
2008-10-12 15:22:16 +00:00
require_once ( $file_name );
2009-01-30 21:25:24 +00:00
$languages_in_use [] = $language ;
2008-10-14 21:40:14 +00:00
}
2005-01-31 20:32:49 +00:00
}
2006-09-26 17:02:46 +00:00
if ( $language2 ) {
$file_name = $lang_language_dir . $lang_compiled_dir . $language2 . " .po.inc " ;
if ( file_exists ( $file_name )) {
2008-10-14 21:40:14 +00:00
if ( ! in_array ( $language2 , $languages_in_use )){
require_once ( $file_name );
$languages_in_use [] = $language2 ;
}
2006-09-26 17:02:46 +00:00
}
}
2005-10-29 05:30:57 +00:00
}
2005-01-31 20:32:49 +00:00
2010-02-03 23:04:34 +00:00
$GLOBALS [ 'languages_in_use' ] = $languages_in_use ; // for Drupal
2008-08-05 22:43:14 +00:00
$cvs_version_tracker [] = " \$ Id $ " ; //Generated automatically - do not edit
2008-10-14 21:40:14 +00:00
?>