Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation #31

Closed
wants to merge 7 commits into from
Closed

Translation #31

wants to merge 7 commits into from

Conversation

thblckjkr
Copy link
Contributor

I've been working in a simple approach to translate the web pages with the less effort possible.

The result, the following code. I hope you like it.

Use
The use it's pretty simple, in the PHP file to be translated, just include the file that acts as translator, and use the function lang()

include 'lang/translate.php'; //Include this at top
<?=lang('ANY TEXT')?> <!-- Translate "ANY TEXT" (HTML format) -->

The lang() function search the keyword on the language files, if no language file exist or the keyword is not found, the original text is returned, if the keyword and file are found, the translated phrase is returned. The current language it's selected by the $_SERVER['HTTP_ACCEPT_LANGUAGE'] directive.

The language files are in JSON format, to make possible the implementation (on a easy way) to JavaScript and another languages.

Only the index page was translated (To Spanish, because i'm Mexican). Just for testing, also the template.json will help to any translation. And the button of search is being displayed in Chinese (for Chinese users, also just for testing).

If you can give any kind of feedback i'll be happy.

@Genteure
Copy link

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

There's zh-CN zh-TW zh-HK fr-CA fr-FR fr-BE en-US en-GB etc.
I'm not sure if there's major difference between fr-CA and fr-FR, but it definitely is different between zh-CN and zh-TW.
A better way to do this is parse Accept-Language, fallback to next one when a language doesn't exist.

My browser's header:

accept-language: en,en-US;q=0.9,zh-CN;q=0.8,zh;q=0.7

@Genteure
Copy link

Genteure commented Dec 20, 2018

$lang = (isset($_GET['lang'])) ? $_GET['lang'] : $lang;
$lang_file = INCLUDE_PATH . '//lang/' . LANGUAGE . '.json';
$lang_file_content = file_get_contents($lang_file);

Also you may want to use string realpath ( string $path ) to check if $lang_file is still inside the lang directory, prevent someone request path like https://trace.moe/?lang=../../../../etc/passwd.
Although passwd definitely can not parsed by json parser, read arbitrary file is bad.

"Anime Scene Search Engine": "",
"Browse a file": "",
"Drag &amp; Drop Anime ScreenShot / Ctrl+V / Enter Image URL": "",
"Please read <a href=\"/faq\">FAQ</a> to understand what can / cannot be searched": "<a href=\"/faq\">FAQ</a>",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not a good idea to put html tags in object keys :\

@soruly
Copy link
Owner

soruly commented Dec 20, 2018

I also wanted to translate pages like about.php and faq.php
It doesn't seems nice to me if we put those text in a big json and leave that page with a lot of placeholders.

@thblckjkr
Copy link
Contributor Author

Thanks for the feedback. In retrospective, I can see that the approach was not the best for the project. I will be working in a better way to do it. Thanks for your comments.

@thblckjkr thblckjkr closed this Dec 21, 2018
@soruly soruly mentioned this pull request Apr 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants