From 8207024c6e242f500b49b171a1527f8e6111e69a Mon Sep 17 00:00:00 2001 From: spiderr Date: Sat, 30 Apr 2011 00:47:46 -0400 Subject: seperate out string translation per language into a separate file, add support for google translate API --- BitLanguage.php | 11 ++ ajax_translate.php | 24 ++++ edit_languages.php | 28 +--- icons/google-favicon.png | Bin 0 -> 993 bytes templates/edit_languages.tpl | 259 +++++++++++++++---------------------- templates/menu_languages.tpl | 1 + templates/menu_languages_admin.tpl | 3 +- templates/translate_strings.tpl | 81 ++++++++++++ translate_strings.php | 99 ++++++++++++++ 9 files changed, 321 insertions(+), 185 deletions(-) create mode 100644 ajax_translate.php create mode 100644 icons/google-favicon.png create mode 100644 templates/translate_strings.tpl create mode 100644 translate_strings.php diff --git a/BitLanguage.php b/BitLanguage.php index 1e7510e..b92dee6 100644 --- a/BitLanguage.php +++ b/BitLanguage.php @@ -636,6 +636,17 @@ class BitLanguage extends BitBase { return (isset( $ret['trans'] ) ? $ret['trans'] : NULL ); } + /** + * getMasterString + * + * @param string $pSourceHash + * @access public + * @return master string with given source hash + */ + function getMasterString( $pSourceHash ) { + return( $this->mDb->getOne( "SELECT `source` FROM `" . BIT_DB_PREFIX . "i18n_masters` WHERE `source_hash` = ? ", array( $pSourceHash ) ) ); + } + /** * getSourceHash * diff --git a/ajax_translate.php b/ajax_translate.php new file mode 100644 index 0000000..7b97111 --- /dev/null +++ b/ajax_translate.php @@ -0,0 +1,24 @@ +getMasterString( $_REQUEST['source_hash'] ) ) { + + $googleUrl = "https://www.googleapis.com/language/translate/v2?key=".$gBitSystem->getConfig('google_api_key')."&source=en&target=".$_REQUEST['lang']."&q=".urlencode( $masterString ); + + if( $fh = fopen( $googleUrl, "r" ) ) { + $jsonResponse = fread( $fh, 8192 ); + $data = json_decode( $jsonResponse ); + fclose( $fh ); + } + if( !empty( $data->data->translations[0]->translatedText ) ) { + $translation = $data->data->translations[0]->translatedText; + } + } +} + +print $translation; diff --git a/edit_languages.php b/edit_languages.php index e596629..b4e20d8 100644 --- a/edit_languages.php +++ b/edit_languages.php @@ -102,34 +102,8 @@ if( !empty( $_REQUEST['clear_cache'] ) ) { $gBitSmarty->assign_by_ref( 'defaults', $languages[$_REQUEST['lang']] ); } $gBitSmarty->assign( 'editDescription', TRUE ); -} elseif( !empty( $_REQUEST['save_translations'] ) ) { - $editLang = $_REQUEST['lang']; - $gBitLanguage->loadLanguage( $editLang ); - $storedStrings = NULL; - foreach( $_REQUEST['edit_trans'] as $sourceHash => $string ) { - if( $string != $gBitLanguage->mStrings[$editLang][$sourceHash]['trans'] ) { - // we need to remove the $_REQUEST slashes here to avoid stuff like: - // {$gBitSystem->getConfig(\'stuff\')} in the translated strings - - // it will kill the site since smarty won't be able to interpret - // the template anymore --xing - if( ini_get( 'magic_quotes_gpc' ) ) { - $string = stripslashes( $string ); - } - $gBitLanguage->storeTranslationString( $editLang, $string, $sourceHash ); - // update string in template as well - $tranStrings[$sourceHash]['trans'] = $string; - // this has to be the source, otherwise the translated string will enter the db and be recognised as a used master - $storedStrings[] = $gBitLanguage->mStrings[$editLang][$sourceHash]['source']; - } - } - $tranStrings = $gBitLanguage->getTranslationString( $sourceHash, $editLang ); - $gBitSmarty->assign_by_ref('tranStrings', $tranStrings ); - $gBitSmarty->assign( 'lang', $editLang ); - $gBitSmarty->assign( 'translate', TRUE ); - $gBitSmarty->assign( 'saveSuccess', tra( "The following items have been saved successfully" ).":" ); - $gBitSmarty->assign( 'storedStrings', $storedStrings ); } -$gBitSystem->display( 'bitpackage:languages/edit_languages.tpl', tra( 'Edit Translations' ) , array( 'display_mode' => 'edit' )); +$gBitSystem->display( 'bitpackage:languages/edit_languages.tpl', tra( 'Edit Languages' ) , array( 'display_mode' => 'edit' )); ?> diff --git a/icons/google-favicon.png b/icons/google-favicon.png new file mode 100644 index 0000000..81e390d Binary files /dev/null and b/icons/google-favicon.png differ diff --git a/templates/edit_languages.tpl b/templates/edit_languages.tpl index 4295b42..c726436 100644 --- a/templates/edit_languages.tpl +++ b/templates/edit_languages.tpl @@ -7,177 +7,122 @@
- {if !$translate} - {jstabs} - {if $editDescription} - {if $gBitUser->hasPermission( 'p_languages_create' )} - {jstab title="Create or Edit Language"} - {form legend="Create or Edit Language"} - {formfeedback error=$saveErrors } - -
- {formlabel label="Native name of the language" for="native_name"} - {forminput} - - {/forminput} -
-
- {formlabel label="English name of the language" for="english_name"} - {forminput} - - {/forminput} -
-
- {formlabel label="Language code" for="lang_code"} - {forminput} - - {formhelp note='For official language codes, see ISO639-1 and to add a localisation you can append the country code from ISO 3166-1.
e.g. ISO639-1 for english is "en" and the country code for britain is "uk". The resulting code is "en-uk".'} - {/forminput} -
-
- {formlabel label="Disabled" for="is_disabled"} - {forminput} - - {formhelp note="Disabling a language will remove it from available language menus."} - {/forminput} -
-
- -
- {formhelp note="A note for localisations: when you have a language, say 'de' and you add a localisation such as 'de-at' it will first check de-at for a string and then fall back to de. If it still hasn't found a translation, it will default to english."} - {/form} - {/jstab} - {/if} - {/if} - - {jstab title="Choose Language"} - {formfeedback success=$saveSuccess} - {form legend="Choose language"} -
- {formlabel label="Select the language to edit" for="select_language"} - {forminput} - - {/forminput} -
- - {if $gBitSystem->isFeatureActive( 'i18n_track_translation_usage' )} + {jstabs} + {if $editDescription} + {if $gBitUser->hasPermission( 'p_languages_create' )} + {jstab title="Create or Edit Language"} + {form legend="Create or Edit Language"} + {formfeedback error=$saveErrors } +
- {formlabel label="Display all strings" for="all_trans"} + {formlabel label="Native name of the language" for="native_name"} {forminput} - - {formhelp note="This will display translation strings for all bitweaver versions. This means it will also show strings that are not used at all and might be useless to you."} + + {/forminput} +
+
+ {formlabel label="English name of the language" for="english_name"} + {forminput} + + {/forminput} +
+
+ {formlabel label="Language code" for="lang_code"} + {forminput} + + {formhelp note='For official language codes, see ISO639-1 and to add a localisation you can append the country code from ISO 3166-1.
e.g. ISO639-1 for english is "en" and the country code for britain is "uk". The resulting code is "en-uk".'} + {/forminput} +
+
+ {formlabel label="Disabled" for="is_disabled"} + {forminput} + + {formhelp note="Disabling a language will remove it from available language menus."} {/forminput}
- {/if} - -
- {formlabel label="Only Untranslated" for="un_trans"} - {forminput} - - {formhelp note="Display only untranslated strings when editing the language translations."} - {/forminput} -
- - {formfeedback warning="Editing a language for the first time will cause an import of the language. this can take several minutes, depending on your configuration."} - -
-   -   - -
- - {formhelp note=" -
-
Delete Language
-
Delete the language from your server.
-
Edit Description
-
Edit the description of the language, including language code.
-
Edit Translations
-
Edit the translated strings of the selected language.
-
- "} - - {if $gBitUser->hasPermission( 'p_languages_create' )}
- +
- {/if} - {/form} - {/jstab} - - {jstab title="Language Cache"} - {formfeedback success=$saveSuccess} - {form legend="Clear Language Cache"} + {formhelp note="A note for localisations: when you have a language, say 'de' and you add a localisation such as 'de-at' it will first check de-at for a string and then fall back to de. If it still hasn't found a translation, it will default to english."} + {/form} + {/jstab} + {/if} + {/if} + + {jstab title="Choose Language"} + {formfeedback success=$saveSuccess} + {form legend="Choose language"} +
+ {formlabel label="Select the language to edit" for="select_language"} + {forminput} + + {/forminput} +
+ + {if $gBitSystem->isFeatureActive( 'i18n_track_translation_usage' )}
- {formlabel label="Clear Language Cache" for="clear_cache"} + {formlabel label="Display all strings" for="all_trans"} {forminput} - - {formhelp note="Clear the cached language translations for all languages. It is necessary to clear the cache when you have made changes to the language database."} + + {formhelp note="This will display translation strings for all bitweaver versions. This means it will also show strings that are not used at all and might be useless to you."} {/forminput}
- {/form} - {/jstab} - {/jstabs} - - {else} - - {form} - {alphabar iall=1 lang=$lang translate=1 un_trans=$unTrans all_trans=$allTrans} - - - + {/if} - {if $storedStrings} - {formfeedback success=$saveSuccess} - - - {legend legend="Clear Language Cache"} -
- {formlabel label="Clear Language Cache" for="clear_cache"} - {forminput} - - {formhelp note="Clear the cached language translations for all languages. It is necessary to clear the cache when you have made changes to the language database."} - {/forminput} -
- {/legend} - {else} - {legend legend="Edit `$languages.$lang.full_name` Language"} - {foreach from=$tranStrings key=sourceHash item=tran} - {if $allTrans || (!$gBitSystem->isFeatureActive( 'i18n_track_translation_usage' ) || $tran.version)} -
- {formlabel label="Translate" for="h_$sourceHash"} - {forminput} - {$tran.source|escape|nl2br}
- {if $tran.textarea} - - {else} - - {/if} - {/forminput} -
- {/if} - {/foreach} +
+ {formlabel label="Only Untranslated" for="un_trans"} + {forminput} + + {formhelp note="Display only untranslated strings when editing the language translations."} + {/forminput} +
+ + {formfeedback warning="Editing a language for the first time will cause an import of the language. this can take several minutes, depending on your configuration."} + +
+   +   + +
+ + {formhelp note=" +
+
Delete Language
+
Delete the language from your server.
+
Edit Description
+
Edit the description of the language, including language code.
+
Edit Translations
+
Edit the translated strings of the selected language.
+
+ "} + {if $gBitUser->hasPermission( 'p_languages_create' )}
-   - +
- - {alphabar iall=1 lang=$lang translate=1 un_trans=$unTrans all_trans=$allTrans} - {/legend} - {/if} - {/form} - {/if} + {/if} + {/form} + {/jstab} + + {jstab title="Language Cache"} + {formfeedback success=$saveSuccess} + {form legend="Clear Language Cache"} +
+ {formlabel label="Clear Language Cache" for="clear_cache"} + {forminput} + + {formhelp note="Clear the cached language translations for all languages. It is necessary to clear the cache when you have made changes to the language database."} + {/forminput} +
+ {/form} + {/jstab} + {/jstabs}
{/strip} diff --git a/templates/menu_languages.tpl b/templates/menu_languages.tpl index 20f44cb..d32bd22 100644 --- a/templates/menu_languages.tpl +++ b/templates/menu_languages.tpl @@ -2,6 +2,7 @@ {if $gBitUser->hasPermission( 'p_languages_edit' )} diff --git a/templates/menu_languages_admin.tpl b/templates/menu_languages_admin.tpl index d69d64d..c046a69 100644 --- a/templates/menu_languages_admin.tpl +++ b/templates/menu_languages_admin.tpl @@ -2,7 +2,8 @@ -{/strip} \ No newline at end of file +{/strip} diff --git a/templates/translate_strings.tpl b/templates/translate_strings.tpl new file mode 100644 index 0000000..0d43dfe --- /dev/null +++ b/templates/translate_strings.tpl @@ -0,0 +1,81 @@ +{strip} +
{bithelp}
+ + + +
+
+

{tr}Edit Languages{/tr}

+
+ +
+ {form} +
+ {formlabel label="Select the language to edit" for="select_language"} + {forminput} + + {/forminput} +
+ + {alphabar iall=1 lang=$smarty.request.lang translate=1 un_trans=$unTrans all_trans=$allTrans} + + + + + {if $smarty.request.lang} + {legend legend="Edit `$languages.$lang.full_name` Language"} + {foreach from=$tranStrings key=sourceHash item=tran} + {if $allTrans || (!$gBitSystem->isFeatureActive( 'i18n_track_translation_usage' ) || $tran.version)} +
+
+ + {if $gBitSystem->getConfig('google_api_key')} +
{biticon iname="google-favicon" ipackage="languages" iexplain="Auto-Translate"} Auto
+ {/if} +
+ {forminput} + {$tran.source|escape|nl2br}
+ {if $tran.textarea} + + {else} + + {/if} + {/forminput} +
+ {/if} + {/foreach} + +
+   + +
+ + {alphabar iall=1 lang=$editLang translate=1 un_trans=$unTrans all_trans=$allTrans} + {/legend} + {/if} + {/form} +
+
+{/strip} + diff --git a/translate_strings.php b/translate_strings.php new file mode 100644 index 0000000..3143ba6 --- /dev/null +++ b/translate_strings.php @@ -0,0 +1,99 @@ +verifyPermission( 'p_languages_edit' ); + +// Get available languages from DB +$languages = $gBitLanguage->listLanguages(); +$gBitSmarty->assign_by_ref('languages', $languages); + +if( !empty( $_REQUEST['all_trans'] ) ) { + $gBitSmarty->assign( 'allTrans', 1 ); +} + +if( !empty( $_REQUEST['un_trans'] ) ) { + $gBitSmarty->assign( 'unTrans', 1 ); +} + + +if( !empty( $_REQUEST['save_translations'] ) ) { + $editLang = $_REQUEST['lang']; + $gBitLanguage->loadLanguage( $editLang ); + $storedStrings = NULL; + foreach( $_REQUEST['edit_trans'] as $sourceHash => $string ) { + if( $string != $gBitLanguage->mStrings[$editLang][$sourceHash]['trans'] ) { + // we need to remove the $_REQUEST slashes here to avoid stuff like: + // {$gBitSystem->getConfig(\'stuff\')} in the translated strings - + // it will kill the site since smarty won't be able to interpret + // the template anymore --xing + if( ini_get( 'magic_quotes_gpc' ) ) { + $string = stripslashes( $string ); + } + $gBitLanguage->storeTranslationString( $editLang, $string, $sourceHash ); + // update string in template as well + $tranStrings[$sourceHash]['trans'] = $string; + // this has to be the source, otherwise the translated string will enter the db and be recognised as a used master + $storedStrings[] = $gBitLanguage->mStrings[$editLang][$sourceHash]['source']; + } + } + $tranStrings = $gBitLanguage->getTranslationString( $sourceHash, $editLang ); + $gBitSmarty->assign_by_ref('tranStrings', $tranStrings ); + $gBitSmarty->assign( 'lang', $editLang ); + $gBitSmarty->assign( 'translate', TRUE ); + $gBitSmarty->assign( 'saveSuccess', tra( "The following items have been saved successfully" ).":" ); + $gBitSmarty->assign( 'storedStrings', $storedStrings ); +} + +if( !empty( $_REQUEST['choose_lang'] ) ) { + $editLang = $_REQUEST['choose_lang']; + $gBitSmarty->assign( 'editLang', $editLang ); + if( !empty( $_REQUEST['hash'] ) ) { + $tranStrings = $gBitLanguage->getTranslationString( $_REQUEST['hash'], $editLang ); + $gBitSmarty->assign_by_ref('tranStrings', $tranStrings ); + } else { + // what strings do we want to display? + if( empty( $_REQUEST['char'] ) ) { + $pattern = "/^a/i"; + } elseif ( $_REQUEST['char'] == '0-9' ) { + $pattern = "/^\d/"; + } elseif ( $_REQUEST['char'] == '+' ) { + $pattern = "/^[^a-zA-Z]/"; + } elseif ( $_REQUEST['char'] == 'all' ) { + $pattern = "//"; + } else { + $pattern = "/^".$_REQUEST['char']."/i"; + } + $gBitLanguage->loadLanguage( $editLang ); + $tranStr = $gBitLanguage->mStrings[$editLang]; + foreach( $tranStr as $key => $tran ) { + // display only the wanted strings and apply a textbox if the string is too long + if( !empty( $_REQUEST['un_trans'] ) && empty( $tran['trans'] ) || empty( $_REQUEST['un_trans'] ) ) { + if( preg_match( $pattern, $tran['source'] ) ) { + $tranStrings[$key] = $tran; + if( strlen( $tran['source'] ) > 50 ) { + $tranStrings[$key]['textarea'] = TRUE; + } + } + } + } + $gBitSmarty->assign( 'char', empty( $_REQUEST['char'] ) ? '' : $_REQUEST['char'] ); + $gBitSmarty->assign_by_ref( 'tranStrings', $tranStrings ); + } +} + +$gBitSystem->display( 'bitpackage:languages/translate_strings.tpl', tra( 'Edit Translations' ) , array( 'display_mode' => 'edit' )); + -- cgit v1.3