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

Improved localisation #2177

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions docs/guides/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ Video.js uses key/value object dictionaries in JSON form. A sample dictionary fo
```

Notes:

- The file name should always be in the format `XX.json`, where `XX` is the two letter value of the language reported to the browser (for options see the bottom of this document).
- For automatic inclusion at build time, add your language file to the `/lang` directory (see 'Adding Languages to Video.js below').

Adding Languages to Video.js
----------------------------
Additional language support can be added to Video.js in multiple ways.

1. Create language scripts out of your JSON objects by using our custom grunt task `vjslanguages`. This task is automatically run as part of the default grunt task in Video.JS, but can be configured to match your `src`/`dist` directories if different. Once these scripts are created, just add them to your DOM like any other script.
1. Create language scripts out of your JSON objects by using our custom grunt task `vjslanguages`. This task is automatically run as part of the default grunt task in Video.JS, but can be configured to match your `src`/`dist` directories if different. Once these scripts are created, just add them to your DOM like any other script.

NOTE: These need to be added after the core Video.js script.

Expand Down Expand Up @@ -122,6 +122,24 @@ During a Video.js player instantiation you can force it to localize to a specifi
</video>
```

Determining Player Language
Copy link
Member

Choose a reason for hiding this comment

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

Very nice addition.

---------------------------

The player language is set to one of the following in descending priority

* The language set in setup options as above
* The document language (`lang` attribute of the `html` element)
* Browser language preference
* 'en'

That can be overridden after instantiation with `language('fr')`.

Language selection
------------------

* Language codes are considered case-insensitively (`en-US` == `en-us`).
* If there is no match for a language code with a subcode (`en-us`), a match for the primary code (`en`) is used if available.

Localization in Plugins
-----------------------

Expand All @@ -133,7 +151,7 @@ var details = '<div class="vjs-errors-details">' + player.localize('Technical de

Language Codes
--------------
The following is a list of official language codes.
The following is a list of official language codes.

**NOTE:** For supported language translations, please see the [Languages Folder (/lang)](../../lang) folder located in the project root.

Expand Down Expand Up @@ -180,10 +198,10 @@ The following is a list of official language codes.
<tr><th>fj<th><td>Fiji</td></tr>
<tr><th>fi<th><td>Finnish</td></tr>
</table>

</td>
<td>

<table>
<tr><th>fr<th><td>French</td></tr>
<tr><th>fy<th><td>Frisian</td></tr>
Expand Down Expand Up @@ -223,10 +241,10 @@ The following is a list of official language codes.
<tr><th>lo<th><td>Laothian</td></tr>
<tr><th>la<th><td>Latin</td></tr>
</table>

</td>
<td>

<table>
<tr><th>lv<th><td>Latvian (Lettish)</td></tr>
<tr><th>li<th><td>Limburgish ( Limburger)</td></tr>
Expand Down Expand Up @@ -266,10 +284,10 @@ The following is a list of official language codes.
<tr><th>ii<th><td>Sichuan Yi</td></tr>
<tr><th>sd<th><td>Sindhi</td></tr>
</table>

</td>
<td>

<table>
<tr><th>si<th><td>Sinhalese</td></tr>
<tr><th>ss<th><td>Siswati</td></tr>
Expand Down Expand Up @@ -307,7 +325,7 @@ The following is a list of official language codes.
<tr><th>yo<th><td>Yoruba</td></tr>
<tr><th>zu<th><td>Zulu</td></tr>
</table>

</td>
</tr>
</table>
14 changes: 11 additions & 3 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,21 @@ class Component {
}

localize(string) {
let lang = this.player_.language();
let lang = ('' + this.player_.language()).toLowerCase();
let primaryCode = lang.split('-')[0];
let languages = this.player_.languages();

if (languages && languages[lang] && languages[lang][string]) {
if (!languages) {
return string;
}
if (languages[lang] && languages[lang][string]) {
return languages[lang][string];
}

if (primaryCode !== lang &&
languages[primaryCode] &&
languages[primaryCode][string]) {
return languages[primaryCode][string];
}
return string;
}

Expand Down
16 changes: 14 additions & 2 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ class Player extends Component {
this.language_ = options['language'] || Options['language'];

// Update Supported Languages
this.languages_ = options['languages'] || Options['languages'];
if (options['languages']) {
// Normalise player option languages to lowercase
let languagesToLower = {};

Object.getOwnPropertyNames(options['languages']).forEach(function(name) {
languagesToLower[name.toLowerCase()] = options['languages'][name];
});
this.languages_ = languagesToLower;
} else {
this.languages_ = Options['languages'];
}

// Cache for video property values.
this.cache_ = {};
Expand Down Expand Up @@ -2105,9 +2115,11 @@ class Player extends Component {

/**
* Get the player's language dictionary
* Merge every time, because a newly added plugin might call videojs.addLanguage() at any time
* Languages specified directly in the player options have precedence
*/
languages() {
return this.languages_;
return mergeOptions(Options['languages'], this.languages_);
}

toJSON() {
Expand Down
3 changes: 2 additions & 1 deletion src/js/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ videojs.plugin = plugin;
* @return {Object} The resulting global languages dictionary object
*/
videojs.addLanguage = function(code, data){
if(Options['languages'][code] !== undefined) {
code = ('' + code).toLowerCase();
if(typeof Options['languages'][code] !== 'undefined') {
Options['languages'][code] = mergeOptions(Options['languages'][code], data);
} else {
Options['languages'][code] = data;
Expand Down
22 changes: 22 additions & 0 deletions test/unit/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,25 @@ test('should have a sensible toJSON that is equivalent to player.options', funct

deepEqual(player2.toJSON(), popts, 'no circular references');
});

test('should ignore case in language codes and try primary code', function() {
expect(3);

var player = TestHelpers.makePlayer({
'languages': {
'en-gb': {
'Good': 'Brilliant'
},
'EN': {
'Good': 'Awesome',
'Error': 'Problem'
}
}
});

player.language('en-gb');
strictEqual(player.localize('Good'), 'Brilliant', 'Used subcode specific localisation');
strictEqual(player.localize('Error'), 'Problem', 'Used primary code localisation');
player.language('en-GB');
strictEqual(player.localize('Good'), 'Brilliant', 'Ignored case');
});
11 changes: 11 additions & 0 deletions test/unit/video.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ test('should add the value to the languages object', function() {
deepEqual(result[code], Options['languages'][code], 'should also match');
});

test('should add the value to the languages object with lower case lang code', function() {
var code, data, result;

code = 'DE';
data = {'Hello': 'Guten Tag'};
result = videojs.addLanguage(code, data);

ok(Options['languages'][code.toLowerCase()], 'should exist');
equal(Options['languages'][code.toLowerCase()], data, 'should match');
deepEqual(result[code.toLowerCase()], Options['languages'][code.toLowerCase()], 'should also match');
});

test('should expose plugin registry function', function() {
var pluginName, pluginFunction, player;
Expand Down