Created
January 24, 2023 16:07
-
-
Save MrCheatEugene/d16449a135e1c2c913f2b9b9587178d0 to your computer and use it in GitHub Desktop.
Simple translator script, easy to embed. [PHP]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
?> |
Author
MrCheatEugene
commented
Jan 24, 2023
- Download this file
- Include it in your something.php
- Replace words with
- Create .en file
- Add this: {"some.keywords.that.can.be.split.by.dots":"Some translational JSON-Escaped words "especially for quotes""}
- 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