Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with javascript internationalization

Is there any mechanism in yii framework to translate messages in javascript source files.

For example with:

yiic message config-message.php

I can generate translation .php files for all Yii::t().

Is it possible to generate .js files with the same mechanism. Currently I'm including to my javascript json data with table returned by translation file.

like image 885
liysd Avatar asked Aug 01 '10 17:08

liysd


People also ask

What is internationalization in js?

Internationalization (I18N) is the process of designing and preparing software products (apps) to support multiple locales, languages, and regions. By internationalizing a codebase, developers and businesses can expand their user base and access a wider audience.

What are locales in js?

A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.

What is polyglot js?

Polyglot. js is a tiny I18n helper library written in JavaScript, made to work both in the browser and in CommonJS environments (Node). It provides a simple solution for interpolation and pluralization, based off of Airbnb's experience adding I18n functionality to its Backbone. js and Node apps.


3 Answers

An alternate solution would be to make Yii include translated messages in the DOM, that will be later accessed by your JS code, so:

  1. You do not need to repeat JS code.
  2. Translation is centralized in the PHP/Yii framework.
like image 97
ereslibre Avatar answered Oct 17 '22 21:10

ereslibre


I've created an extension that should handle this:

http://www.yiiframework.com/extension/jstrans/

like image 36
Henk Avatar answered Oct 17 '22 23:10

Henk


It's a really problem. Multi languages sites is always problem. YII solve the problem for php code. But js code problem still on place.

I do not very elegant solution but it's works. Create controller/component and generate js code with localization on the fly using t().

/js/get/?file=some-js-file in layout:

Of course, every JS need to be rewrite. If use small pieces of code (cs()->RegisterScript) I use t() as well.

like image 2
Andrejs Pankins Avatar answered Oct 17 '22 23:10

Andrejs Pankins