Skip to content

Instantly share code, notes, and snippets.

@MrCheatEugene
Created January 24, 2023 16:07
Show Gist options
  • Save MrCheatEugene/d16449a135e1c2c913f2b9b9587178d0 to your computer and use it in GitHub Desktop.
Save MrCheatEugene/d16449a135e1c2c913f2b9b9587178d0 to your computer and use it in GitHub Desktop.
Simple translator script, easy to embed. [PHP]
<?php
if (!defined("translator_loaded")) {
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$acceptLang = ['en','ru'];
$user_pref_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($user_pref_langs as $idx => $llang) {
$llang = substr($llang, 0, 2);
if (in_array($llang, $acceptLang)) {
$lang=$llang;
break;
}
}
$lang = in_array($lang, $acceptLang) ? $lang : 'en';
$packet= json_decode(file_get_contents(".".$lang),true);
foreach ($packet as $key => $value) {
if (!defined($key)){
$key = str_replace(".", "_",$key);
define($key, $value);
}
}
function e($var){
$var = str_replace(".", "_",$var);
echo constant($var);
}
define("translator_loaded",true);
}
?>
@MrCheatEugene
Copy link
Author

  1. Download this file
  2. Include it in your something.php
  3. Replace words with
  4. Create .en file
  5. Add this: {"some.keywords.that.can.be.split.by.dots":"Some translational JSON-Escaped words "especially for quotes""}
  6. Done, add languages and the same file with it's 2-letter code(.ru, .de, .fr)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment