Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
b7f01f2b
Commit
b7f01f2b
authored
May 03, 2017
by
Phil Hughes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved all the text translation manipulation into the locale index file
Commented the translation methods
parent
ee65de48
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
14 deletions
+73
-14
app/assets/javascripts/locale/index.js
app/assets/javascripts/locale/index.js
+44
-4
app/assets/javascripts/vue_shared/translate.js
app/assets/javascripts/vue_shared/translate.js
+29
-10
No files found.
app/assets/javascripts/locale/index.js
View file @
b7f01f2b
import
Jed
from
'
jed
'
;
import
Jed
from
'
jed
'
;
/**
This is required to require all the translation folders in the current directory
this saves us having to do this manually & keep up to date with new languages
**/
function
requireAll
(
requireContext
)
{
return
requireContext
.
keys
().
map
(
requireContext
);
}
function
requireAll
(
requireContext
)
{
return
requireContext
.
keys
().
map
(
requireContext
);
}
const
allLocales
=
requireAll
(
require
.
context
(
'
./
'
,
true
,
/^
(?!
.*
(?:
index.js$
))
.*
\.
js$/
));
const
allLocales
=
requireAll
(
require
.
context
(
'
./
'
,
true
,
/^
(?!
.*
(?:
index.js$
))
.*
\.
js$/
));
...
@@ -16,11 +20,47 @@ let lang = document.querySelector('html').getAttribute('lang') || 'en';
...
@@ -16,11 +20,47 @@ let lang = document.querySelector('html').getAttribute('lang') || 'en';
lang
=
lang
.
replace
(
/-/g
,
'
_
'
);
lang
=
lang
.
replace
(
/-/g
,
'
_
'
);
const
locale
=
new
Jed
(
locales
[
lang
]);
const
locale
=
new
Jed
(
locales
[
lang
]);
/**
Translates `text`
@param text The text to be translated
@returns {String} The translated text
**/
const
gettext
=
locale
.
gettext
.
bind
(
locale
);
const
gettext
=
locale
.
gettext
.
bind
(
locale
);
const
ngettext
=
locale
.
ngettext
.
bind
(
locale
);
const
pgettext
=
(
context
,
key
)
=>
{
/**
const
joinedKey
=
[
context
,
key
].
join
(
'
|
'
);
Translate the text with a number
return
gettext
(
joinedKey
).
split
(
'
|
'
).
pop
();
if the number is more than 1 it will use the `pluralText` translation.
This method allows for contexts, see below re. contexts
@param text Singular text to translate (eg. '%d day')
@param pluralText Plural text to translate (eg. '%d days')
@param count Number to decide which translation to use (eg. 2)
@returns {String} Translated text with the number replaced (eg. '2 days')
**/
const
ngettext
=
(
text
,
pluralText
,
count
)
=>
{
const
translated
=
locale
.
ngettext
(
text
,
pluralText
,
count
).
replace
(
/%d/g
,
count
).
split
(
'
|
'
);
return
translated
[
translated
.
length
-
1
];
};
/**
Translate context based text
Either pass in the context translation like `Context|Text to translate`
or allow for dynamic text by doing passing in the context first & then the text to translate
@param keyOrContext Can be either the key to translate including the context
(eg. 'Context|Text') or just the context for the translation
(eg. 'Context')
@param key Is the dynamic variable you want to be translated
@returns {String} Translated context based text
**/
const
pgettext
=
(
keyOrContext
,
key
)
=>
{
const
normalizedKey
=
key
?
`
${
keyOrContext
}
|
${
key
}
`
:
keyOrContext
;
const
translated
=
gettext
(
normalizedKey
).
split
(
'
|
'
);
return
translated
[
translated
.
length
-
1
];
};
};
export
{
lang
};
export
{
lang
};
...
...
app/assets/javascripts/vue_shared/translate.js
View file @
b7f01f2b
import
{
import
{
__
,
__
,
n__
,
n__
,
s__
,
}
from
'
../locale
'
;
}
from
'
../locale
'
;
export
default
(
Vue
)
=>
{
export
default
(
Vue
)
=>
{
Vue
.
mixin
({
Vue
.
mixin
({
methods
:
{
methods
:
{
/**
Translates `text`
@param text The text to be translated
@returns {String} The translated text
**/
__
(
text
)
{
return
__
(
text
);
},
__
(
text
)
{
return
__
(
text
);
},
n__
(
text
,
pluralText
,
count
)
{
/**
const
translated
=
n__
(
text
,
pluralText
,
count
).
replace
(
/%d/g
,
count
).
split
(
'
|
'
);
Translate the text with a number
return
translated
[
translated
.
length
-
1
];
if the number is more than 1 it will use the `pluralText` translation.
},
This method allows for contexts, see below re. contexts
s__
(
keyOrContext
,
key
)
{
const
normalizedKey
=
key
?
`
${
keyOrContext
}
|
${
key
}
`
:
keyOrContext
;
// eslint-disable-next-line no-underscore-dangle
const
translated
=
this
.
__
(
normalizedKey
).
split
(
'
|
'
);
return
translated
[
translated
.
length
-
1
];
@param text Singular text to translate (eg. '%d day')
},
@param pluralText Plural text to translate (eg. '%d days')
@param count Number to decide which translation to use (eg. 2)
@returns {String} Translated text with the number replaced (eg. '2 days')
**/
n__
(
text
,
pluralText
,
count
)
{
return
n__
(
text
,
pluralText
,
count
);
},
/**
Translate context based text
Either pass in the context translation like `Context|Text to translate`
or allow for dynamic text by doing passing in the context first & then the text to translate
@param keyOrContext Can be either the key to translate including the context
(eg. 'Context|Text') or just the context for the translation
(eg. 'Context')
@param key Is the dynamic variable you want to be translated
@returns {String} Translated context based text
**/
s__
(
keyOrContext
,
key
)
{
return
s__
(
keyOrContext
,
key
);
},
},
},
});
});
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment