Jump to content

Babel (Multi-Language Module)


dadish
 Share

Recommended Posts

Babel is a module that provides functionality for managing section based multilanguage sites. It is inspired by Babel Plugin for MODX.

There are cases where websites have 600 pages in english, 700 pages in russian and only 300 pages in english and russian. In this case it is much better to keep the content separate, each under one section and have their translations linked. This module helps you to manage those links.



How to Install

Warning

Backup your database before installing this module.

Requirements

Babel works on top of core LanguageSupport module. You will have to install it first and create languages for your site. Then:

1. Copy all the module files to /site/modules/Babel/ 

2. In your admin, go to Modules > Refresh for new modules. 

3. Click the "Install" button next to Babel.

Settings

First of all you should set the root page for each language. Usually section based multilingual sites will have a structure like...



Root
  |__Home (English)
       |__About
       |__News
           |__ News Article 1
           |__ News Article 2
           |__ News Article 3
           |__ News Article 4
       |__Contact
       ...
  |__Главная (Russian)
       |__О нас
       |__Новости
           |__ Новостная статья 1
           |__ Новостная статья 2
           |__ Новостная статья 3
           |__ Новостная статья 4
       |__Связаться
       ...
  |__Baş Sahypa (Turkmen)
       |__Barada
       |__Habarlar
           |__ Habar Makalasy 1
           |__ Habar Makalasy 2
           |__ Habar Makalasy 3
           |__ Habar Makalasy 4
       |__Aragatnaşyk
       ...


You just need to select the languages that you want Babel to manage and select the root pages for each of them.

bbl_settings.png

API

Babel creates couple useful methods and a properties for you.

language (Page property)

Every page that is handled by Babel (those who are descendants of root pages that you choose on Babel module's settings page) will have a language property that returns a Language object that they are assigned to. It is determined based on under which rootParent the $page lives;

Syntax



$page->language;


Return

Language object.

translation (Page method)

This method returns the page that was assigned as a translation for the given $language.

Syntax



$page->translation($language);


Arguments

The $language argument could be either a string (the name of the language), an integer (the id of the language) or a Language object itself.

Throws

WireException if the language is not found or isn't handled by Babel.

Return

The method returns a Page object or NullPage if the translation is not available.

translations (Page method)

This method will return the pages that were assigned as a translation for all $languages that are available for that page. Or an empty PageArray if no translations are available.

Syntax



$page->translations();


Return

Returns PageArray.

addTranslation (Page method)

This method will create a translation link from $page to $otherPage.

Syntax



$page->addTranslation($otherPage[, $overwrite]);


Arguments

The $otherPage argument should be a Page object. The language of the page will be determined by Babel itself based on under which language section the page is located.

The $overwrite argument is optional and has three case scenarios:

  • If omitted the reverse link of the translation will be created only if the $otherPage does not have a translation link for the $page->language
  • If set to true the reverse link will be overwritten even if $otherPage does have a translation link for the $page->language.
  • If set to false the reverse translation link will not be created even if the $otherPage does not have any translation links.
Return

boolean if the translation link/s is/are created successfully or not.

removeTranslation (Page method)

The method removes a translation link between $page and $otherPage.

Syntax



$page->removeTranslation($language[, $remove]);


Arguments

$language (string|integer|Language) The language link you wish to remove.

$remove (boolean) If there is a reverse link that points back from the translation to this page, should Babel remove it too. Default is false.

Throws

Throws WireException if the language couldn't be found or is not handled by Babel.

Return

boolean If the removal of the translation link/s is/are successful.

closestParentTranslation (Page method)

Returns the translation of the closest translated parent of the page.

Syntax



$page->closestParentTranslation($language);


Arguments

$language (string|integer|Language) The language for which you want a translation for.

Throws

Throws WireException if the language couldn't be found or is not handled by Babel.

Return

Page object.

getRoot (Babel method)

Returns the rootPage of the language. The one you have assigned in the module settings page.

Syntax



$modules->get('Babel')->getRoot($language);


Arguments

$language (string|integer|Language)

Throws

Throws WireException if the language couldn't be found or is not handled by Babel.

Return

Page object.

translatable (Babel method)

Tells if a page is translatable via Babel or not.

Syntax



$modules->get('Babel')->translatable($page);


Arguments

$page is a Page object. The page you want to check on.

Return

boolean If the page is translatable or not.

getTranslated (Babel method)

Returns a PageArray of translated pages. 

Syntax



$modules->get('Babel')->getTranslated($fromLanguage, $toLanguage[, $limit[, $pageNum]]);


Arguments

$fromLanguage (string|integer|Language) The language from which the translation is. If omitted (or null given) then all the translation from all languages will be considered.

$toLanguage (string|integer|Language) The language to which the translation is. If omitted (or null given) then translation to all languages will be considered.

$limit is the number of pages you want to receive. Default is 50.

$pageNum is the page number. Use for pagination of the returned PageArray. Default is 1.

Throws

Throws WireException if the language couldn't be found or is not handled by Babel.

Return

PageArray.

getUntranslated (Babel method)

Returns a PageArray of pages that are not translated to one or the other language.

Syntax



$modules->get('Babel')->getUntranslated($fromLanguage, $toLanguage[, $limit[, $pageNum]]);


Arguments

$fromLanguage (string|integer|Language) The language from which there is no translation. If omitted (or null given) then all the untranslated pages from any language will be considered.

$toLanguage (string|integer|Language) The language to which there is no translation. If omitted (or null given) then untranslated pages to any language will be considered.

$limit is the number of pages you want to receive. Default is 50.

$pageNum is the page number.  Use for pagination of the returned PageArray. Default is 1.

Throws

Throws WireException if the language couldn't be found or is not handled by Babel.

Return

PageArray.

babelHomePage ($config property)

Reference to the home page. This is usually one of the pages that you chose in the module settings.

Syntax



$config->babelHomePage;


The babelHomePage is determined based on:

  the path that user has entered...

  if not available then the session is checked...

  if not available then it defaults to the core LanguageSupport module's default language;

Usage Tips

If your site structure is the way it looks like it is shown above. Your root page usually won't ever render. Whenever a user enters your site at the `/` path you will redirect him to one of language root pages. Here is how I suggest you to do that in your root.php template file.



$session->redirect($config->babelHomePage->url, false);


that's pretty much everything you need to have in your root.php file.


To display links for each language homepage...



$home = $config->babelHomePage;
$homePages = $home->translations()->and($home);
echo "<ul>";
foreach ($homePages as $p) {
$title = $p->language->title;
echo "<li><a href='$p->url'>$title</a></li>";
}
echo "</ul>";


To display links for each translation of the current page...



echo "<ul>";
foreach ($page->translations() as $p) {
$title = $p->language->title;
echo "<li><a href='$p->url'>$title</a></li>";
}
echo "</ul>";



ProcessBabelTranslate

Babel comes with very useful admin helper. You can link pages as a translation for each other. You can create translation page if there isn't one yet. There is small indicators on page tree that shows which pages are translated to what languages. And you get a Babel Tanslate page where you can quickly find all the translated and untranslated pages accross your site. See the screenshots below...

Settings page

pbt_settings.png

Translation indicators

pbt_indicators.png

Quick action button

pbt_action_button.png

Quick modal translate form

pbt_quick_translate.png

Tab translate form

pbt_translate_form.png

ProcessBabelTranslate page

pbt_pages_menu.png

pbt_find.png

Go ahead and try it out. This is my first module to publish. Please tell me if something need to be added, changed, removed...

  • Like 14
Link to comment
Share on other sites

Seems like a lot of work you have done. But why is this module even needed while we have native PW multi-lingual support. I am probably missing something, so please be so kind to point out the differencies.

ProcessWire does not support tree based multi-language sites. This is just another approach to handle sites with many languages. Some people prefer to do it this way.

There are cases where websites have 600 pages in english, 700 pages in russian and only 300 pages in english and russian. In this case it is much better to keep the content separate, each under one section and have their translations linked.

In case for core LanguageSupport module. When you publish an article in english, it will be shown in the russian version of the site too, even if there isn't a russian version of the article. It will also will show in places like navigation, on sidebar with the latest news etc. However if you put everything english under one page and build every parts of your site under that page (your navigation root, latest news) then your article in english language will never be seen in russian version.

Update: The last paragraph is not entirely correct. See the comment by @Wanze below.

  • Like 4
Link to comment
Share on other sites

Nice work :)

In case for core LanguageSupport module. When you publish an article in english, it will be shown in the russian version of the site too, even if there isn't a russian version of the article. It will also will show in places like navigation, on sidebar with the latest news etc. However if you put everything english under one page and build every parts of your site under that page (your navigation root, latest news) then your article in english language will never be seen in russian version.

That's not entirely correct. If you don't check the "active" checkbox under Settings when editing a page, the page is not listed for users with this language, e.g. excluded from $pages->find calls. I'm not sure what the default behaviour is when you visit the page directly in a language which is not active. Maybe you'll see the content in the default language. In this case, I'm sure you could also hook into somehwere and throw a 404 or do the prefered action.

  • Like 1
Link to comment
Share on other sites

Nice work :)

In case for core LanguageSupport module. When you publish an article in english, it will be shown in the russian version of the site too, even if there isn't a russian version of the article. It will also will show in places like navigation, on sidebar with the latest news etc. However if you put everything english under one page and build every parts of your site under that page (your navigation root, latest news) then your article in english language will never be seen in russian version.

That's not entirely correct. If you don't check the "active" checkbox under Settings when editing a page, the page is not listed for users with this language, e.g. excluded from $pages->find calls. I'm not sure what the default behaviour is when you visit the page directly in a language which is not active. Maybe you'll see the content in the default language. In this case, I'm sure you could also hook into somehwere and throw a 404 or do the prefered action.

Yep. You're right. I did not even know that. I guess the difference with this module is only the contextual approach to the multi-language sites. I don't think this module is any better than the core LanguageSupport module, just the other way of doing things. I posted it here for community to decide. If we decide we don't need it the thread will stay only for historical reasons I guess.

Another thing might be the hassle with FieldTypes that do not support multi-language. FieldtypeFile and FieldtypeImage for instance. I know that you can have multi-language descriptions. But what about files themselves. You can't have different files for different languages.

  • Like 4
Link to comment
Share on other sites

I guess the difference with this module is only the contextual approach to the multi-language sites. I don't think this module is any better than the core LanguageSupport module, just the other way of doing things. I posted it here for community to decide. If we decide we don't need it the thread will stay only for historical reasons I guess.

Oh I like your module and it's very nice to have an alternative way of going with multi language  ^-^

Link to comment
Share on other sites

Another thing might be the hassle with FieldTypes that do not support multi-language. FieldtypeFile and FieldtypeImage for instance. I know that you can have multi-language descriptions. But what about files themselves. You can't have different files for different languages.

Language alternate fields is the keyword here. This allows you to setup any field for multi-language frontends. Just from the UI point it's not as nice as all the native ones.

Link to comment
Share on other sites

It is quite often, when you have different sets of pages for different languages of website.

Upon our real-world experience - it is usually looks like complete website in major language, and kind of alternated (reduced) versions for minor translations.

With this approach - it is very easy to create few different content architectures (trees), while still use the same codebase and have such a nice ability to control which pages are corresponded, what we have translated and what parts are still needed to be translated.

Thank you!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...