Skip to content

Instantly share code, notes, and snippets.

@Systerr
Last active August 29, 2015 14:19
Show Gist options
  • Save Systerr/98cddd4da2d616455060 to your computer and use it in GitHub Desktop.
Save Systerr/98cddd4da2d616455060 to your computer and use it in GitHub Desktop.
Modx bable plugin multilang

#How to use

Create nessesary contexts for each language:

Each context setting is (name,key) => val (comment):

(Base URL, base_url) => / (or /en/ for english,etc).

(Culture key, cultureKey) => ru (or en for english,etc).

(Site start, site_start) => 1 (it's a ID of home document, like 404 eror page id)

(Site URL, site_url) => http://example.com/ (or http://example.com/en/ for english. Be careful, should match base_url).

(error_page,error_page) => 2 (it's a ID of 404 error page)

##Create new namespace 'YOU_NAMESPACE' Core path should be {core_path}components/YOU_NAMESPACE/

Create file(s) {core_path}components/YOU_NAMESPACE/lexicon/{{cultureKey}}/default.inc.php

<?php
$_lang['download'] = 'Download';
...

For other languages array key should be the same, but array value may be transtaled. For example for japanese:

<?php
$_lang['download'] = 'ダウンロード';
...

##Translate all nessesary words ##Create chunk named "translate"

[[%[[+key]]? &topic=`default` &namespace=`YOU_NAMESPACE` &language=[[++cultureKey]]]]

Use chunk as

[[$translate?key=`download`]]

##Install Babel http://rtfm.modx.com/extras/revo/babel

##Create new plugin
See gateway.php above

##Get linked resources You have some page id on russian context and you want to get this page on another content (let say in 'English' content:

[[BabelTranslation? &contextKey=`[[*context_key]]` &resourceId=`[[*id]]`]]

http://rtfm.modx.com/extras/revo/babel/babel.babeltranslation

Lets create a chunk 'gti' (Get Translated Id)

[[BabelTranslation? &contextKey=`[[*context_key]]` &resourceId=`[[+id]]`]]

How we can use it as:

[[$gti?id=`10`]]
<?php
/*
Plugin for switch context for MODX REVO. Works on nginx and apache. Not required to changing server configs.
Attach plugin to event: OnHandleRequest
Each context setting is (name,key) => val (comment):
(Base URL, base_url) => / (or /en/ for english,etc)
(Culture key, cultureKey) => ru (or en for english,etc)
(Site start, site_start) => 1 (it's a ID of home document, like 404 eror page id)
(Site URL, site_url) => http://example.com/ (or http://example.com/en/ for english. Be careful, should match base_url)
*/
$lankey = substr($_SERVER['REQUEST_URI'], 1, 2);
if ($modx->context->get('key') != "mgr") {
/* grab the current language from the cultureKey request var */
switch ($lankey) {
case 'ru':
/* switch the context */
$modx->switchContext('web');
//set the cultureKey
$modx->setOption('cultureKey', 'ru');
$_REQUEST["q"] = substr($_REQUEST["q"], 3);
break;
case 'ANOTHER LANG':
/* switch the context */
$modx->switchContext('ANOTGHER LANG');
//set the cultureKey
$modx->setOption('cultureKey', 'ANOTGHER LANG');
$_REQUEST["q"] = substr($_REQUEST["q"], 3);
break;
default:
/* Set the default context here */
$modx->switchContext('English');
//set the cultureKey
$modx->setOption('cultureKey', 'en');
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment