3

I have in the @thesis entries in my .bib file the names of institutions and types written in the local language of the thesis. I need to translate these to English. As an example, I need to translate institution names like

Universitetet i Oslo

Universitetet i Bergen

Høgskolen i Agder

to

University of Oslo

University of Bergen

Agder University College

and translate type names like

Hovedoppgave

Hovedfagsavhandling

Masteroppgave

to

Main subject thesis

Main subject thesis

MA thesis

Based on similar conversions I've done (Change US postal codes (CA) to AP stylebook abbreviations (Calif.) in bibliography (biblatex)), I have a vague idea what to do, but I don't know how to accurately implement it:

\DeclareSourcemap{
  \maps[datatype = bibtex]{
    \map{
      \step[fieldsource = institution,
          match = , % <- strings like "Universitetet i Oslo", "Universitetet i Bergen"
          replace = ] % <- strings like "University of Oslo", "University of Bergen"
    }
    \map{
      \step[fieldsource = type,
          match = , % <- strings like "Hovedoppgave", "Masteroppgave"
          replace = ] % <- strings like "Main subject thesis", "MA thesis"
    }
  }
}

EDIT

There seems to be a problem doing this when the string I need to replace contains non-ascii characters like æøå:

\documentclass{article}
\usepackage[style = authoryear-comp]{biblatex}
\usepackage{fontspec,filecontents}
\begin{filecontents}{\jobname.bib}
@THESIS{aasen2004,
    AUTHOR = "Anita Aasen",
    INSTITUTION = "Universitetet i Oslo",
    TITLE = "Språklig nivellering i Oslo-regionen",
    TYPE = "Hovedfagsoppgave i nordisk språk og litteratur",
    YEAR = "2004",
    SUBTITLE = "Ungdommers valg av språklige varianter i Follo"}
\end{filecontents}
\DeclareSourcemap{
  \maps[datatype = bibtex]{
    \map{
       \step[fieldsource = type,
          match = {Hovedfagsoppgave i nordisk språk og litteratur}, 
          replace = {Main subject thesis}]
    }
  }
}
\addbibresource{\jobname.bib}
\begin{document}
\nocite{*}
\printbibliography
\end{document}

enter image description here

Sverre
  • 20,729
  • 1
    You might be delighted to hear that the upcoming version 3 of biblatex allows to do exactly that (not automatically though). Then you could use institution = {Universitetet i Oslo}, institution _translated_english = {University of Oslo}, and choose whether the original or translated string is shown. I'm sure the biblatex maintainers are keen on beta testers, you can find a dev version on http://sourceforge.net/projects/biblatex/files/experimental/. – moewe Apr 28 '14 at 16:28
  • I am delighted to hear that, but I need to wait until it's released through texlive. I have no idea how to install packages manually, and I'm not even going to attempt it :) – Sverre Apr 28 '14 at 16:31

2 Answers2

3

Here is a solution using \regexps

\DeclareSourcemap{
  \maps[datatype = bibtex]{
    \map{
      \step[fieldsource = institution,
          match = \regexp{Universitetet(\s)i},           
          replace = \regexp{University$1of}]
      \step[fieldsource = institution,
        match = \regexp{Høgskolen(\s)i(\s)(.*)},
        replace = \regexp{$3$1University$2College}
      ]
      }
    \map{
      \step[fieldsource = type,
          match = {Hovedoppgave}, 
          replace = {Main subject thesis} ]
      \step[fieldsource = type,
          match = {Hovedfagsavhandling}
          replace = {Main subject thesis}]
      \step[fieldsource = type,
          match = {Masteroppgave},
          replace = {MA Thesis}]
    }
  }
}
Guido
  • 30,740
  • I'm pretty sure the \regexp call with institution will fail with some name at one point, but I can see how to avoid that with your solution to the type entries - if that situation arises. – Sverre Apr 28 '14 at 16:03
  • 1
    You should also be able to shorten this a bit by matching {(Hovedoppgave)|(Hovedfagsavhandling)} instead of the two patterns seperately. – musicman Apr 28 '14 at 16:05
  • What does %$ at the end of the replace lines mean? – Sverre Apr 28 '14 at 16:05
  • 1
    nothing it was a comment to make my editor syntax highlight happy :-). I have removed them now – Guido Apr 28 '14 at 16:06
  • 1
    @musicman yes, but you have to use match=\regexp{(Hovedoppgave)|(Hovedfagsavhandling)} – Guido Apr 28 '14 at 16:09
  • I can't get any of these suggestions to work when the original string contains letters like æøå ... – Sverre Apr 28 '14 at 16:19
  • Do you include \usepackage[utf8]{inputenc}? It works for me. – Guido Apr 28 '14 at 16:26
  • I'm using XeLaTeX. I'll see if I can get a MWE up. – Sverre Apr 28 '14 at 16:30
  • You can try to include \usepackage[bibencoding=utf8]{biblatex} – Guido Apr 28 '14 at 16:32
  • If that implies that my .bib file is saved in utf8 format, it's not - it's in ansi. All non-ansi characters are coded with their Unicode point, e.g. å is written as {\char"00E5}. EDIT: I tried adding it to my MWE anyway, but it has no effect. – Sverre Apr 28 '14 at 16:35
  • That seems command-line like ... ? I'm compiling with TeXworks in Windows. – Sverre Apr 28 '14 at 16:43
2

I would not use regex for this. Isn't it easier to define commands for the local strings and put them in different tex-files?For example:

swedish.tex:

\newcommand*{\strhogskolen}{Høgskolen i Agder}


english.tex:

\newcommand*{\strhogskolen}{Agder University College}

In your main-texfile then you just have to include the local file via \input{swedish} and use it in the biblatex-file like:

@book{somebook,
author={an author},
title={title},
location={\strhogskolen}
}
musicman
  • 3,559
  • I agree that using regular expressions doesn't make much sense here, which is why I didn't include regex in my example. It seems to me that the disadvantage to your suggestion is that I would have to retype all the relevant entries in my .bib file, and always have to include some other .tex file to compile a document that refers to these entries. – Sverre Apr 28 '14 at 15:52
  • @Sverre -- note that you can use @preamble{ \newcommand{\whatever}{definition goes here} } in your .bib file. It doesn't solve the re-typing problem, but note that the \DeclareSourceMap route requires re-typing and extra typing for each re-map. – jon Apr 28 '14 at 15:57
  • @jon If you look at the linked question for postal codes, \DeclareSourceMap only needs to be called once, followed by a list of \def commands that change from the original string to my new string. That doesn't seem to me to involve any more typing that equally many \newcommand lines in my .bib file? – Sverre Apr 28 '14 at 16:00
  • @Sverre -- well, how much more typing is unclear since I haven't tried to implement it. Of course, you could use \def in the @preamble commands too. It was the regex replace via \DeclareSourceMap that adds the extra typing, not the \def-ed strings. (Anyway, I was only making the suggestion because of your first comment about the need to include some other .tex file: @preamble is just a way to keep everything in the .bib file...) I am curious about a solution because I care about language localization for city names based on the main language loaded by babel/polyglossia! – jon Apr 28 '14 at 16:19